如果用户输入特定号码,我尝试运行Tkinter窗口,但它无效。这是我的代码:
#Import Tkinter module
from tkinter import *
ans = int (input('What is 100 x 10? '))
if ans == int ('1000'):
#Create a window and set Geometry
pyr = Tk()
pyr.geometry('300x300')
#Title window
pyr.title("Right")
#Set background color using hex code
pyr.configure(background = "#32cd32")
#Create Labels
lbl = Label(pyr, text="Correct!", bg="#32cd32")
# Packing
lbl.grid(column=2, row=1)
#Draw window and start application
pyr.mainloop
else:
#Create a window and set Geometry
pyw = Tk()
pyw.geometry('300x300')
#Title window
pyw.title("Wrong")
#Set background color using hex code
pyw.configure(background = "#ff0000")
#Create Labels
lbl1 = Label(pyw, text="Nope!", bg="#32cd32")
# Packing
lbl1.grid(column=2, row=1)
#Draw window and start application
pyw.mainloop()
我输入的是1000或不同的数字,没有任何反应。它只是坐在那里,甚至不打印">>>"表明它已经完成了。我究竟做错了什么?感谢
答案 0 :(得分:1)
您的代码的两个主要问题基本上是两个拼写错误:
mainloop
函数(添加()
)pyf
代替pyr
和pyw
我还建议将窗口打开的东西移动到减少代码重复的方法中,并在尝试强制转换为isdigit
之前使用int
检查输入是否为数字。
def open_window(title, message, color):
pyr = Tk()
pyr.geometry('300x300')
pyr.title(title)
pyr.configure(background = color)
lbl = Label(pyr, text=message, bg=color)
lbl.grid(column=2, row=1)
pyr.mainloop()
ans = input('What is 100 x 10? ')
if ans.isdigit() and int(ans) == 1000:
open_window("Right", "Correct", "#32cd32")
else:
open_window("Wrong", "Nope", "#ff0000")