将变量赋给radiobutton Tkin

2024-06-16 11:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个代码,它要求用户选择其中一个选项。我用单选按钮。在用户选择了他的选择之后,该选择将使用另一个if语句。我已经将choice的变量赋值为variable = specialistchoose。但是当我使用specialistchoosespecialistchoose.get()时,它不起作用。有人能帮忙吗?在

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

注意:这只是一个较长代码的示例。在


Tags: textgetvaluecolumnopenvariablecommandgrid
2条回答

{{{cd2}的值与^}无关。您将变量specialistchoose声明为IntVar,因此它将是一些int

if specialistchoose.get() == "Cardiology":永远不会是{}

昨天我在这里回答了你关于同一主题的问题:Gray out a frame after clicking a radiobutton Tkinter

如果您将func改为如下所示:

def func():
    print specialistchoose.get()

您将能够看到每个radiobutton按下时得到的值。在

从那里你可以提出一个条件,如果他们被按下或没有。在

因为您只是在它们创建之后才get()输入它们的值,所以您将只得到它们的初始值,而没有其他值。在

尝试使用command或其他按钮获取它们的值。在

不知道你在command_rX下有什么,但是你应该把那些if分开,放在各自的^{

另外,由于变量是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...

或者创建一个按钮,当它被点击时,它将获得值并执行所有这些操作。在

^{pr2}$

另一个小问题是,在使用文件时,最好使用with语句,因为当您移出范围时,它会自动处理关闭操作,并且您不必担心每次都会关闭。在

每次您更改值,那个txt文件将从零开始创建,因为您在w模式下打开它。我不知道这是不是你想要的,但我想给你一个提示。在

相关问题 更多 >