【问题标题】:Wrong structure of calling global variable调用全局变量的结构错误
【发布时间】:2016-02-18 07:36:13
【问题描述】:

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

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

【问题讨论】:

  • @WoLy 请帮我最后一次
  • 您不应创建多个根窗口。 Tkinter 被设计为始终只有一个 Tk 实例。如果您需要额外的窗口,您应该创建 Toplevel 的实例。

标签: python python-2.7 tkinter coding-style


【解决方案1】:

有多个错误使用户无法获得满分。错误 #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()

错误 #2 在 next3() 中,您会犯与上面的 next1() 相同的错误,但也会通过与 answr3.get 而不是 answr3.get() 进行比较来复合它

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

解决这些问题,您的用户和您将有望获得满分。

【讨论】:

  • 还是有bug。如果没有回答,我们点击下一步按钮。然后tkmassagebox会弹出。如果我们关闭它,下一个界面继续,那就是问题
  • @student,我建议您关闭此问题并发布一个新问题,其中包含您遇到问题的特定代码以及您希望它做什么而不是做什么。您可以包含返回此问题的链接,以获取有关您的问题的更多背景信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2021-10-13
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多