2

I am working through an Oreilly Tutorial on tkinter, but the code provided in the tutorial doesn't work for me. The "Choose one" message doesn't show, instead it shows: PY_VAR0. When I click the hello button nothing happens. When I click the goodbye button the window closes as expected but no message is shown.

Of note, prior I had:

def say_hello(self):
  self.label.configure(text="Hello World!")

def say_goodbye(self):
  self.label.configure(text="Goodbye! \n (Closing in 2 seconds)")
  self.after(2000, self.destroy)

And received an attribute error: attributeerror: '_tkinter.tkapp' object has no attribute 'label' site:stackoverflow.com.

I am uncertain what is wrong as I have followed the example explicitly in both cases.

My code is below:

import tkinter as tk

class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Hello Tkinter')
        self.label_text = tk.StringVar()
        self.label_text.set("Choose One")

        label = tk.Label(self, text=self.label_text)
        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.set("Hello World!")

    def say_goodbye(self):
        self.label_text.set("Goodbye! \n (Closing in 2 seconds)")
        self.after(2000, self.destroy)


if __name__ == "__main__":
    window = Window()
    window.mainloop()

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Search this site for "PY_VAR0". You'll find lots of similar questions. As for "no attribute 'label'", well, that's telling you the truth. – Bryan Oakley Oct 28 '20 at 05:04
  • https://stackoverflow.com/questions/44768319/tkinter-label-not-appearing https://stackoverflow.com/questions/24139166/tkinter-label-not-showing-int-variable refer these if you dont get expected ouput please comment – Dourave Oct 28 '20 at 06:10

1 Answers1

2

You need to set the label textvariable=self.label_text instead of text=self.label_text

import tkinter as tk


class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Hello Tkinter')
        self.label_text = tk.StringVar()
        self.label_text.set("Choose One")

        label = tk.Label(self, textvariable=self.label_text)
        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.set("Hello World!")

    def say_goodbye(self):
        self.label_text.set("Goodbye! \n (Closing in 2 seconds)")
        self.after(2000, self.destroy)


if __name__ == "__main__":
    window = Window()
    window.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80