移动Tkinter窗口

时间:2020-07-17 09:43:42

标签: python tkinter

我正在尝试通过删除其标题栏来创建一个窗口。当用户单击并拖动鼠标时,使窗口移动。现在,如果我在任何小部件内单击并拖动,则窗口正在移动。但我想让它仅在用户在窗口小部件外部单击时移动。任何建议都会有所帮助。这是我的代码。

from tkinter import *
    
win = Tk()
win.geometry('200x200')
win.overrideredirect(True)

offsetx = 0
offsety = 0

lst = ['Sample 1', 'Sample 2', 'Sample 3', 'Sample 4']
    
def drag(event):
    x = win.winfo_pointerx() - win.offsetx
    y = win.winfo_pointery() - win.offsety
    win.geometry('+{x}+{y}'.format(x=x,y=y))

def click(event):
    win.offsetx = event.x
    win.offsety = event.y


win.bind('<Button-1>', click)
win.bind('<B1-Motion>', drag)
            
lb1 = Listbox(win)
lb1.pack()

b1 = Button(win, text='Close', command=win.destroy).pack()

for i in range(len(lst)):
    lb1.insert(i, lst[i])

win.mainloop()

3 个答案:

答案 0 :(得分:2)

在这里,我使用框架作为容器,而不是根窗口。 它将显示一个窗口,顶部区域为列表框,默认为10行,底部区域为一个按钮,两个框为绿色区域。在这里单击并拖动列表框和按钮将不会移动窗口,但是这两个深绿色区域起作用。

enter image description here

from tkinter import *

def mouse_down(event):
    global x, y
    x, y = event.x, event.y

def mouse_up(event):
    global x, y
    x, y = None, None

def mouse_drag(event):
    global x, y
    try:
        deltax = event.x - x
        deltay = event.y - y
        x0 = win.winfo_x() + deltax
        y0 = win.winfo_y() + deltay
        win.geometry("+%s+%s" % (x0, y0))
    except:
        pass

win = Tk()
# win.geometry('200x400')
win.overrideredirect(True)

x, y = None, None

frame = Frame(win, background='#004000')
frame.bind('<ButtonPress-1>', mouse_down)
frame.bind('<B1-Motion>', mouse_drag)
frame.bind('<ButtonRelease-1>', mouse_up)
frame.pack()

list_box = Listbox(frame)
list_box.pack()

button = Button(frame, text='Close', command=win.destroy)
button.pack()

lst = ['Sample 1', 'Sample 2', 'Sample 3', 'Sample 4']
for i, item in enumerate(lst):
    list_box.insert(i, item)

win.mainloop()

答案 1 :(得分:2)

似乎最简单的方法是检查拖动函数中哪个窗口接收到该事件,并且仅在根窗口中进行拖动:

def drag(event):
    if event.widget == win:
        x = win.winfo_pointerx() - win.offsetx
        y = win.winfo_pointery() - win.offsety
        win.geometry('+{x}+{y}'.format(x=x,y=y))

或者,如果您希望能够在中间窗口中单击时进行拖动,则可以检查小部件的类:

def drag(event):
    window_class = event.widget.winfo_class()
    if window_class in ("Tk", "Toplevel", "Frame"):
        x = win.winfo_pointerx() - win.offsetx
        y = win.winfo_pointery() - win.offsety
        win.geometry('+{x}+{y}'.format(x=x,y=y))

答案 2 :(得分:0)

如何使用tkinter创建标题栏画布并将其绑定到标题栏,而不是整个应用程序?

titlebar_canvas = tk.Canvas(win, height=20, width=200)
titlebar_canvas.pack(side='top')
titlebar_canvas.bind('<Button-1>', click)
titlebar_canvas.bind('<B1-Motion>', drag)