python - 定义窗口并添加 mainloop() 后,Tkinter 窗口未打开
问题描述
我试图制作一个改进的自动点击器,具有更多功能,例如:
- 按键保存光标位置
- 按键执行
- 在按键上杀死程序这是代码:
import win32api, win32com
import pyautogui
import tkinter as tk
from time import sleep
import keyboard
win = tk.Tk()
cursor = (0,0)
di = 0
e = tk.Entry()
def geti():
di = float(e.get())
l = tk.Label(text="Info:").pack()
l1 = tk.Label(text="Press q to stop clicking").pack()
l2 = tk.Label(text="Press c to copy mouse position to execute on").pack()
l3 = tk.Label(text="Press e to start executing").pack()
dl = tk.Label(text="Delay between eack click (s)").pack()
e.pack()
b = tk.Button(text="save delay time",command=geti).pack()
def click(x, y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
sleep(di)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
while True:
if(keyboard.is_pressed('c') == True):
cursor = pyautogui.position()
if(keyboard.is_pressed('q') == True):
break
if(keyboard.is_pressed('e') == True):
click(cursor[0], cursor[1])
win.mainloop()
解决方案
您的 while 循环会阻止 win.mainloop()。您可以将 tkinters 事件系统与绑定一起使用,而不是检查按键:
win.bind('key', callback)
# thanks to @acw1668's comment: added evt
win.bind('e', lambda evt: click(cursor[0], cursor[1]))
lambda 是必要的,因为您希望调用函数,而不是结果。来自tkinter 文档:
如果这样做,Python 将在创建小部件之前调用回调函数,并将函数的返回值传递给 Tkinter。然后 Tkinter 尝试将返回值转换为字符串,并告诉 Tk 在按钮被激活时调用具有该名称的函数。这可能不是你想要的。
推荐阅读
- python-2.7 - 是否可以在 GUI 之外使用数据?
- python - 使用 unique_together 并保存经过身份验证的用户时出现问题
- ember.js - Ember init 无法识别 _super
- entity-framework - 如何从使用 EF 核心的父级获取所有子元素
- reactjs - 反应路由器路由特定创建存储,而不是在单页应用程序中加载页面加载时的所有状态
- python - 如何在本地运行我的 Cloud Run 服务?
- asp.net-core - 无法解析类型“Data.ApplicationDbContext”的服务
- c - 为什么会发生这种情况?(C 编程错误)
- javascript - IE 抛出预期的冒号错误,在 Chrome 中有效吗?具有对象字面量和函数的对象数组
- python-3.x - 错误:在执行 pip3 install python-geohash 时,命令“x86_64-linux-gnu-gcc”失败,退出状态为 1