单选按钮无法选择和取消选择

时间:2016-02-14 20:16:41

标签: 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="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()

1 个答案:

答案 0 :(得分:-1)

无法选择和取消选择Radiobuttons。你要找的是复选框。 (see the difference

PS:您可能应该阅读另一个python Tk教程。多次使用Tkinter.Tk()并不是它的工作方式。