简体   繁体   English

将变量分配给单选按钮Tkinter

[英]Assign variable to radiobutton Tkinter

I have this code which asks user to select one of the choices. 我有此代码,要求用户选择选项之一。 I used radiobuttons. 我使用单选按钮。 After the user select his choice, the choice will be use another if statement. 用户选择了自己的选择后,该选择将使用另一个if语句。 I already assigned the choice's variable as variable = specialistchoose . 我已经将选择的变量分配为variable = specialistchoose But when I use the specialistchoose or specialistchoose.get() , it does not work. 但是,当我使用specialistchoosespecialistchoose.get() ,它不起作用。 Can anyone help? 有人可以帮忙吗?

                specialistchoose = IntVar()

                r1 = Radiobutton (f2, text = "Cardiology", variable = specialistchoose, value = 1, command = command_r1 )
                r1.grid(row = 4, column = 0, stick = W) 

                r2 = Radiobutton (f2, text = "Gastroenterology", variable = specialistchoose, value = 2, command =  command_r2)
                r2.grid(row = 4, column = 1,stick = W ) 

                r3 = Radiobutton (f2, text = "Dermatology", variable = specialistchoose, value = 3, command = command_r3)
                r3.grid (row = 4, column = 2,stick = W )

                r4 = Radiobutton (f2, text = "Psychiatry", variable = specialistchoose, value = 4, command = command_r4)
                r4.grid (row = 5, column = 0,stick = W )

                r5 = Radiobutton (f2, text = "Dentist", variable = specialistchoose, value = 5, command = command_r5)
                r5.grid(row = 5, column = 1,stick = W  )

                f2.place(relx = 0.01, rely = 0.125, anchor = NW)
                Label(f1, text = "Specialist").place(relx = .06, rely = 0.125, anchor = W)

                f1.grid(stick = W)

                if specialistchoose.get() == "Cardiology":
                    file = open ("test2.txt", "w")
                    file.write ("Specialist : Cardiology")
                    file.close()
                elif specialistchoose.get() == "Gastroenterology":
                    file = open ("test2.txt", "w")
                    file.write ("Specialist : Gastroenterology")
                    file.close()
                elif specialistchoose.get() == "Dermatology":
                    file = open ("test2.txt", "w")
                    file.write ("Specialist : Dermatology")
                    file.close()
                elif specialistchoose.get() == "Psychiatry":
                    file = open ("test2.txt", "w")
                    file.write("Specialist : Psychiatry")
                    file.close()
                elif specialistchoose.get() == "Dentist":
                    file = open ("test2.txt", "w")
                    file.write("Specialist : Dentist")
                    file.close()          

note : this is just the sample of a longer code. 注意:这只是更长代码的示例。

Since you are get() ting their values only right after they created, you'll get only their initial value and nothing more. 由于仅在它们创建后立即get()它们的值,所以您将仅获得它们的初始值,仅此而已。

Try to get their value using command , or another button. 尝试使用command或另一个按钮获取它们的值。

No idea what do you have under command_rX s but you should split those if's and put under respective command s. 不知道在command_rX下有什么,但是您应该将那些if分开并放在相应的command下。

Also, since your variables are IntVar() and you'll be getting value which will be between 1 and 5 inclusive since you assigned as such. 另外,由于您的变量是IntVar() ,因此您将获得的value在1到5之间(含1和5),因为您是这样分配的。

def command_r1():
    with open('test2.txt', 'w') as file:
        file.write ("Specialist : Cardiology")

def command_r2():
    with open('test2.txt', 'w') as file:
        file.write ("Specialist : Gastroenterology")
#etc...

or create a button, when that's clicked it'll get the value and does all those if-else things. 或创建一个按钮,单击该按钮将获得值并执行所有这些if-else事情。

def external_button_callback():
    radioValue = specialistchoose.get()
    if radioValue == 1:
        with open('test2.txt', 'w') as file:
            file.write ("Specialist : Cardiology")
    #etc...

btn = Button(f2, text= "Get Value" command = external_button_callback)
btn.grid(row=6, column=0)

Another little thing is, when using files, it is better to use with statement since it handles closing automatically when you move out of scope and you don't need to worry about closing everytime. 另一个小问题是,在使用文件时,最好与with语句一起使用with因为它会在您移出作用域时自动处理关闭,并且您不必每次都担心关闭。

And everytime you change value, that txt file will be created from scratch since you open it on w mode. 而且,每次更改值时,由于在w模式下打开该txt文件,它都会从头开始创建。 I don't know if that's what you wanted or not but wanted to give a heads up. 我不知道那是不是您想要的,但想要提示。

The value of radiobutton variable has nothing to do with its text . radiobutton变量的值与其text无关。 You declared the variable specialistchoose to be IntVar so it will be some int. 您将变量specialistchoose选择为IntVar因此它将是一些int。

if specialistchoose.get() == "Cardiology": will never be True if specialistchoose.get() == "Cardiology":永远不会为True

Yesterday i answered your question on a very same topic here: Gray out a frame after clicking a radiobutton Tkinter 昨天我在一个非常相同的主题上回答了您的问题: 单击单选按钮Tkinter后,将框架变灰

If you alter the func to something like this: 如果将func更改为以下形式:

def func():
    print specialistchoose.get()

You will be able to see what values each radiobutton gets when it is pressed. 您将能够看到每个radiobutton被按下时得到的值。

From there you can make a condition if they are pressed or not. 在此可以确定是否按下它们。

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

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