简体   繁体   中英

Troubles with Variable Classes (StringVar, IntVar) using Pyzo IDE

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM