Python 单选按钮可以';t选择和取消选择
我的radiobutton无法选择和取消选择。我只想让其中一个可以选择。而且它无法很好地链接到下一个界面。这些是我的代码。请查看radiobutton部分。我提供了所有代码,以便你们可以看到发生了什么Python 单选按钮可以';t选择和取消选择,python,python-2.7,user-interface,tkinter,radio-button,Python,Python 2.7,User Interface,Tkinter,Radio Button,我的radiobutton无法选择和取消选择。我只想让其中一个可以选择。而且它无法很好地链接到下一个界面。这些是我的代码。请查看radiobutton部分。我提供了所有代码,以便你们可以看到发生了什么 import Tkinter import tkMessageBox WindowBox = Tkinter.Tk() WindowBox.geometry("250x200") WindowBox.title("Welcome to E-UPSR") level1="easy" level2
import Tkinter
import tkMessageBox
WindowBox = Tkinter.Tk()
WindowBox.geometry("250x200")
WindowBox.title("Welcome to E-UPSR")
level1="easy"
level2="moderate"
level3="hard"
Tkinter.Label (WindowBox, text="Username:").pack()
username1 = Tkinter.Entry (WindowBox)
username1.pack()
Tkinter.Label (WindowBox, text="Password:").pack()
password1 = Tkinter.Entry (WindowBox)
password1.pack()
student=[]
def read():
if not username1.get() or not password1.get():
tkMessageBox.showerror('Invalid', 'Empty username or password')
else:
addstudent = open ("student.txt", "r")
lines = addstudent.readlines()
addstudent.close ()
i = 0
while i < len(lines) - 1:
# username and password are saved in two line, label and value are separated by ':'.
# to get them we need to reed two line in each iteration and split with ':' to get the value (second result of spliting) then strip to remove end line.
user = lines[i].split(':')[1].strip()
password = lines[i+1].split(':')[1].strip()
# test if the user is registred
if user == username1.get() and password == password1.get():
WindowBox.withdraw()
MenuBox.deiconify()
break
i += 2
return
def register():
WindowBox.withdraw()
RegBox.deiconify()
return
RegBox = Tkinter.Tk()
RegBox.geometry("250x200")
RegBox.title("register")
Tkinter.Label (RegBox, text="Username:").pack()
username2 = Tkinter.Entry (RegBox)
username2.pack()
Tkinter.Label (RegBox, text="Password:").pack()
password2 = Tkinter.Entry (RegBox)
password2.pack()
RegBox.withdraw()
def back():
RegBox.withdraw()
WindowBox.deiconify()
return
def save():
if not username2.get() or not password2.get():
tkMessageBox.showerror('Invalid', 'Empty username or password')
else:
addstudent = open ("student.txt", "a")
addstudent.write('Username:' + username2.get() + '\n')
addstudent.write('Password:' + password2.get()+'\n')
tkMessageBox.showinfo("Writing", "Done")
return
MenuBox = Tkinter.Tk()
MenuBox.geometry("250x200")
MenuBox.title("MainMenu")
LabelName= Tkinter.Label(MenuBox, text="Choose Subject").pack()
MenuBox.withdraw()
MathBox = Tkinter.Tk()
MathBox.geometry("250x200")
MathBox.title("Math")
MathBox.withdraw()
BmBox = Tkinter.Tk()
BmBox.geometry("250x200")
BmBox.title("Bm")
BmBox.withdraw()
SnBox = Tkinter.Tk()
SnBox.geometry("250x200")
SnBox.title("Science")
SnBox.withdraw()
EasyBox1 = Tkinter.Tk()
EasyBox1.geometry("250x200")
EasyBox1.title("Eng")
EasyBox1.withdraw()
EngBox = Tkinter.Tk()
EngBox.geometry("250x200")
EngBox.title("Eng")
EngBox.withdraw()
EasyBox1 = Tkinter.Tk()
EasyBox1.geometry("250x200")
EasyBox1.title("Math Easy")
EasyBox1.withdraw()
ModerateBox1 = Tkinter.Tk()
ModerateBox1.geometry("250x200")
ModerateBox1.title("Math Moderate")
ModerateBox1.withdraw()
HardBox1 = Tkinter.Tk()
HardBox1.geometry("250x200")
HardBox1.title("Math Hard")
HardBox1.withdraw()
def Math():
MenuBox.withdraw()
MathBox.deiconify()
return
def Bm():
MenuBox.withdraw()
MathBox.deiconify()
return
def Sn():
MenuBox.withdraw()
SnBox.deiconify()
return
def Eng():
MenuBox.withdraw()
EngBox.deiconify()
return
def back1():
MathBox.withdraw()
EngBox.withdraw()
SnBox.withdraw()
BmBox.withdraw()
MenuBox.deiconify()
return
def callback():
radSel=radVar.get()
if radSel == 0:win.configure(MathBox.withdraw,EasyBox1.deiconify)
elif radSel == 0:win.configure(MathBox.withdraw,ModerateBox1.deiconify)
elif radSel == 0:win.configure(MathBox.withdraw,HardBox1.deiconify)
radVar = Tkinter.IntVar()
rad1 = Tkinter.Radiobutton(MathBox,text=level1,variable=radVar,value=1)
rad1.grid(column=5,row=5)
radVar = Tkinter.IntVar()
rad1 = Tkinter.Radiobutton(MathBox,text=level2,variable=radVar,value=1)
rad1.grid(column=5,row=6)
radVar = Tkinter.IntVar()
rad1 = Tkinter.Radiobutton(MathBox,text=level3,variable=radVar,value=1)
rad1.grid(column=5,row=7)
Tkinter.Button (RegBox, text="Back", command=back).pack()
Tkinter.Button (RegBox, text="Enter", command=save).pack()
Tkinter.Button (WindowBox, text="Register", command=register).pack()
Tkinter.Button (WindowBox, text="Proceed", command=read).pack()
Tkinter.Button (MenuBox, text="Math",command=Math,width=15).place(relx=.0,rely=.2)
Tkinter.Button (MenuBox, text="Bm",command=Bm,width=15).place(relx=.0,rely=.4)
Tkinter.Button (MenuBox, text="Science",command=Sn,width=15).place(relx=.5, rely=.2)
Tkinter.Button (MenuBox, text="Eng",command=Eng,width=15).place(relx=.5,rely=.4)
Tkinter.Button (MathBox, text="Back", command=back1).place(relx=.4,rely=.7)
Tkinter.Button (EngBox, text="Back", command=back1).place(relx=.4,rely=.7)
Tkinter.Button (BmBox, text="Back", command=back1).place(relx=.4,rely=.7)
Tkinter.Button (SnBox, text="Back", command=back1).place(relx=.4,rely=.7)
Tkinter.Button (MathBox, text="Start", command=callback).place(relx=.8,rely=.7)
WindowBox.mainloop()
导入Tkinter
导入tkMessageBox
WindowBox=Tkinter.Tk()
WindowBox.几何图形(“250x200”)
WindowBox.title(“欢迎使用E-UPSR”)
level1=“简单”
level2=“中等”
level3=“硬”
Tkinter.Label(WindowBox,text=“Username:”).pack()
username1=Tkinter.Entry(WindowBox)
username1.pack()
Tkinter.Label(WindowBox,text=“Password:”).pack()
password1=Tkinter.Entry(WindowBox)
password1.pack()
学生=[]
def read():
如果不是username1.get()或不是password1.get():
tkMessageBox.淋浴错误('无效'、'空用户名或密码')
其他:
addstudent=open(“student.txt”、“r”)
lines=addstudent.readlines()
addstudent.close()
i=0
而i
单选按钮设计为不可选择和取消选择。你要找的是复选框。()
PS:您可能应该阅读另一篇python Tk教程。多次使用Tkinter.Tk()不是这种工作方式。对于初学者,您的程序必须只创建一个
Tk
的实例。您需要将对Tk()。你有