【发布时间】:2022-01-13 18:42:44
【问题描述】:
想知道如何将事件绑定到鼠标左键,但不必将鼠标悬停在窗口/应用程序/根目录中。基本上,我希望事件发生在我的鼠标指针碰巧在我的屏幕上的任何地方。现在我只能弄清楚当鼠标悬停在窗口/应用程序/根上时如何使鼠标按钮绑定起作用。
from tkinter import*
from tkinter import Tk, Label, StringVar
import random
import pyautogui
root = Tk()
root.configure(background='black')
root.attributes("-topmost", True)
root.overrideredirect(True)
root.geometry("100x100")
title_bar = Frame(root, bg='black')
title_bar.pack(side=TOP, fill=X)
tout = StringVar()
label = Label(root, textvariable=tout, font=('TkDefaultFont',
50), bg='black', fg='red')
label.pack()
def _quit():
root.quit()
root.destroy()
def move_app(event):
root.geometry(f'+{event.x_root}+{event.y_root}')
def gen_rand(event):
randvar = random.randint(0,99)
tout.set(randvar)
def clear_rand(event):
tout.set("")
myButton = Button(title_bar, text="X", font=('TkDefaultFont', 5),
bg='black', activebackground='red', fg='red', command=_quit)
myButton.pack(side=RIGHT)
root.bind('<Button-1>', gen_rand)
root.bind('<Button-3>', clear_rand)
root.bind("<B2-Motion>", move_app)
root.mainloop()
【问题讨论】:
标签: python tkinter binding window mouse