简体   繁体   English

Tkinter 单选按钮未正确选择

[英]Tkinter radiobuttons not selecting properly

im trying to make a small tkinter program calculate area of shapes and volumes and ive made a main py file "area_volume_calculator.py" and two libraries "area.py" and "volume.py"我试图制作一个小型 tkinter 程序计算形状和体积的面积,我制作了一个主 py 文件“area_volume_calculator.py”和两个库“area.py”和“volume.py”

the main program's radio buttons work fine but when i create the second window from Area_calculator i cant seem to get radio buttons to work properly.主程序的单选按钮工作正常,但是当我从 Area_calculator 创建第二个 window 时,我似乎无法让单选按钮正常工作。 when it opens the last 2 radio buttons are selected already.当它打开时,最后 2 个单选按钮已被选中。

i can still select an option but self.choice.get() always returns the default value which is -1, idk how to get it to work.我仍然可以选择 select 但 self.choice.get() 总是返回默认值 -1,不知道如何让它工作。

can anyone help me get the radio buttons to work properly?谁能帮我让单选按钮正常工作? any and all help is appreciated任何和所有的帮助表示赞赏

Main Program:主程序:

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 library: 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()

You are creating two instances of tk.Tk() (one in each class) which is never a good idea.您正在创建tk.Tk()的两个实例(每个类中一个),这绝不是一个好主意。 tk.Tk() does not just create a new window but also a new embedded Tcl interpreter and this can mess things up, particularly IntVar s and other variables such as self.choice . tk.Tk()不仅创建了一个新的 window ,而且还创建了一个新的嵌入式 Tcl 解释器,这可能会搞砸事情,尤其是IntVar和其他变量,例如self.choice For more information see here .有关更多信息,请参见此处

If you want to have multiple windows then use tk.Toplevel() instead.如果你想拥有多个 windows 然后使用tk.Toplevel()代替。

In answer to your second question in the comments, you must first add the parameters name and age to the __init__ of the Area_calculator class, and also pass them to the class when creating it in Menu .为了回答评论中的第二个问题,您必须首先将参数nameage添加到Area_calculator class 的__init__中,并在Menu中创建时将它们传递给 class 。

Completed code:完成代码:

Main program:主程序:

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 library: 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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