我最近学习了一些python。我正试图在一个框架小部件上制作一个数字猜谜游戏。按下"随机数"按钮可生成一个随机数。然后,Enter按钮从输入框中获取输入,并与生成的随机数进行比较。为了测试它,程序会有一个标签,显示生成的随机数。当我输入与生成的数字相同的数字时,它表示不相等。只有当我输入0(零(时,它才会说它是相等的。我还有一些行告诉我值的类型,只是为了确保我比较的是相同的类型。
from random import randint
from tkinter import *
root = Tk()
root.title('Guess Number Final')
root.geometry('500x500')
directionFrame = Frame(root, width = 200, height = 200, highlightbackground = 'red',
highlightthickness = 3)
directionFrame.grid(row = 0, column = 0, padx = 20, pady = 20, ipadx = 20, ipady = 20)
randomNum = IntVar()
newguessInt = IntVar()
def randomize():
randomNum = randint(0,10)
randomStr.set(randomNum)
def enterGuess():
newguessInt = int(number_input.get())
newRandom = randomNum.get()
if (newguessInt == newRandom):
guess_var.set('Your guess is right.')
else:
guess_var.set('Your guess is wrong.')
guess_str.set(type(newguessInt))
random_str.set(type(newRandom))
##### Random Button #####
randomBtn = Button(directionFrame, text = 'Random Number', command = randomize)
randomBtn.pack()
##### Random Variable #####
randomStr = StringVar()
randomStr.set('Number')
##### Guess Variable #####
guess_var = StringVar()
guess_var.set('Your guess is...')
##### typeGuessVariable #####
guess_str = StringVar()
guess_str.set('Type')
##### randomNumVariable #####
random_str = StringVar()
random_str.set('Type')
##### Random Label #####
randomLbl = Label(directionFrame, textvariable = randomStr)
randomLbl.pack()
##### Number Entry Box #####
number_input = Entry(directionFrame)
number_input.pack()
##### Enter Button #####
enterBtn = Button(directionFrame, text = 'Enter Guess', command = enterGuess)
enterBtn.pack()
##### Your Guess Label #####
yourGuessLbl = Label(directionFrame, textvariable = guess_var)
yourGuessLbl.pack()
##### GuessInt Label ######
guessIntLbl = Label(directionFrame, textvariable = guess_str)
guessIntLbl.pack()
##### Random Label ######
randomNumLbl = Label(directionFrame, textvariable = random_str)
randomNumLbl.pack()
mainloop()
您已经声明了许多tkinter变量,这些变量使您混淆了使用错误的变量进行比较。
实际上,只使用两个tkinter变量就足够了:
randomNum
用于随机生成数- 用户猜测数的
guessNum
from random import randint
from tkinter import *
root = Tk()
root.title('Guess Number Final')
root.geometry('500x500')
directionFrame = Frame(root, width=200, height=200, highlightbackground='red', highlightthickness=3)
directionFrame.grid(row=0, column=0, padx=20, pady=20, ipadx=20, ipady=20)
randomNum = StringVar(value='Number')
guessNum = StringVar(value='')
def randomize():
randomNum.set(randint(0,10))
def enterGuess():
ok = randomNum.get() == guessNum.get().strip()
resultLabel['text'] = f'Your guess is {"right" if ok else "wrong"}.'
##### Random Button #####
randomBtn = Button(directionFrame, text='Random Number', command=randomize)
randomBtn.pack()
##### Random Label #####
randomLbl = Label(directionFrame, textvariable=randomNum)
randomLbl.pack()
##### Number Entry Box #####
number_input = Entry(directionFrame, textvariable=guessNum)
number_input.pack()
##### Enter Button #####
enterBtn = Button(directionFrame, text='Enter Guess', command=enterGuess)
enterBtn.pack()
##### Result Label #####
resultLabel = Label(directionFrame, text='Your guess is ...')
resultLabel.pack()
mainloop()
此行:
randomNum = randint(0,10)
创建一个新的局部变量randomNum
并将其分配给一个常规Pythonint
值。外部范围中的原始randomNum
仍然是值为零的默认IntVar
。我想你想做的是:
randomNum.set(randint(0, 10))
即,将您创建的IntVar
设置为指向新的随机内部