我在Python中设置了热键但不起作用,如何设置有效的热键?

问题描述 投票:0回答:1

我正在尝试制作一个程序,可以根据个人需要快速单击按钮,但现在我只能单击左侧按钮。我正在尝试制作一个热键来将其关闭,但它不起作用。 我不能使用线程,这就是为什么它都在同一个循环中。

from tkinter import *
from tkinter import messagebox
import pyautogui
import time
import keyboard
#3 We define the spam command
spamming=False
def off():
      global spamming
      spamming=False
def spam():
    global clickidy
    clickidy=tipidytype.get()
    global spamming
    spamming= True
    while spamming==True:
      pyautogui.click(clicks=1)
      root.update()
      try:
           if keyboard.is_pressed("q"):
                off()
      except:
           nothing=0
#1 Lets set up our window
root=Tk()
root["bg"]="#fafafa"
root.title("Sigma clicker")
root.geometry("400x110")
root.resizable(width=False, height=False)
frame=Frame()
frame.place(relx=0,rely=0,relheight=1,relwidth=1)
#2 Now we can add interface
title = Label(frame, text="Enter the button for spamming here")
title.place(x=15, y=15)
tipidytype=Entry(frame,bg="white")
tipidytype.place(x=15, y=45)
tester = Entry(frame, bg="white")
tester.place(x=250,y=45)
testtext=Label(frame, text="Focus here before starting")
testtext.place(x=250,y=15)
button = Button(frame,text="      On     ", bg="gray", command=spam)
button.place(x=15,y=75)
off=Button(frame,text="Off", bg="gray", command=off)
off.place(x=250,y=75)
def window_exit():
      global spamming
      spamming=False
      root.destroy()
root.protocol("WM_DELETE_WINDOW", window_exit)
root.mainloop()

我尝试过延迟和两 (2) 种不同类型的键盘输入(侦听器不起作用,因为它需要循环)。

python loops tkinter keyboard pyautogui
1个回答
0
投票

您可以使用

.after()
循环进行点击。关于热键,解决方案可以将
<Key>
绑定到
root
上并检查
event.keysym == 'q'
是否存在,但这仅在 GUI 处于活动状态时才有效。如果垃圾邮件按钮不在您的
Tk
应用程序中,则 此答案 显示如何设置系统热键。

这是一个将绑定方法重构为 OOP 方法的示例:

from tkinter import *
import pyautogui


class Spammer(Tk):
    def __init__(self):
        super().__init__()  # replaces root=Tk() as self of class
        self.spamming = False  # use instance variable instead of global variable
        self.clickidy = None
        # build GUI
        self["bg"] = "#fafafa"
        self.title("Sigma clicker")
        self.geometry("400x110")
        self.resizable(width=False, height=False)
        frame = Frame()
        frame.place(relx=0, rely=0, relheight=1, relwidth=1)
        title = Label(frame, text="Enter the button for spamming here")
        title.place(x=15, y=15)
        self.tipidytype = Entry(frame, bg="white")  # make this an instance variable to access from other function
        self.tipidytype.place(x=15, y=45)
        tester = Entry(frame, bg="white")
        tester.place(x=250, y=45)
        testtext = Label(frame, text="Focus here before starting")
        testtext.place(x=250, y=15)
        button = Button(frame, text="      On     ", bg="gray", command=self.start)
        button.place(x=15, y=75)
        off = Button(frame, text="Off", bg="gray", command=self.off)
        off.place(x=250, y=75)
        # bindings
        self.bind('<Key>', self.on_key)  # bind keypress on root → only working while Tk window is in focus
        self.protocol("WM_DELETE_WINDOW", self.window_exit)

    def on_key(self, event):
        ''' check if q button is pressed while window is active '''
        if event.keysym == 'q':
            self.off()

    def off(self):
        ''' toggle spamming off '''
        self.spamming = False

    def start(self):
        ''' initiate spamming '''
        self.clickidy = self.tipidytype.get()
        self.spamming = True
        self.spam()

    def spam(self):
        ''' actual spamming with .after "loop" '''
        if self.spamming:
            print('click')  # print debugging for testing the click loop
            # pyautogui.click(clicks=1)  # maby leave this commented for testing ;)
            self.after(1000, self.spam)  # click again after 1000 ms if spamming remains True

    def window_exit(self):
        ''' this is unneccesary cleanup '''
        self.spamming = False
        self.destroy()


if __name__ == '__main__':
    app = Spammer()
    app.mainloop()

© www.soinside.com 2019 - 2024. All rights reserved.