如何动态调整tkinter帧的大小

时间:2018-04-28 15:32:50

标签: python python-3.x tkinter

我有一个框架,我想用作侧边栏导航菜单。我想用鼠标重新调整菜单。我有代码来调整框架的大小,但在调整顶层窗口大小之前它不会调整大小。我知道PanedWindows,这对我不起作用。

from tkinter import *


def dragbar_on_click(event):
    event.widget.mouse_x = event.x


def dragbar_on_release(event):
    event.widget.mouse_x = 0


def dragbar_on_motion(event):
    if event.widget.mouse_x != 0:
        width = event.widget.parent.winfo_width() + event.x - event.widget.mouse_x

        event.widget.parent.config(width=width)


class DragBar(Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent


class SideMenu(Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.config(bg='red', width=300)
        self.rowconfigure(0, weight=1)
        self.grid(sticky='NSEW')
        self.grid_propagate(0)

        self.frame = Frame(self, bg='purple')
        self.columnconfigure(0, weight=1)
        self.frame.grid(row=0, column=0, sticky='NSEW')

        self.dragbar = DragBar(self, bg='green', width=10)
        self.dragbar.mouse_x = 0
        self.dragbar.grid(row=0, column=1, sticky='NSW')

        self.dragbar.bind("<Motion>", dragbar_on_motion)
        self.dragbar.bind("<Button-1>", dragbar_on_click)
        self.dragbar.bind("<ButtonRelease-1>", dragbar_on_release)


root = Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(1, weight=1)

f = SideMenu(root)
Label(f.frame, text='This is a test line.').grid(sticky='NW')

root.mainloop()

1 个答案:

答案 0 :(得分:0)

我不是100%确定是否有更好的方法,但看起来滑块在"<Configure>"事件触发时更新。

所以这是一个快速的hacky方法来做到这一点。

在您的函数dragbar_on_motion中添加以下行:

x = root.winfo_width()
y = root.winfo_height()
root.geometry("{}x{}".format(x, y))

这会强制"<Configure>"事件触发并更新滑块。

以下是您修改的代码:

from tkinter import *


def dragbar_on_click(event):
    event.widget.mouse_x = event.x


def dragbar_on_release(event):
    event.widget.mouse_x = 0

def dragbar_on_motion(event):
    if event.widget.mouse_x != 0:
        width = event.widget.parent.winfo_width() + event.x - event.widget.mouse_x
        event.widget.parent.config(width=width)
        x = root.winfo_width()
        y = root.winfo_height()
        root.geometry("{}x{}".format(x, y))

class DragBar(Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent


class SideMenu(Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.config(bg='red', width=300)
        self.rowconfigure(0, weight=1)
        self.grid(sticky='NSEW')
        self.grid_propagate(0)

        self.frame = Frame(self, bg='purple')
        self.columnconfigure(0, weight=1)
        self.frame.grid(row=0, column=0, sticky='NSEW')

        self.dragbar = DragBar(self, bg='green', width=10)
        self.dragbar.mouse_x = 0
        self.dragbar.grid(row=0, column=1, sticky='NSW')

        self.dragbar.bind("<Motion>", dragbar_on_motion)
        self.dragbar.bind("<Button-1>", dragbar_on_click)
        self.dragbar.bind("<ButtonRelease-1>", dragbar_on_release)


root = Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(1, weight=1)

f = SideMenu(root)
Label(f.frame, text='This is a test line.').grid(sticky='NW')

root.mainloop()