Tkinter 单选按钮未正确选择

Posted

技术标签:

【中文标题】Tkinter 单选按钮未正确选择【英文标题】:Tkinter radiobuttons not selecting properly 【发布时间】:2022-01-17 20:14:24 【问题描述】:

我正在尝试制作一个小型 tkinter 程序来计算形状和体积的面积 我制作了一个主 py 文件“area_volume_calculator.py”和两个库“area.py”和“volume.py”

主程序的单选按钮工作正常,但是当我从Area_calculator 创建第二个窗口时,我似乎无法让单选按钮正常工作。当它打开时,最后 2 个单选按钮已被选中。

我仍然可以选择一个选项,但 self.choice.get() 总是返回默认值 -1,我不知道如何让它工作。

谁能帮我让单选按钮正常工作? 任何和所有的帮助表示赞赏

主程序:

from tkinter import *

from area import *

# from volume import *

class Menu:
    def __init__(self):
        self.window = Tk()
        self.window.title("Areas and Volumes")
    
    
        # self.name_label = Label(self.window, text = "Name: ")
        # self.name_label.grid(row=0,column=0)
    
        # self.name_entry = Entry(self.window)
        # self.name_entry.grid(row=0,column=1)
    
        # self.age_label = Label(self.window, text = "Age: ")
        # self.age_label.grid(row=1,column=0)
    
        # self.age_entry = Entry(self.window)
        # self.age_entry.grid(row=1,column=1)
    
        self.choice = IntVar()
    
        self.area_radiobutton = Radiobutton(self.window, text = "Area Calculator", var = self.choice, value = 0)
        self.area_radiobutton.grid(row=2,column=0)
    
        self.volume_radiobutton = Radiobutton(self.window, text = "Volume Calculator", var = self.choice, value = 1)
        self.volume_radiobutton.grid(row=3,column=0)
    
        self.choice.set(-1)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=4,column=0)
    
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())

        if self.choice.get() == 0:
            area_calculator = Area_calculator()
    
        # elif self.choice.get() == 1:
        #     volume_calculator = Volume_calculator()


menu = Menu()

area.py 库:

from tkinter import *
class Area_calculator():
    def __init__(self):
        self.window = Tk()
        self.window.title
        self.window.title("Area Calculator")
    
        self.choice_label = Label(self.window, text = "Choose a shape")
        self.choice_label.grid(row=0,columnspan=1)
    
        self.choice = IntVar()
    
        self.rectangle_radiobutton = Radiobutton(self.window, text = "Rectangle", var = self.choice, value = 0)
        self.rectangle_radiobutton.grid(row=1,column=0)
    
        self.triangle_radiobutton = Radiobutton(self.window, text = "Triangle", var = self.choice, value = 1)
        self.triangle_radiobutton.grid(row=2,column=0)
    
        self.circle_radiobutton = Radiobutton(self.window, text = "Circle", var = self.choice, value = 2)
        self.circle_radiobutton.grid(row=3,column=0)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=4,column=0)
    
        self.choice.set(-1)
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())
#        if self.choice.get() == 0:
#            rectangle_calculator = Rectangle_calculator()
#        elif self.choice.get() == 1:
#            triangle_calculator = Triangle_calculator()
#        elif self.choice.get() == 2:
#            circle_calculator = Circle_calculator()

【问题讨论】:

【参考方案1】:

您正在创建tk.Tk() 的两个实例(每个类中一个),这绝不是一个好主意。 tk.Tk() 不仅创建了一个新窗口,而且还创建了一个新的嵌入式 Tcl 解释器,这可能会搞砸事情,尤其是 IntVars 和其他变量,例如 self.choice。如需更多信息,请参阅here。

如果您想拥有多个窗口,请改用tk.Toplevel()

在cmets中回答你的第二个问题,你必须首先将参数nameage添加到Area_calculator类的__init__中,并且在@中创建它时也将它们传递给类987654331@.

完成的代码:

主程序:

from tkinter import *

from area import *


class Menu:
    def __init__(self):
        self.window = Tk()
        self.window.title("Areas and Volumes")
    
        self.name_label = Label(self.window, text = "Name: ")
        self.name_label.grid(row=0,column=0)
    
        self.name_entry = Entry(self.window)
        self.name_entry.grid(row=0,column=1)
    
        self.age_label = Label(self.window, text = "Age: ")
        self.age_label.grid(row=1,column=0)
    
        self.age_entry = Entry(self.window)
        self.age_entry.grid(row=1,column=1)
    
        self.choice = IntVar()
    
        self.area_radiobutton = Radiobutton(self.window, text = "Area Calculator", var = self.choice, value = 0)
        self.area_radiobutton.grid(row=2,column=0)
    
        self.volume_radiobutton = Radiobutton(self.window, text = "Volume Calculator", var = self.choice, value = 1)
        self.volume_radiobutton.grid(row=3,column=0)
    
        self.choice.set(-1)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=4,column=0)
    
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())

        if self.choice.get() == 0:
            name = self.name_entry.get()
            age = self.age_entry.get()
            area_calculator = Area_calculator(name, age)


menu = Menu()

area.py 库:

from tkinter import *


class Area_calculator():
    def __init__(self, name, age):
        self.window = Toplevel()
        self.window.title
        self.window.title("Area Calculator")

        self.name_label = Label(self.window, text = f"name=", width=10)
        self.name_label.grid()

        self.age_label = Label(self.window, text = f"age=", width=10)
        self.age_label.grid(row=0, column=1)
    
        self.choice_label = Label(self.window, text = "Choose a shape")
        self.choice_label.grid(row=1,columnspan=1)
    
        self.choice = IntVar()
    
        self.rectangle_radiobutton = Radiobutton(self.window, text = "Rectangle", var = self.choice, value = 0)
        self.rectangle_radiobutton.grid(row=2,column=0)
    
        self.triangle_radiobutton = Radiobutton(self.window, text = "Triangle", var = self.choice, value = 1)
        self.triangle_radiobutton.grid(row=3,column=0)
    
        self.circle_radiobutton = Radiobutton(self.window, text = "Circle", var = self.choice, value = 2)
        self.circle_radiobutton.grid(row=4,column=0)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=5,column=0)
    
        self.choice.set(-1)
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())

【讨论】:

非常感谢!这解决了它,如果您不介意,我还有另一个问题:在注释代码中,我得到了 2 个变量名称和年龄,我如何将这些变量传递给我可以将它们显示到第二个窗口?是否像拥有'class Area_calculator(name,age)'并从第一个窗口执行函数一样简单? 还是写 area_calculator = Area_calculator(name, age)?

以上是关于Tkinter 单选按钮未正确选择的主要内容,如果未能解决你的问题,请参考以下文章

Tkinter:所有单选按钮都已被选中

tkinter 单选按钮未在类中显示值

Tkinter - 单击默认情况下未设置的按钮后无法获取单选按钮值

Python Tkinter 中的单选按钮值

如何在 tkinter 中只选择一个单选按钮

基于选定的单选按钮在 Tkinter 中启动命令?