我有这个代码,它要求用户选择其中一个选项。我用单选按钮。在用户选择了他的选择之后,该选择将使用另一个if语句。我已经将choice的变量赋值为variable = specialistchoose
。但是当我使用specialistchoose
或specialistchoose.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()
注意:这只是一个较长代码的示例。在
{{{cd2}的值与^}无关。您将变量
specialistchoose
声明为IntVar
,因此它将是一些intif specialistchoose.get() == "Cardiology":
永远不会是{昨天我在这里回答了你关于同一主题的问题:Gray out a frame after clicking a radiobutton Tkinter
如果您将
func
改为如下所示:您将能够看到每个
radiobutton
按下时得到的值。在从那里你可以提出一个条件,如果他们被按下或没有。在
因为您只是在它们创建之后才
get()
输入它们的值,所以您将只得到它们的初始值,而没有其他值。在尝试使用
command
或其他按钮获取它们的值。在不知道你在下
command_rX
下有什么,但是你应该把那些if分开,放在各自的^{另外,由于变量是
IntVar()
,因此您将得到value
,它将介于1和5之间(包括1和5),因为您是这样分配的。在或者创建一个按钮,当它被点击时,它将获得值并执行所有这些操作。在
^{pr2}$另一个小问题是,在使用文件时,最好使用
with
语句,因为当您移出范围时,它会自动处理关闭操作,并且您不必担心每次都会关闭。在每次您更改值,那个txt文件将从零开始创建,因为您在
w
模式下打开它。我不知道这是不是你想要的,但我想给你一个提示。在相关问题 更多 >
编程相关推荐