简体   繁体   English

如何在 python 中更改 Tkinter panedWindow 宽度?

[英]How to change Tkinter panedWindow width in python?

I'm using tk.PanedWindow object to divide my GUI into two panes.我正在使用 tk.PanedWindow 对象将我的 GUI 分成两个窗格。 I want to change pane width dynamically.我想动态更改窗格宽度。 Because I'm planing save pane widths on ini file to reload after when program is loaded again.因为我打算在ini文件上保存窗格宽度以在程序再次加载后重新加载。

Tried self.side_panel.configure(width=200) and self.side_panel['width']=200 but those doesn't do any effect.试过self.side_panel.configure(width=200)self.side_panel['width']=200但那些没有任何效果。 Commands seems to be ignored.命令似乎被忽略了。 There is no error message, and nothing changed too.没有错误消息,也没有任何改变。

Below is the simplified version my GUI code.下面是我的 GUI 代码的简化版本。 Notice the # Adjust Geometry part.注意# Adjust Geometry部分。

import tkinter as tk
import tkinter.filedialog
import tkinter.ttk as ttk


class Window(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # Define GUI
        self.place_gui_elements()

        # Adjust Geometry
        self.geometry("640x500+500+200")
        self.statusbar['text'] = (
                                    "Window: " 
                                    + self.geometry()
                                    )

        self.side_panel.configure(width=200)
        self.side_panel['width']=200
        self.statusbar['text'] += (
                                    " side_panel.width:" +
                                    str(self.side_panel.winfo_width())
                                    ) 

    def place_gui_elements(self):
        # Define GUI Elements      
        self.pw = ttk.PanedWindow(orient = "horizontal")
        self.main_panel = tk.Frame(self.pw, borderwidth = 1, relief = "sunken")
        self.side_panel = tk.Frame(self.pw, borderwidth = 1, relief = "sunken")
        self.map_canvas = tk.Canvas(borderwidth = 0, highlightthickness = 0,
                                        bg="black")
        self.tile_canvas = tk.Canvas(borderwidth = 0, highlightthickness = 0)
        self.scrollbar = tk.Scrollbar(orient = "vertical", borderwidth = 1)
        self.statusbar = tk.Label(self, borderwidth = 1, anchor = 'w',
                                    relief = "sunken")

        # Place Already Defined GUI Elements
        self.statusbar.pack(side = "bottom", fill="x")
        self.pw.pack(fill="both", expand=True)

        self.pw.add(self.main_panel, weight=1)
        self.map_canvas.pack(in_ = self.main_panel, side = "left",
                                fill = "both", expand = True)
        self.pw.add(self.side_panel, weight=0)
        self.tile_canvas.grid(in_ = self.side_panel, row = 1, column=0,
                                sticky="nsew")
        self.scrollbar.grid(in_ = self.side_panel, row = 1, column=1,
                                sticky="ns")


if __name__ == "__main__":
    win = Window()
    win.mainloop()

Edited -and further simplified- code after @Bryan Oakley suggestion.在@Bryan Oakley 建议之后编辑 - 并进一步简化 - 代码。 Unfortunately still not working.不幸的是仍然无法正常工作。

import tkinter as tk
import tkinter.filedialog
import tkinter.ttk as ttk


class Window(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # Define GUI Elements      
        self.pw = ttk.PanedWindow(orient = "horizontal")
        self.main_panel = tk.Frame(self.pw, borderwidth = 1, relief = "sunken")
        self.side_panel = tk.Frame(self.pw, borderwidth = 1, relief = "sunken")

        # Place Already Defined GUI Elements
        self.pw.pack(fill="both", expand=True)
        self.pw.add(self.main_panel, weight=1)
        self.pw.add(self.side_panel, weight=0)

        # Adjust Geometry
        self.geometry("640x300+500+200")
        self.pw.sashpos(0, 300)
        self.update()
        print ("sashpos %s" % self.pw.sashpos(0)) 


if __name__ == "__main__":
    win = Window()
    win.mainloop()

You can use the sashpos method of the ttk paned window to both get the relative position of the sash (the thing that divides the panes), and to set it.您可以使用sashpos窗格窗口的sashpos方法来获取窗扇(分隔窗格的东西)的相对位置,并对其进行设置。 If you pass it just the index of the sash (the first sash is number 0 (zero)) then it will return the location of that sash.如果您只传递腰带的索引(第一个腰带是数字 0(零)),那么它将返回该腰带的位置。 If you provide an additional argument, it will set the sash to that position.如果你提供一个额外的参数,它会将窗扇设置到那个位置。

For example, to get the position of the first sash you would use:例如,要获取第一个腰带的位置,您将使用:

position = self.win.sashpos(0)

If you read position in from an ini file, you can restore it like this:如果您从 ini 文件中读取position ,您可以像这样恢复它:

self.win.sashpos(0, position)

In the updated code you provided, you should call update before setting the sash position.在您提供的更新代码中,您应该设置窗扇位置之前调用update I think what's happening is that since the paned window hasn't yet been displayed, it's actual width is 1. When you set the position of the sash, you're setting it outside the bounds of the window so tkinter resets the position to the smallest value less than the width of the window.我认为正在发生的事情是,由于尚未显示窗格窗口,它的实际宽度为 1。当您设置窗扇的位置时,您将其设置在窗口边界之外,因此 tkinter 将位置重置为小于窗口宽度的最小值。

也许为时已晚,但对于其他人,我只是在一小段延迟后才设法调整窗格的大小

self.after(50, lambda: self.win.sashpos(0, position))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM