调用全局变量的结构错误

时间:2016-02-18 07:36:14

标签: python python-2.7 tkinter coding-style

我猜结构代码是错误的。因为tkmassagebox信息总是显示C.据说当总数= 3时,tkmassagebox显示A.虽然总得到3正确但结果仍然是相同的C.Can有人修复我的问题。我的代码是你们可以自己尝试的。

import Tkinter 
import tkMessageBox

#easybox1
EasyBox1 = Tkinter.Tk()
EasyBox1.geometry("250x200")
EasyBox1.title("Quesion 1")

Tkinter.Label (EasyBox1, text="answer:").pack()

answr1 = Tkinter.Entry (EasyBox1)
answr1.pack()

LabelName2 = Tkinter.Label (EasyBox1, text="State the number of edges in a cube")
LabelName2.grid(row=1,column=0)
LabelName2.pack()

total=0
def next1():
    global total
    if not answr1.get():
        tkMessageBox.showerror('no answer')
    elif answr1.get() == 8 :
        total=total+1
        EasyBox1.withdraw()
        EasyBox2.deiconify()
    else:
        total=total
        EasyBox1.withdraw()
        EasyBox2.deiconify()
    return

#easybox2
EasyBox2 = Tkinter.Tk()
EasyBox2.geometry("250x200")
EasyBox2.title("Quesion 2")

Tkinter.Label (EasyBox2, text="answer:").pack()

answr2 = Tkinter.Entry (EasyBox2)
answr2.pack()

LabelName2 = Tkinter.Label (EasyBox2, text="What is the place value of the digit 4 in 76421?")
LabelName2.grid(row=1,column=0)
LabelName2.pack()

LabelName2 = Tkinter.Label (EasyBox2, text="A.Thousands B.Hundreds C.Ones D.Tens")
LabelName2.grid(row=1,column=0)
LabelName2.pack()

def next2():
    global total
    if not answr2.get():
        tkMessageBox.showerror('no answer')
    elif answr2.get() in ["B", "b"]:
        total=total+1
        EasyBox2.withdraw()
        EasyBox3.deiconify()
    else:
        total=total
        EasyBox2.withdraw()
        EasyBox3.deiconify()


EasyBox2.withdraw()


#easybox3
EasyBox3 = Tkinter.Tk()
EasyBox3.geometry("250x200")
EasyBox3.title("Quesion 2")

Tkinter.Label (EasyBox3, text="answer:").pack()

answr3 = Tkinter.Entry (EasyBox3)
answr3.pack()

LabelName2 = Tkinter.Label (EasyBox3, text="1234+143x23=?")
LabelName2.grid(row=1,column=0)
LabelName2.pack()

def next3():
    global total
    if not answr3.get():
        tkMessageBox.showerror('no answer')
    elif answr3.get == 4523:
        total=total+1
        EasyBox3.withdraw()
        ResultBox.deiconify()
    else:
        total=total
        EasyBox3.withdraw()
        ResultBox.deiconify()

EasyBox3.withdraw()



ResultBox = Tkinter.Tk()
ResultBox.geometry("320x260")
ResultBox.title("Results")

LabelName5 = Tkinter.Label(ResultBox, text = "Mark"+`total`+'\n')
LabelName5.pack

def mark():
    global total_mark
    total_mark = total
    if total_mark == 3:
        tkMessageBox.showinfo('A')
    elif total_mark == 2:
        tkMessageBox.showinfo('B')
    else:
        tkMessageBox.showinfo('C')
ResultBox.withdraw()

Tkinter.Button (EasyBox1, text="Next", command=next1).pack()
Tkinter.Button (EasyBox2, text="Next", command=next2).pack()
Tkinter.Button (EasyBox3, text="Next", command=next3).pack()
Tkinter.Button (ResultBox, text="Result", command=mark).pack()


EasyBox1.mainloop()

1 个答案:

答案 0 :(得分:0)

有多个错误导致用户无法获得满分。错误#1在next1()中:您将答案与8进行比较,但它会以字符串形式返回,因此您应将其与“' 8'或者在其上调用int():

def next1():
    global total
    if not answr1.get():
        tkMessageBox.showerror('no answer')
    elif answr1.get() == '8':
        total = total + 1
    EasyBox1.withdraw()
    EasyBox2.deiconify()

Bug#2在next3()中,你在上面的next1()中犯了同样的错误,但也通过比较answersr3.get而不是answersr3.get()

来复合它
def next3():
    global total
    if not answr3.get():
        tkMessageBox.showerror('no answer')
    elif answr3.get() == '4523':
        total = total + 1
    EasyBox3.withdraw()
    ResultBox.deiconify()

修复那些,您的用户和您,将获得完美的分数。