簡體   English   中英

Tkinter 動態調整窗格內的小部件 Window

[英]Tkinter dynamically resize Widgets within a Paned Window

我目前有一個帶有兩個 LabelPanes 的 window。 在 rightPane 中,我有三個 LabelFrame。 當我調整 window 的大小時,最底部的 LabelFrame 變大,但頂部的兩個仍然是我在設置它們的高度時指定的大小。 我想要的是當 window 在 Y 軸上調整大小時,它們三個都能均勻增長。 這可能嗎?

import tkinter as tk
if __name__ == '__main__':
    appHeight=720
    aspectRatio=16/9
    appWidth=int(appHeight*aspectRatio)
    app=tk.Tk()
    app.title("Title")
    app.minsize(width=appWidth,height=appHeight)
    #================================================================================
    # Setup Panes
    #================================================================================
    #Create Left Pane for settings
    leftPane = tk.PanedWindow(bd=4,relief='raised',bg='red')
    leftPane.pack(fill=tk.BOTH,expand=1)
    #Add Settings Frame to Pane
    settingsFrame = tk.LabelFrame(leftPane,text='Signature Filters',padx=5,pady=5,width=int(appWidth/4))
    leftPane.add(settingsFrame)

    #Create Right Pane for charts
    rightPane = tk.PanedWindow(leftPane,orient=tk.VERTICAL,bd=4,relief=tk.SUNKEN,bg='blue')
    leftPane.add(rightPane)
    #Add Depth Frame to Pane
    depthFrame = tk.LabelFrame(rightPane,text='Depth Signature',padx=5,pady=5,height=int(appHeight/3))
    rightPane.add(depthFrame)
    #Add Velocity Frame to Pane
    velocityFrame = tk.LabelFrame(rightPane,text='Velocity Signature',padx=5,pady=5,height=int(appHeight/3))
    rightPane.add(velocityFrame)
    #Add Depth Frame to Pane
    currentFrame = tk.LabelFrame(rightPane,text='Current Signature',padx=5,pady=5,height=int(appHeight/3))
    rightPane.add(currentFrame)

app.mainloop()

添加每個窗格時,您需要為字符串“always”定義stretch選項。

rightPane.add(depthFrame, stretch="always")
rightPane.add(velocityFrame, stretch="always")
rightPane.add(currentFrame, stretch="always")

stretch接受以下值:

  • 始終此窗格將始終伸展。
  • first僅當此窗格是第一個窗格(最左側或最頂部)時,它才會拉伸。
  • 僅當此窗格是最后一個窗格(最右側或最底部)時,它才會拉伸。 這是默認值。
  • 當此窗格不是第一個或最后一個窗格時,它才會拉伸。
  • never此窗格永遠不會拉伸。

此信息來自規范 tcl/tk 文檔的paneconfigure部分。

您將需要設置窗扇 position。 我已經編寫了一些代碼來使右側的所有窗格在右側窗格大小發生變化時都相等。

import tkinter as tk

if __name__ == '__main__':
    appHeight=720
    aspectRatio=16/9
    appWidth=int(appHeight*aspectRatio)
    app=tk.Tk()
    app.title("Title")
    app.minsize(width=appWidth,height=appHeight)
    #================================================================================
    # Setup Panes
    #================================================================================
    #Create Left Pane for settings
    leftPane = tk.PanedWindow(bd=4,relief='raised',bg='red')
    leftPane.pack(fill=tk.BOTH,expand=1)
    #Add Settings Frame to Pane
    settingsFrame = tk.LabelFrame(leftPane, text='Signature Filters',padx=5,pady=5,width=int(appWidth/4))
    leftPane.add(settingsFrame)

    #Create Right Pane for charts
    rightPane = tk.PanedWindow(leftPane, orient=tk.VERTICAL, bd=4, relief=tk.SUNKEN, bg='blue')

    leftPane.add(rightPane)
    #Add Depth Frame to Pane
    depthFrame = tk.LabelFrame(rightPane, text='Depth Signature', padx=5, pady=5, height=int(appHeight/3))
    rightPane.add(depthFrame)
    #Add Velocity Frame to Pane
    velocityFrame = tk.LabelFrame(rightPane, text='Velocity Signature', padx=5, pady=5, height=int(appHeight/3))
    rightPane.add(velocityFrame)
    #Add Depth Frame to Pane
    currentFrame = tk.LabelFrame(rightPane, text='Current Signature', padx=5, pady=5, height=int(appHeight/3))
    rightPane.add(currentFrame)

    def set_sash(_):
        size = rightPane.winfo_height()//3
        for idx in range(len(rightPane.panes())-1):
            rightPane.sash_place(idx, 4, (idx+1)*size)
    rightPane.bind('<Configure>', set_sash)

app.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM