【问题标题】:Create a hotkey that allows an event to be toggled on and off after a button is pressed创建一个热键,允许在按下按钮后打开和关闭事件
【发布时间】:2021-05-11 05:16:54
【问题描述】:

尝试创建一个热键(例如Shift + d)来打开或关闭事件。

有两个输入框。最上面的有你想要说的单词或短语。底部是每次说出单词/短语之间的冷却时间。现在它的设置是,当点击“输入”按钮时,它会立即开始输入带有冷却时间的短语。

我希望这样当单击“输入”按钮时,它不会开始输入,直到您使用热键(如shift + d)切换它,然后再次按下它时停止。我试过搞乱pynput,但我所做的一切要么导致错误,要么当我点击按钮时程序崩溃了。

from tkinter import Tk, Button, Checkbutton, Entry, Frame
import time 
import threading
from pynput.keyboard import Listener, KeyCode, Controller, Key
from pynput import keyboard

#Main window of the Gui
root = Tk()
root.title("Phrase Test")
root.configure(bg="black")
root.geometry("250x100")
root.resizable(width=False, height=False)

#delete text in entry box
def some_callback(event):
    event.widget.delete(0, "end")
    return None

#Command Entry box
e = Entry(root, width=25, bg="#101010", highlightbackground="black", fg="white", justify="center")
e.pack()
e.insert(0, "Enter your phrase:")
e.bind("<Double-1>", some_callback)

#Cooldown entrybox
e2 = Entry(root, width=25, bg="#101010", highlightbackground="black", fg="white", justify="center")
e2.pack()
e2.insert(0, "Enter cooldown time:")
e2.bind("<Double-1>", some_callback)


#Command
def makemoney():

    keyboard = Controller()
       
    while True:
        keyboard.type(e.get())
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        time.sleep(e2.get())

#Activate Button
myButton = Button(root, text="Enter", command=makemoney, bg="black", highlightbackground="black", fg="white")
myButton.pack()

root.mainloop()

【问题讨论】:

    标签: python-3.x tkinter pynput


    【解决方案1】:

    根据我从您的问题中了解到的情况,您可以尝试这样的事情(这是您可以做到这一点的多种方式之一)

    from tkinter import Tk, Button, Checkbutton, Entry, Frame
    import keyboard
    
    #Main window of the Gui
    root = Tk()
    root.title("Phrase Test")
    root.configure(bg="black")
    root.geometry("250x100")
    root.resizable(width=False, height=False)
    
    #delete text in entry box
    def some_callback(event):
        event.widget.delete(0, "end")
        return None
    
    def check():
        global pressed
        if keyboard.is_pressed('Shift') and keyboard.is_pressed('d'):
            keyboard.press('\b')
            if not pressed:
                pressed=True
                makemoney()
            else:
                pressed=False
                root.update()
        root.after(100,check)
    
    def makemoney():
        if pressed:
            keyboard.write(e.get())
            root.after(int(e2.get())*1000,makemoney)
    
    #Command Entry box
    e = Entry(root, width=25, bg="#101010", highlightbackground="black", fg="white", justify="center")
    e.pack()
    e.insert(0, "Enter your phrase:")
    e.bind("<Double-1>", some_callback)
    
    #Cooldown entrybox
    e2 = Entry(root, width=25, bg="#101010", highlightbackground="black", fg="white", justify="center")
    e2.pack()
    e2.insert(0, "Enter cooldown time:")
    e2.bind("<Double-1>", some_callback)
    
    pressed=False
    
    #Activate Button
    myButton = Button(root, text="Enter", command=check, bg="black", highlightbackground="black", fg="white")
    myButton.pack()
    
    root.mainloop()
    
    • 我使用了keyboard 模块而不是pynput,它应该足以满足您的用例。
    • 我尝试不使用带有time.sleep() 函数的无限while 循环,因为它会干扰主线程,而是使用.after() 方法来安排调用。
    • 我定义了一个函数check(),它检查Shift + d的按键事件,更改pressed标志并相应地调用makemoney()函数。它会在每 100 毫秒后重复一次。
    • 如果第一次按下热键,makemoney() 函数会在指定的秒数后调用自身。

    您可以改进代码,但我希望这提供了一个关于如何进行的想法。

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多