簡體   English   中英

當我在按下按鈕 1 后單擊按鈕 2 時,它不起作用

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

當我在按下按鈕 1 后單擊按鈕 2 時,它不起作用。

我正在制作一個有趣的自動點擊器,作為一個副項目。

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()

如果你想改變它,你必須聲明全局標志
也正如 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

我剛剛注意到的東西
button2中, command=lambda:end_f
刪除 lambda
這基本上是說

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

並且由於命令( l )是在單擊按鈕時執行的,
它只返回function,不執行

當您單擊第一個按鈕時,該標志設置為 True。 但是,當您單擊第二個按鈕時,該標志將設置為 False。 稍后當您返回第一個按鈕時,該標志為 False,因此它永遠不會進入 while 循環。

您想嘗試並以另一種方式實施嗎?

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