Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter Class
#1
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()
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Using Tkinter With Concurrent.Futures / ThreadPoolExecutor Class AaronCatolico1 1 1,417 Dec-14-2022, 08:01 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,967 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,597 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  tkinter moving an class object with keybinds gr3yali3n 5 3,183 Feb-10-2021, 09:13 PM
Last Post: deanhystad
  [Tkinter] Troubles with accessing attr from other class zarize 3 2,576 Aug-20-2020, 06:05 PM
Last Post: deanhystad
  [Tkinter] Use function from other class (Tkinter) zarize 8 4,688 Aug-17-2020, 09:47 AM
Last Post: zarize
  Unable fetch fucntion data in class in tkinter jenkins43 2 3,829 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  Tkinter Gui ScrolledText Insert From Different Class whisperquiet 1 4,270 Jan-08-2019, 09:25 PM
Last Post: Larz60+
  Using a class to create instances of Tkinter Toplevel() windows nortski 2 10,872 Mar-27-2018, 11:44 AM
Last Post: nortski

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020