resetCounter()函数未被访问或在python Tkinter程序中未按预期工作

2024-06-24 11:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个tkinter程序,希望屏幕上始终有一个按钮(button1)。每次单击第一个按钮(按钮1)时,新按钮(按钮2)应比上一个按钮低30像素。为此,我使用了一个计数器,将新按钮(button2)的y值乘以30乘以计数器。单击任何一个新按钮(按钮2)时,我希望计数器复位,以便新按钮开始出现在第一个按钮再次出现的位置。换句话说,单击按钮1时,按钮2应再次出现在原始位置

我通过访问resetCounter()函数来实现这一点,该函数应再次将计数器设置为0

我遇到的问题是,我的计数器从未重置,新按钮(button2)即使在单击其中一个按钮后,也会一直低于上一个按钮。我不确定resetCounter()函数是否从未被访问,或者resetCounter()函数是否有问题。请帮忙,谢谢

我的代码如下所示:

from tkinter import *

window = Tk()
window.geometry("300x300")
window.configure(background='gray')

counter = 0
def resetCounter(counter):
   counter = 0

def button1Clicked():
   global counter
   button2 = Button(window, text="click me when ready", command=lambda:[(resetCounter(counter)), button2.destroy()])
   button2.place(x=50, y=(100 + (counter*30)))
   counter += 1

button1 = Button(window, text="click me", command=button1Clicked)
button1.place(x=0, y=0)

window.mainloop()


Tags: 函数texttkinterdefcounter计数器buttonwindow