Tkinter无法识别我的变量

时间:2013-09-11 18:00:06

标签: python tkinter

我正在尝试在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()

1 个答案:

答案 0 :(得分:2)

我想这一行会返回str,例如'7'

enter = self.Input.get()

此行返回int,例如7

a = random.randrange(1,10)

请注意7 != '7'

您可以尝试更改此行:

enter = int(self.Input.get())

或类似这样的行:

if enter == str(a):