python - Tkinter gui 不起作用鼠标和键盘模块
问题描述
我试图使用鼠标和键盘模块以及 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')
我应该怎么办?
解决方案
GUI 没有显示的原因是在代码命中之前mainloop()
它进入了一个无限循环(while
循环)并且它无法到达mainloop
,因此窗口没有显示并且事件没有被处理。所以你应该做的是摆脱while
. 一种方法是使用after()
方法来模拟while
.
def joesd(self):
if kb.is_pressed('q'):
ms.press('left')
ms.release('left')
self.after(100,self.joesd)
这将每 100 毫秒重复一次该功能,您也可以将其减少到 1 毫秒。但请确保系统无法处理太多。
推荐阅读
- xamarin - Xamarin Forms - 我们如何使用 ble 连接 MI-Band
- git - 如何检查 Atom 中更改了哪个提交行?
- apache-spark - Spark - EMR - GlueCatalog:DataFrameWriter.bucketBy() 失败并出现 UnknownHostException
- c# - 将 .Net Core 2.2 Web 部署到 NGINX Centos 7 失败
- php - 将值添加到数组中以获得新值
- java - Java缓存系统(JCS)中的打印空值
- visual-studio - 我无法在文件后面获取 specflow 的代码以重新生成
- html - 如何在 HTML5 表格中设置列宽,第一行 colSpan > 1
- github - 有没有办法保留 Gist 设置 - 空格与制表符,换行与不换行?如何?
- fonts - MobaXterm V12 下 X11 应用程序中的默认字体大小