如何让 tkinter 按钮自己点击?
how to make a tkinter button click itself?
我正在尝试为我的朋友制作一款点击游戏。我只想知道如何制作一个按钮,当点击时,点击另一个按钮。这是一些代码:
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)
功能。
就你的功能而言,你可以尝试这样的事情
def autoclicker(args):
global clicker_counter
if args == 1:
time_interval=1000 #change it as you like
points_button.invoke()
root.after(time_interval,lambda:autoclicker(1))
我使用了 root.after()
方法,这将在您指定的每 time_interval
毫秒调用您的 points_button
。
此外,据我了解你的目的是(请随时纠正我),你真的不需要一个按钮来增加分数,你可以只替换 points_button.invoke()
在带有 counter()
的代码中,它应该给出相同的结果。即使您计划拥有多个,您也可以将不同的参数传递给 clickerpurchase()
、counter()
、autoclicker(args)
,以便为每个项目获得不同的分数。
编辑(问题编辑后)
time.sleep()
在无限循环中将不起作用,因为整个程序将等待它完成才能执行任何其他事情,因此它停止响应并崩溃。尝试以与我上面提到的类似的方式使用 .after()
方法。
如果你想在没有autoclicker()
功能的情况下工作,就像你在编辑中所说的那样,你可以进行以下更改
def counter(args):
global score
if args==1:
score += 1
points_label.config(text=score)
elif args==2:
time_interval=1000 #change it as you like
points_button.invoke()
root.after(time_interval,lambda:counter(2))
points_button = Button(root, text='click me', command=lambda:counter(1))
我正在尝试为我的朋友制作一款点击游戏。我只想知道如何制作一个按钮,当点击时,点击另一个按钮。这是一些代码:
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)
功能。
就你的功能而言,你可以尝试这样的事情
def autoclicker(args):
global clicker_counter
if args == 1:
time_interval=1000 #change it as you like
points_button.invoke()
root.after(time_interval,lambda:autoclicker(1))
我使用了 root.after()
方法,这将在您指定的每 time_interval
毫秒调用您的 points_button
。
此外,据我了解你的目的是(请随时纠正我),你真的不需要一个按钮来增加分数,你可以只替换 points_button.invoke()
在带有 counter()
的代码中,它应该给出相同的结果。即使您计划拥有多个,您也可以将不同的参数传递给 clickerpurchase()
、counter()
、autoclicker(args)
,以便为每个项目获得不同的分数。
编辑(问题编辑后)
time.sleep()
在无限循环中将不起作用,因为整个程序将等待它完成才能执行任何其他事情,因此它停止响应并崩溃。尝试以与我上面提到的类似的方式使用 .after()
方法。
如果你想在没有autoclicker()
功能的情况下工作,就像你在编辑中所说的那样,你可以进行以下更改
def counter(args):
global score
if args==1:
score += 1
points_label.config(text=score)
elif args==2:
time_interval=1000 #change it as you like
points_button.invoke()
root.after(time_interval,lambda:counter(2))
points_button = Button(root, text='click me', command=lambda:counter(1))