Python 如何避免再次按下按钮重置值
按下按钮时,Python 如何避免再次按下按钮重置值,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,按下按钮时,buttonGuess会运行numRandom功能,同时还会运行remainingAttemps功能。问题是,如果用户按下按钮gues,尝试值将再次签名 import tkinter import random window = tkinter.Tk() window.geometry('600x500') x = random.randint(1,10) remainingTime = True Attempts = 4 def countdown (time_left):
buttonGuess
会运行numRandom
功能,同时还会运行remainingAttemps
功能。问题是,如果用户按下按钮gues
,尝试
值将再次签名
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()
导入tkinter
随机输入
window=tkinter.Tk()
窗口几何体('600x500')
x=随机随机随机数(1,10)
剩余时间=真
尝试次数=4次
def倒计时(剩余时间):
全局剩余时间
如果remainingTime==True:
lblCrono.configure(text=str(左时间))
如果剩余时间>0:
时间左=时间左-1
窗口。之后(1000,倒计时,剩余时间)
其他:
剩余时间=假
lblCrono.configure(text=0)
返回剩余时间,gameOver()
其他:
返回
def numRamdom():
全球尝试
numwrited=int(EntryWriteEnumber.get())
如果numwrited>x:
configure(text='这是一个较小的数字')
返回剩余尝试次数(尝试次数)
如果numwrited
当你处理掉所有没有做任何事情或不必要的事情时,管理起来就容易多了。您所有的返回值都不起作用。当您有计数器时,不需要创建remainingTime
变量。给你的小部件和功能起一堆复杂和/或误导性的名字,是没有帮助的。您正在调用countdown(0)
调用gameOver()
,然后调用gameOver()
您从不在row=4
上设置某些内容,而是将计时器设置在row=5
上。这显然和把它放在4上没有什么不同。您有非常重复的网格
选项
,因此我在dict
中对它们进行了同质化,并将该dict
用作**kwargs
。像这样编写参数->func(arg1=value1,arg2=value2,…)
没有任何好处。没有理由保留对lblNumber
或buttonGuess
的引用。您也不会以任何方式修改或进一步引用。如果未指定列
,tkinter
将假定您的意思是列=0
。如果未指定行
,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()
当再次按下按钮时,您希望发生什么?因此,当您按下按钮时,如果号码不对,请减少尝试次数。但是当调用numRandom时,尝试再次被重新分配(至少我认为会发生这种情况)。我不认为这是怎么回事,因为numRamdom()
没有改变尝试次数
,也没有重新分配尝试次数(),有些可能与问题有关,但不是全部。虽然有可能解决问题,但这将是一项艰巨的工作。我建议您重新开始,将游戏定义为Python类
。这将消除大多数全局变量(可能是问题的根本原因)。另外,如果您要编写Python代码,我建议您阅读并开始遵循。非常感谢。我不知道**kwargs
。我对Python的实际知识并不了解所编写代码的某些方面:如果您的函数相同,为什么要使用全局剩余
和全局进程
<代码>如果未剩余
指剩余尝试为0?不能有'-1次尝试''?如果时间还剩,为什么要写?在这个函数中总是会有一个参数,否则你不能调用这个函数,对吗?无论如何,非常感谢你。我将努力理解代码并加以改进。@KIRA~global restination
指的是全局变量restination=4
。如果您没有在那里指定全局
,它会抛出一个错误,指出它是本地
<代码>过程
也用于gameOver
。如果某个值等于0
,False
或None
,则该值为非
,否则该值为“是”,无需特别检查某个值是否为0
<代码>剩余-=1
从当时剩余的中减去1。