Nov-24-2019, 02:28 AM
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
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()
