0

我试图在 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()
4

1 回答 1

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):
于 2013-09-11T18:04:06.213 回答