Skip to content Skip to sidebar Skip to footer

Object Orientated Tkinter Functions - How Would I Put This In?

I am coding a program which will need functions to change labels and enter text into text boxes however I do not know how to do this with a new style of programming which I am usin

Solution 1:

I've modified one of your page classes to illustrate how it could be done. It involved adding a Label to hold the message, a Button to control it, and a function, called simply handler(), to call when the latter is pressed. It saves the widgets by making them attributes of the containing Frame subclass instance, self, so they can be easily referenced in the handler() function (without resorting to global variables).

classPageOne(tk.Frame):
    def__init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(3, weight=1)
        label = tk.Label(self, text="Page One!!!", font=Title_Font)
        label.grid(row=1, column=1)

        self.msg_label = tk.Label(self, text="")
        self.msg_label.grid(row=2, column=1)
        self.msg_button = tk.Button(self, text='Show Message',
                                    command=self.handler)
        self.msg_button.grid(row=3, column=1)
        self.msg_toggle = False

        bk2_menu = tk.Button(self, text="Back to Home", 
                             command=lambda: controller.show_frame(StartPage))
        bk2_menu.grid(row=5, column=1)

    defhandler(self):
        self.msg_toggle = not self.msg_toggle
        if self.msg_toggle:
            self.msg_label.config(text='Hello')
            self.msg_button.config(text='Clear Message')
        else:
            self.msg_label.config(text='')
            self.msg_button.config(text='Show Message')

Screenshots

Before button is pressed:

screenshot showing before button is pressed

After button is pressed:

screenshot showing before button is pressed

Post a Comment for "Object Orientated Tkinter Functions - How Would I Put This In?"