Tkinter Class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Tkinter Class (/thread-22719.html) |
Tkinter Class - pythonenthusiast2 - Nov-24-2019 Hi, I need some help understanding the class in the code below. I am using the code to learn from and have searched the internet and have not found any information that I understand. Why is a class necessary and is this a standard class for Tkinter. What do the contents of the class actually mean? Any help would be appreciated. Thanks PE try: import Tkinter import ttk except ImportError: # Python 3 import tkinter as Tkinter import tkinter.ttk as ttk class Window(Tkinter.Frame): ''' classdocs ''' def __init__(self, master): ''' Constructor ''' Tkinter.Frame.__init__(self, ) self.master=master self.initialize_user_interface() def initialize_user_interface(self): """Draw a user interface allowing the user to type items and insert them into the treeview """ self.master.title("Canvas Test") self.master.grid_rowconfigure(0, weight=1) self.master.grid_columnconfigure(0, weight=1) self.master.config(background="lavender") # Define the different GUI widgets self.dose_label = Tkinter.Label(self.master, text="Doserr:") self.dose_entry = Tkinter.Entry(self.master) self.dose_label.grid(row=0, column=0, sticky=Tkinter.W) self.dose_entry.grid(row=0, column=1) self.modified_label = Tkinter.Label(self.master, text="Date Modified:") self.modified_entry = Tkinter.Entry(self.master) self.modified_label.grid(row=1, column=0, sticky=Tkinter.W) self.modified_entry.grid(row=1, column=1) self.submit_button = Tkinter.Button(self.master, text="Insert", command=self.insert_data) self.submit_button.grid(row=2, column=1, sticky=Tkinter.W) self.exit_button = Tkinter.Button(self.master, text="Exit", command=self.master.quit) self.exit_button.grid(row=0, column=3) # Set the treeview self.tree = ttk.Treeview(self.master, columns=('Title', 'Author','Year','ISBN')) self.tree.heading('#0', text='Title') self.tree.heading('#1', text='Author') self.tree.heading('#2', text='Year') self.tree.heading('#3', text='ISBN') self.tree.column('#1', stretch=Tkinter.YES) self.tree.column('#2', stretch=Tkinter.YES) self.tree.column('#0', stretch=Tkinter.YES) self.tree.column('#3', stretch=Tkinter.YES) self.tree.grid(row=4, columnspan=4, sticky='nsew') self.treeview = self.tree # Initialize the counter self.i = 0 def insert_data(self): """ Insertion method. """ self.treeview.insert('', 'end', text="Item_"+str(self.i), values=(self.dose_entry.get() + " mg", self.modified_entry.get())) # Increment counter self.i = self.i + 1 def main(): root=Tkinter.Tk() d=Window(root) root.mainloop() if __name__=="__main__": main() RE: Tkinter Class - Larz60+ - Nov-24-2019 A class is a container for a bunch of related functions, which once they become part of the class are now referred to as methods. the __init__ method is used to initialize any inherited classes, define variables that are used throught the class, and any other objects that need to be initialized. This class inherits tkinter.Frame, so initializes that class, and makes master visible to all methods and then executes the initialize_user_interface method which sets up the widgets being use by the application. Once the class has been created, new instances of the class can be created simply by using simple syntax x = MyClass() You really ought to read up on classes as they are a bit complex here are a couple of links: https://www.w3schools.com/python/python_classes.asp and the python doc: https://docs.python.org/3/tutorial/classes.html |