我试图在 Tkinter 中创建一个计数器,但由于某种原因 python 无法识别我的变量并从我的 else 语句中打印出来。
from Tkinter import *
import random
a = random.randrange(1,10)
b = random.randrange(1,10)
c = random.randrange(1,10)
d = random.randrange(1,10)
print a, b, c, d
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.Input = Entry(frame)
self.Input.pack()
self.display = Label(frame, text='')
self.display.pack(side=BOTTOM)
self.confirm = Button(frame, text='confirm', command=self.confirm)
self.confirm.pack(side=BOTTOM)
def confirm(self):
enter = self.Input.get()
abc = 0
if enter == a:
abc += 1
self.display['text'] = abc
print('a')
elif enter == b:
abc += 2
self.display['text'] = abc
print('b')
elif enter == c:
abc += 3
self.display['text'] = abc
print('c')
elif enter == d:
abc += 4
self.display['text'] = abc
print('d')
else:
print('invalid character')
root = Tk()
app = App(root)
root.mainloop()