Tkinter window 在定义 window 并添加 mainloop() 后未打开
Tkinter window not opening after defining window and adding mainloop() also
我正在尝试制作具有更多功能的改进型自动答题器,例如:
- 按一个键保存光标位置
- 按键时执行
- 一键终止程序
这是代码:
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-loop 块 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 是必需的,因为您希望调用函数而不是结果。来自 the tkinter docs:
If you do this, Python will call the callback function before creating
the widget, and pass the function’s return value to Tkinter. Tkinter
then attempts to convert the return value to a string, and tells Tk to
call a function with that name when the button is activated. This is
probably not what you wanted.
我正在尝试制作具有更多功能的改进型自动答题器,例如:
- 按一个键保存光标位置
- 按键时执行
- 一键终止程序 这是代码:
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-loop 块 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 是必需的,因为您希望调用函数而不是结果。来自 the tkinter docs:
If you do this, Python will call the callback function before creating the widget, and pass the function’s return value to Tkinter. Tkinter then attempts to convert the return value to a string, and tells Tk to call a function with that name when the button is activated. This is probably not what you wanted.