【发布时间】:2021-08-18 03:00:11
【问题描述】:
我试图使用鼠标和键盘模块以及 tkinter 作为 gui 来制作一个自动点击器并编写了这段代码
#Import
import tkinter as tk
import random as r8
import keyboard as kb
import mouse as ms
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
self.joesd()
def create_widgets(self):
self.joe = tk.Frame(self)#main frame
self.joe.pack(fill=tk.BOTH)
self.lbl = tk.Label(self.joe, text='Example text', height=5, width=30)
self.lbl.pack(side="top")# where the label will be located
self.lb = tk.Label(self.joe, text='Example Text', height=5, width=35)
self.lb.pack(side="top")# where the label will be located
def joesd(self):
while True:
if kb.is_pressed('q') == True:
ms.press('left')
ms.release('left')
root = tk.Tk()
app = Application(master=root)
app.mainloop()
然后我注意到 gui 从未出现,但如果我删除它就会出现
def joesd(self):
while True:
if kb.is_pressed('q') == True:
ms.press('left')
ms.release('left')
我该怎么办?
【问题讨论】:
-
在使用
tkinter时不应使用keyboard/mouse模块。而是查看tkinter事件绑定。 -
while将阻止mainloop更新 GUI。 -
@TheLizzard 可以 tkinter 事件绑定自动按键吗?
-
@CoolCloud 我删除了但仍然没有发生任何事情
-
@AmirTheDev 我建议你看一个 tkinter 教程。要使用 tkinter 的事件绑定,您需要传入一个函数,当按下鼠标/键盘按钮等事情发生时 tkinter 会调用该函数。
标签: python tkinter keyboard mouse