按下buttonGuess
会运行numRandom
函数,它也会运行remainingAttemps
函数。问题是,如果用户按下buttonGues
,则会重新分配Attemps
值。
import tkinter
import random
window = tkinter.Tk()
window.geometry('600x500')
x = random.randint(1,10)
remainingTime = True
Attempts = 4
def countdown (time_left):
global remainingTime
if remainingTime == True:
lblCrono.configure(text = str(time_left))
if time_left > 0:
time_left = time_left - 1
window.after(1000, countdown, time_left)
else:
remainingTime = False
lblCrono.configure(text = 0)
return remainingTime, gameOver()
else:
return
def numRamdom():
global Attempts
numWritten = int(entryWriteNumber.get())
if numWritten > x:
lblClue.configure(text = 'Its a smaller number')
return remainingAttempts(Attempts)
if numWritten < x:
lblClue.configure(text = 'Its a bigger number')
return remainingAttempts(Attempts)
if numWritten == x:
lblClue.configure(text = 'Congratulations ;)')
remainingTime = False
return remainingTime, countdown(0)
def gameOver():
if remainingTime == False and Attempts != 0:
lblClue.configure(text = '¡Time\'s up!')
else:
lblClue.configure(text = 'No attempts')
def remainingAttempts(countAtempts):
Attempts = countAtempts
if Attempts == 0:
return remainingTime, countdown(0), Attempts, gameOver()
else:
Attempts = Attempts - 1
entryWriteNumber = tkinter.Entry(window)
entryWriteNumber.grid(column = 0, row = 1, padx = 10, pady = 10)
lblNumber = tkinter.Label(window, text = 'Number', font = 'Comic 13 bold')
lblNumber.grid(column = 0, row = 0, padx = 10, pady = 10, sticky = tkinter.W )
buttonGuess = tkinter.Button(window, text = 'Guess', bg = 'light grey', padx = 20, command = numRamdom)
buttonGuess.grid(column = 0, row = 2, sticky = tkinter.W, padx = 10, pady = 10)
lblClue = tkinter.Label(window, text = 'Clue', font = 'Comic 13 bold')
lblClue.grid(column = 0, row = 3, padx = 10, pady = 10, sticky = tkinter.W )
lblCrono = tkinter.Label(window, text = '', bg = 'white', fg = 'red', font = 'Comic 20', padx = 50, pady = 5)
lblCrono.grid(column = 1, row = 5, sticky = tkinter.S, padx = 100, pady = 150)
countdown(30)
window.mainloop()
答案 0 :(得分:1)
当您摆脱无所事事或不必要的一切时,管理起来会容易得多。您所有的returns
都无所事事。有计数器时无需创建remainingTime
变量。给小部件和功能提供一堆复杂和/或误导性的名称并没有帮助。您正在呼叫countdown(0)
,后者先呼叫gameOver()
,然后再呼叫gameOver()
。
您从来没有在row=4
上放任何东西,而是在row=5
上放了计时器。显然,这与将其放到4上没有什么不同。您有非常重复的grid
options
,因此我在dict
中将它们同质化,然后将dict
用作**kwargs
。这样写参数-> func(arg1 = value1, arg2 = value2, ...)
没有好处。没有理由保留对lblNumber
或buttonGuess
的引用。您绝不会以任何方式修改或进一步引用。如果您未指定column
,则tkinter
将假设您的意思是column=0
。如果您未指定row
,则tkinter
会假设您的意思是比当前总行大1行,而与列无关。导入tkinter
而不带别名只会给您带来更多输入。
以下是我根据我刚写的内容对游戏进行的编辑。
import tkinter as tk
import random
root = tk.Tk()
root.geometry('600x500')
x = random.randint(1,10)
remaining = 4
def countdown(time_left):
global process
chrono['text'] = str(time_left)
if time_left:
process = root.after(1000, countdown, time_left-1)
else:
gameOver()
def check():
global remaining
n = int(guess.get())
if n == x:
gameOver(True)
return
else:
clue['text'] = f'Its a {"smaller" if n > x else "bigger"} number'
remaining -= 1
if not remaining:
gameOver()
def gameOver(win=False):
root.after_cancel(process)
if not win:
clue['text'] = '¡Time\'s up!' if remaining else 'No attempts remain'
else:
clue['text'] = 'Congratulations ;)'
grid = dict(padx=10, pady=10, sticky=tk.W)
tk.Label(root, text='Number', font='Comic 13 bold').grid(**grid)
guess = tk.Entry(root)
guess.grid(**grid)
tk.Button(root, text='Guess', bg='light grey', padx=20, command=check).grid(**grid)
clue = tk.Label(root, text='Clue', font='Comic 13 bold', width=20, anchor='w')
clue.grid(**grid)
chrono = tk.Label(root, text='', bg='white', fg='red', font='Comic 20', padx=50, pady=5)
chrono.grid(column=1, **grid)
countdown(30)
root.mainloop()