python-3.x - 按住开始按钮的自动答题器问题
问题描述
我正在使用pyautogui
自动点击器程序编写,程序由三个按钮(开始、停止、退出)和一个组合框组成。我的问题是当我按下开始按钮时,按钮被按住并且我无法按下另一个按钮。我用while循环编写了点击函数。我想在程序运行时按其他按钮。
from tkinter import *
from tkinter import ttk
from threading import Thread
import pyautogui
def durdur():
global veri,baslat
def thread():
baslat.config(relief="sunken",state=DISABLED,text="Stop")
run = Thread(target=tikla,args=(baslat))
run.start()
veri = ttk.Combobox(p, width=30)
veri["values"] = ("Left Click", "Right Click")
veri.place(x=13, y=10)
baslat = Button(p, text="Start", width=8, height=2,command=tikla)
baslat.place(x=10, y=40)
durdur = Button(p, text="Stop", width=8, height=2, command=thread)
durdur.place(x=80, y=40)
cikis = Button(p, text="Exit", width=8, height=2, command=lambda:
p.destroy())
cikis.place(x=150, y=40)
#click function
def tikla():
t=veri.get()
if t=="Left Click":
while True:
pyautogui.click(clicks=1,interval=0.5,button="left")
baslat.config(relief="raised",state=NORMAL,text="Start")
elif t=="Right Click":
while True:
pyautogui.click(clicks=1,interval=0.06,button="right")
baslat.config(relief="raised", state=NORMAL, text="Start")
else:
pass
p=Tk()
durdur()
p.title("Auto Clicker")
p.geometry("250x100")
if __name__=="__main__":
p.mainloop()
解决方案
在处理事件时,您的 GUI 将被阻止并且无法自行更新(直到事件完成)。相反,您可以使用 tkinterafter
方法来安排您的点击。
在按钮单击时,您可能希望输入一个执行一次“自动单击”的函数,然后安排自己在设定的时间后再次运行。after
下面是一个用于在不阻止 GUI 的情况下执行操作的示例。
import tkinter as tk
class AfterExample:
def __init__(self):
self.root = tk.Tk()
self.b1 = tk.Button(master = self.root, text = "Turn on", command = self.b1_cmd)
self.b2 = tk.Button(master = self.root, text = "Turn off", command = self.b2_cmd)
self.b1.pack()
self.b2.pack()
self._job = None # Represents the scheduled event
def b1_cmd(self):
if self._job is None: # Only start if it isn't already running
self._job = self.root.after(0, self.clicker)
return
def clicker(self):
print("<CLICK>")
self._job = self.root.after(100, self.clicker)
return
def b2_cmd(self):
print("Stopping!")
if self._job is not None:
self.root.after_cancel(self._job)
self._job = None
return
def start(self):
self.root.mainloop()
a = AfterExample()
a.start()
推荐阅读
- nativescript - NativeScript 从 /src 中排除 *.js
- r - 为什么调用 setDT 后 data.table 没有正确排序?
- algolia - 当搜索词在 Algolia 中为空时,如何防止显示搜索结果?
- actionscript-3 - Flash 加载第一个外部 swf 加载
- php - 使用 Laravel 删除文件夹中除 'placeholder.png' 之外的所有文件
- python - 将 python 类翻译成 cython
- java - 如何在 Spring Boot 应用程序中使用带有 jmx 的千分尺?
- bootstrap-4 - 从源代码引导 4:'npm run dist' 抛出错误
- javascript - SyntaxError: '[object HTMLDocument]' 不是 Firefox 中的有效选择器
- vba - 将 Active.Sheet 设置为 Range 时“需要对象”