Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Tkinter Class

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.


    import Tkinter
    import ttk
except ImportError:  # Python 3
    import tkinter as Tkinter
    import tkinter.ttk as ttk

class Window(Tkinter.Frame):
    def __init__(self, master):
        Tkinter.Frame.__init__(self, )

    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)

        # 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",
        self.submit_button.grid(row=2, column=1, sticky=Tkinter.W)
        self.exit_button = Tkinter.Button(self.master, text="Exit",
        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",
        # Increment counter
        self.i = self.i + 1

def main():

if __name__=="__main__":
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:
and the python doc:

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Use function from other class (Tkinter) zarize 5 100 4 hours ago
Last Post: Yoriz
  Unable fetch fucntion data in class in tkinter jenkins43 2 802 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  Tkinter Gui ScrolledText Insert From Different Class whisperquiet 1 1,823 Jan-08-2019, 09:25 PM
Last Post: Larz60+
  Using a class to create instances of Tkinter Toplevel() windows nortski 2 5,112 Mar-27-2018, 11:44 AM
Last Post: nortski

Forum Jump:

Users browsing this thread: 1 Guest(s)