【发布时间】:2020-12-29 02:03:05
【问题描述】:
我正在尝试为我的朋友制作一个点击游戏。我只想知道如何制作一个按钮,当单击它时,它会单击另一个按钮。以下是部分代码:
from tkinter import *
import time
root = Tk()
root.geometry('600x600')
score = 2000000
clicker_counter = 0
def counter():
global score
score += 1
points_label.config(text=score)
def autoclicker(args):
global clicker_counter
if args == 1:
pass
def clickerpurchase():
global clicker_counter
global score
if score >= 1000:
score -= 1000
clicker_counter += 1
points_label.config(text=score)
clicker_label['text'] += str(clicker_counter)
clicker_label.config(text='purchase clicker(1k): ' + str(clicker_counter))
clicker_button = Button(root, text='purchase', command=lambda:[clickerpurchase, autoclicker(1)])
clicker_button.grid(row=0, column=3)
clicker_label = Label(root, text='purchase clicker(1k): ')
clicker_label.grid(row=0, column=2)
points_label = Label(root, text='0')
points_label.grid(row=0, column=1)
points_button = Button(root, text='click me', command=counter)
points_button.grid(row=0, column=0)
points_label.config(text=score)
root.mainloop()
clicker_button 是主要问题。 clickerpurchase() 函数负责更新score 和clicker_counter。该按钮也绑定到autoclicker(args)。我希望clicker_button 每隔一段时间点击一次points_button。我正在考虑将自动点击代码放在autoclicker(args) 函数中,但我不知道它的代码。
编辑:
我在counter() 函数中创建了一个“while”循环,并在其中添加了args。我给 points_button 一个 arg 1 和 clicker_button 一个 arg 2。我的代码现在看起来像这样:
def counter(args):
global score
if args == 1:
score += 1
points_label.config(text=score)
if args == 2:
while args == 2:
time.sleep(1)
points_button.invoke()
points_button = Button(root, text='click me', command=counter(1))
points_button.grid(row=0, column=0)
clicker_button = Button(root, text='purchase', command=lambda:[clickerpurchase, counter(2)])
clicker_button.grid(row=0, column=3)
每当我单击clicker_button 时,points_button 都会被单击,但程序会崩溃。我完全抛弃了autoclicker(args) 函数。
【问题讨论】:
-
为什么你希望按钮被点击?您可以根据需要每隔一段时间调用相关函数
counter()。 -
另外,
args的值有什么意义? -
@AST args 值不执行任何操作。我只是在寻找解决问题的方法。
-
阅读文档。该按钮有一个
invoke方法。 -
@AST 而不是手动单击以获得分数我希望程序每隔一段时间为我单击一次。请注意,分数应该等于零。我将其设置为高以用于测试目的