Python tkinter单选按钮在类中不显示值

Python tkinter单选按钮在类中不显示值,python,oop,user-interface,tkinter,inheritance,Python,Oop,User Interface,Tkinter,Inheritance,我目前是一名学习python的大学生,在使用tkinter时遇到了一些Radiobutton问题。当使用多个类时,我的单选按钮不会更新它们的值,并且总是打印出.set()方法的值。这是一段代码,与我实验室中的代码类似,但这只是问题所在 import matplotlib matplotlib.use("TkAgg") import tkinter as tk from matplotlib.backends.backend_tkagg import FigureCanvasT

我目前是一名学习python的大学生,在使用tkinter时遇到了一些
Radiobutton
问题。当使用多个类时,我的单选按钮不会更新它们的值,并且总是打印出
.set()
方法的值。这是一段代码,与我实验室中的代码类似,但这只是问题所在

import matplotlib
matplotlib.use("TkAgg")
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
from matplotlib.figure import Figure


class test(tk.Tk):
    def __init__(self):
        super().__init__()
        self.TOPNUM=15


class dialogWindow(test):
    def __init__(self):
        super().__init__()

        self.rc=tk.IntVar()
        self.rc.set(20)


        self.radio1=tk.Radiobutton(self,text="1",value=1,variable=self.rc,command=self.rc.get())
        self.radio2=tk.Radiobutton(self,text="2",value=2,variable=self.rc,command=self.rc.get())

        self.button=tk.Button(self,text="CLick Me!",command=lambda : self.clicked(self.rc.get()))

        self.radio1.pack()
        self.radio2.pack()
        self.button.pack()
        self.rc.set(20)

    def clicked(self,value):
        print(value)


def run():
    win=test()
    win.title("Top Colleges")
    win.minsize(200,200)
    header=tk.Label(win,text="College Lookup",fg="blue",font=15)
    header.pack()

    dataButton=tk.Button(win,text="By Data",width=8, command= lambda : dialogWindow())

    dataButton.pack()

    win.mainloop()

run()
然而,当我尝试在一个单独的类中编写它时,它确实起了作用,我不知道为什么

class dialogWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.rc=tk.IntVar()
        self.rc.set(20)

        self.radio1=tk.Radiobutton(self,text="1",value="1",variable=self.rc,command=self.rc.get())
        self.radio2=tk.Radiobutton(self,text="2",value="2",variable=self.rc,command=self.rc.get())

        self.button=tk.Button(self,text="CLick Me!",command=lambda : self.clicked(self.rc.get()))

        self.radio1.pack()
        self.radio2.pack()
        self.button.pack()
        self.rc.set(1)

    def clicked(self,value):
        print(value)


def run():
    win=dialogWindow()
    win.minsize(200,200)
    dataButton=tk.Button(win,text="By Data",width=8, command= lambda:dialogWindow())

    dataButton.grid(row=1,column=2,padx=5)

    win.mainloop()
run()

解释很简单,因为继承而创建了两个
Tk()
实例。现在您必须指定
IntVar()
应该属于哪个实例,因为它总是将自己设置为第一个创建的实例(
win
),但在这里您不希望这样。因此:

class dialogWindow(test):
    def __init__(self):
        super().__init__()
        
        self.rc=tk.IntVar(self)
        .....

哇,这么简单的解决方案。谢谢你的解释!