O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Our first try

Let's give it a try the regular way. Open up your previous code and change it to look like this:

class Window(tk.Tk):    def __init__(self):        super().__init__()        self.title("Hello Tkinter")        self.label_text = "Choose One"        self.label = tk.Label(self, text=self.label_text)        self.label.pack(fill=tk.BOTH, expand=1, padx=100, pady=30)        hello_button = tk.Button(self, text="Say Hello",                                  command=self.say_hello)        hello_button.pack(side=tk.LEFT, padx=(20, 0), pady=(0, 20))        goodbye_button = tk.Button(self, text="Say Goodbye",                                   command=self.say_goodbye)        goodbye_button.pack(side=tk.RIGHT, padx=(0, 20), pady=(0, 20))        def say_hello(self):          self.label_text = "Hello World"        def say_goodbye(self):          self.label_text="Goodbye! \n (Closing in 2 seconds)" self.after(2000, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required