简体   繁体   English

当我在按下按钮 1 后单击按钮 2 时,它不起作用

[英]When I click on button 2 after pressing on button 1 it does not work

When I click on button 2 after pressing on button 1 it does not work.当我在按下按钮 1 后单击按钮 2 时,它不起作用。

I am making an auto clicker for fun, as a side project.我正在制作一个有趣的自动点击器,作为一个副项目。

import tkinter as tk
from pynput.mouse import Button, Controller
import time

Height = 700
Width = 800
mouse = Controller()
flag = True

def  click_function():
    while flag == True:
        time.sleep(.001)
        mouse.click(Button.left, 1)

def endclick_function():
    flag = False

root = tk.Tk()

canvas = tk.Canvas(root, height=Height, width=Width)
canvas.pack()

frame = tk.Frame(root,bg='black')
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.5)

button = tk.Button(frame, text="Start" , bg='white', fg='black', font=50, command=click_function)
button.place(relx=0, rely=0, relwidth=0.5, relheight=0.5)

button2 = tk.Button(frame, text="Stop" , bg='white', fg='black', font=50, command=lambda: endclick_function)
button2.place(relx=.5, rely=0, relwidth=0.5, relheight=0.5)

label = tk.Label(frame, text='Time to Sleep:', bg='white', font=50)
label.place(relx=0, rely =0.5, relwidth=0.5, relheight=0.25)

label2 = tk.Label(frame, text='How many times to click:', bg='white', font=50)
label2.place(relx=0, rely =0.75, relwidth=0.5, relheight=0.25)

entry = tk.Entry(frame, bg='white')
entry.place(relx=0.5, rely =0.5, relwidth=0.5, relheight=0.25)

entry2 = tk.Entry(frame,text='Time to Sleep(ms):', bg='white')
entry2.place(relx=0.5, rely =0.75, relwidth=0.5, relheight=0.25)

root.mainloop()

you have to declare flag global if you want to change it如果你想改变它,你必须声明全局标志
also as Joe Ferndz pointed out, the flag is never set back to True也正如 Joe Ferndz 指出的那样,标志永远不会设置回 True

def  click_function():
    global flag
    flag = True # of course, only if you want to use clicker more than once
    while flag == True:
        time.sleep(.001)
        mouse.click(Button.left, 1)

def endclick_function():
    global flag
    flag = False

Something I just noticed我刚刚注意到的东西
in button2 , command=lambda:end_fbutton2中, command=lambda:end_f
remove lambda删除 lambda
this is basically saying这基本上是说

def l():
    return end_f
button2['command'] = l

and since the command ( l ) is executed at the click on the button,并且由于命令( l )是在单击按钮时执行的,
it only returns the function, does not execute it它只返回function,不执行

When you click the first button, the flag is set to True.当您单击第一个按钮时,该标志设置为 True。 However, when you click on second button, the flag gets set to False.但是,当您单击第二个按钮时,该标志将设置为 False。 Later when you come back to first button, the flag is False so it never goes into the while loop.稍后当您返回第一个按钮时,该标志为 False,因此它永远不会进入 while 循环。

Do you want to try and implement this an alternate way?您想尝试并以另一种方式实施吗?

def  click_function():
    while flag == True:
        time.sleep(.001)
        mouse.click(Button.left, 1)

def endclick_function():
    flag = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM