Previous entries in this series:
Previously we have made an application that doesn’t really do anything. Its purpose was to show the necessary code to create the application in a GUI on the screen. Now it’s time to create an application that actually does something.
The application that we are going to make is based on the wonderful tutorial provided by TkDocs. specifically, we are going to be recreating a first (real) example, but instead of using non-object-oriented code, we will be using python classes.
There is plenty of explaining to do on the code, but instead of going for a line-by-line explanation, I will be following the tutorial at TkDocs to explain each different piece.
So, without further ado (or any real explanation) here is the example feet to meters program using python’s object-orientated programming:
# filename = ttk_ft2m.py # # This app was based on the sample program found at: # http://www.tkdocs.com/tutorial/firstexample.html # The original program was Copyright (c) 2007-2015 # by Mark Roseman under A Creative Commons # Attribution-Noncommercial-Share Alike 2.5 Canada License: # https://creativecommons.org/licenses/by-nc-sa/2.5/ca/ # # This app was altered to use classes as well as to make # tkinter calls more visible by importing tkinter as tk. # It is Copyright (c) 2016 by Marisa D. Burnes # under the compatible Creative Commons # Attribution-Noncommercial-Share Alike 2.5 Generic License: # https://creativecommons.org/licenses/by-nc-sa/2.5/ # import tkinter as tk from tkinter import ttk class Application(ttk.Frame): def __init__(self, master=None, *args, **kwargs): super().__init__(master, *args, **kwargs) self.grid(column=0, row=0, sticky=tk.NSEW) self.columnconfigure(0, weight=1) self.rowconfigure(0, weight=1) self.create_widgits() for child in self.winfo_children(): child.grid_configure(padx=5, pady=5) def create_widgits(self): self.feet = tk.StringVar() self.meters = tk.StringVar() feet_entry = ttk.Entry(self, width=7, textvariable=self.feet) feet_entry.grid(column=2, row=1, sticky=tk.EW) ttk.Label(self, textvariable=self.meters).grid(column=2, row=2, sticky=tk.EW) ttk.Button(self, text='Calculate', command=self.calculate).grid(column=3, row=3, sticky=tk.W) ttk.Label(self, text='feet').grid(column=3, row=1, sticky=tk.W) ttk.Label(self, text='is equivalent to').grid(column=1, row=2, sticky=tk.E) ttk.Label(self, text='meters').grid(column=3, row=2, sticky=tk.W) feet_entry.bind('<Return>', self.calculate) feet_entry.focus() def calculate(self, *args): try: value = float(self.feet.get()) self.meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0) except ValueError: pass root = tk.Tk() root.title('Feet to Meters') app = Application(master=root, padding='12 3 12 3') app.mainloop()
This application was created with Python version 3.5.2. I will assume that you have a relatively recent version of Python 3.x installed on your system with the binaries available in your path. If you don’t, please go to https://www.python.org/downloads/ and install a recent 3.x version.
To make this application run under Windows, all you need to do is copy and paste it into a file with the Python extension, then launch the newly created file from the command line.