1

I've been attempting to make a simple GUI, and have been working my way through tkinter's various functions. However, I can't for the life of me figure out why this doesn't work.

from tkinter import Tk, Label, Button, Radiobutton,IntVar,StringVar

class TestGUI:
    def __init__(self, master):
        self.master = master
        master.title("Test GUI")

        self.mode = IntVar()

        self.modetext = StringVar()
        self.modetext.set("What does this do?")

        self.modelabel = Label(master,textvariable=self.modetext)
        self.modelabel.pack()

        self.close_button = Button(master, text="Close", command=master.destroy)
        self.close_button.pack()

        R1 = Radiobutton(master, text="Mode 1", variable=self.mode, value=0, command=self.modeset)
        R1.pack()

        R2 = Radiobutton(master, text="Mode 2", variable=self.mode, value=1, command=self.modeset)
        R2.pack()

        R3 = Radiobutton(master, text="Mode 3", variable=self.mode, value=2, command=self.modeset)
        R3.pack()

    def modeset(self):
        self.modetext.set("Mode is " + str(self.mode.get()))
        print(self.mode.get())

root = Tk()
T_GUI = TestGUI(root)
root.mainloop()

What it should do, as far as I can tell, is display three radio buttons which set the value of mode, and display "Mode is [mode]" in the label and print the value of mode when one is selected.

Instead, the label is never displayed and choosing a radio button doesn't change the value of mode.

Can anyone give me a clue on what is happening?

Lafexlos
  • 7,618
  • 5
  • 38
  • 53
eyeballfrog
  • 225
  • 1
  • 6
  • Works perfect: https://i.imgur.com/sQo9YzL.png (macOS Sierra 10.12.3, Python 3.6) – falsetru Feb 20 '17 at 08:57
  • Works perfect on Win7, Python 3.5.2 as well. What is your operating system and python version? – Lafexlos Feb 20 '17 at 09:12
  • Seems like your Variable Classes(IntVar, StringVar etc.) are causing problems. Try specifying parents on them (just a thought, no idea if it'll work or not). I mean, `self.mode = IntVar(master=self.master)`,`self.modetext = StringVar(master=self.master)` – Lafexlos Feb 20 '17 at 09:17
  • @Lafexlos Windows 10, Python 3.5.2. Using Pyzo, if that makes a difference. – eyeballfrog Feb 20 '17 at 09:17
  • @Lafexlos Huh, that seems to have fixed it. Neat. – eyeballfrog Feb 20 '17 at 09:21
  • Weird. Glad it is fixed though. Edited a bit for future readers to increase their probability to find this. If edit caused any harm, feel free to rollback. – Lafexlos Feb 20 '17 at 09:32

1 Answers1

0

Looking at your explanation, StringVar and IntVar are causing problems here.

Specifying master on them should solve your issue.

self.mode = IntVar(master=self.master)
self.modetext = StringVar(master=self.master)

Something to do with Pyzo probably because on most IDEs, omitting master don't cause any problems.

Lafexlos
  • 7,618
  • 5
  • 38
  • 53