我已经在 tkinter 中创建了一个overrideredirected
根窗口,但是当我尝试从我放置标签的特定位置拖动 title_bar (我创建的)时似乎出现了问题
from tkinter import Tk, Label, Button, Frame
基本上这些功能是用于窗口移动,仅此而已
x_cor, y_cor = 0, 0
def start_move(event):
global x_cor, y_cor
x_cor = event.x
y_cor = event.y
def stop_move(_):
global x_cor, y_cor
x_cor = None
y_cor = None
def do_move(event):
global x_cor, y_cor
deltax = event.x - x_cor
deltay = event.y - y_cor
x = root.winfo_x() + deltax
y = root.winfo_y() + deltay
root.geometry(f"+{x}+{y}")
这是主程序
root = Tk()
root.overrideredirect(1)
root.geometry('500x500')
title = Frame(root, bg='pink')
title.pack(fill='x')
label = Label(title, text='Title of my program',bg='black', fg='white', anchor='c')
label.place(x=200, y=0)
close = Button(title, text='X', fg='white', bg='red', command=root.destroy)
close.pack(side='right')
title.bind("<ButtonPress-1>", lambda events: start_move(events))
title.bind("<ButtonRelease-1>", lambda events: stop_move(events))
title.bind("<B1-Motion>", lambda events: do_move(events))
root.mainloop()
当我在除所在空间之外的任何其他位置按住鼠标按钮时,标题栏移动得很好 label
。有没有办法来解决这个问题?
换句话说,我可以以某种方式“克服”标签并将标题栏从我想要的任何地方拖动吗?
提前致谢!