【问题标题】:Tkinter: PanedWindow not stretching within FramesTkinter:PanedWindow 不在框架内拉伸
【发布时间】:2021-09-28 07:55:20
【问题描述】:

我正在尝试使用 Frames 和 PanedWindows 在主窗口中创建两个“页面”。 “Main”类使用由两个页面继承的 PanedWindow。然后它们被初始化为由 PanedWindow(s) 或 Frame(s) 组成的 Frames。问题是它们不会伸展。尽管已添加窗扇,但它们都没有窗扇 (sashwidth=50)。

我已经检查了 stackoverflow,但没有发现任何相关内容。有人可以向我解释为什么会这样吗?

import tkinter as tk
from tkinter import ttk
class Main(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Program")

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

        container = tk.PanedWindow(self, bg='bisque',orient=tk.HORIZONTAL, relief='groove', borderwidth=2, sashwidth=3)
        container.grid(row=0, column=0)

        self.frames = {}        

        main_page = MainPage(container, self)
        self.frames[0] = main_page
        main_page.grid(row=0, column=0, sticky="nsew")

        plotting_page = PlottingPage(container, self)
        self.frames[1] = plotting_page
        plotting_page.grid(row=0, column=1, sticky="nsew")
    
class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # Paned Windows
        top_pane = tk.PanedWindow(self, orient=tk.HORIZONTAL, relief='groove', borderwidth=2, sashwidth=3)
        top_pane.grid(column=0, row=1, sticky="nsew", columnspan=4)
        top_pane.rowconfigure(0, weight=1)
        top_pane.columnconfigure(0, weight=1)
        top_pane.rowconfigure(1, weight=1)
        top_pane.columnconfigure(1, weight=1)

        bottom_pane = tk.PanedWindow(self, orient=tk.VERTICAL, relief='groove', borderwidth=2, sashwidth=50)
        bottom_pane.grid(column=0, row=2, sticky="nsew", columnspan=2)
        bottom_pane.rowconfigure(0, weight=1)
        bottom_pane.columnconfigure(0, weight=1)
        bottom_pane.rowconfigure(1, weight=1)
        bottom_pane.columnconfigure(1, weight=1)

        # Frames      
        top_frame = ttk.LabelFrame(self, text="Top frame")
        bottom_frame = ttk.LabelFrame(self, text="Bottom frame")

        # Layout of frames
        top_frame.grid(row=1, column=0, sticky="nsew")
        bottom_frame.grid(row=2, column=0, sticky="ew")

        top_frame.columnconfigure(0, weight=1)
        top_frame.columnconfigure(1, weight=1)
        top_frame.rowconfigure(0, weight=1)
        top_frame.rowconfigure(1, weight=1)
        top_pane.add(top_frame, stretch="always")

        bottom_frame.columnconfigure(0, weight=1)
        bottom_frame.columnconfigure(1, weight=1)
        bottom_frame.rowconfigure(0, weight=1)
        bottom_frame.rowconfigure(1, weight=1)
        bottom_pane.add(bottom_frame, stretch="always")

    
        top = tk.Label(top_frame, text="top pane")
        top.grid(column=1, row=0, sticky="nsew")

        bottom = tk.Label(bottom_frame, text="bottom pane")
        bottom.grid(column=1, row=0, sticky="nsew")

        

class PlottingPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # Paned Windows
        right_pane = tk.PanedWindow(self, orient=tk.HORIZONTAL, relief='groove', borderwidth=2, sashwidth=3)
        right_pane.grid(column=2, row=1, sticky="nsew", columnspan=4)
        right_pane.rowconfigure(0, weight=1)
        right_pane.columnconfigure(0, weight=1)
        right_pane.rowconfigure(1, weight=1)
        right_pane.columnconfigure(1, weight=1)

        # Frames      
        right_frame = ttk.LabelFrame(self, text="Right frame")

        # Layout of frames
        right_frame.grid(row=1, column=2, sticky="nsew")

        right_frame.columnconfigure(0, weight=1)
        right_frame.columnconfigure(1, weight=1)
        right_frame.rowconfigure(0, weight=1)
        right_frame.rowconfigure(1, weight=1)
        right_pane.add(right_frame, stretch="always")

        # Widgets
        right = tk.Label(right_frame, text="right pane")
        right.grid(column=1, row=1, sticky="nsew")

def main():
    app = Main()
    app.mainloop()

if __name__ == '__main__':
    main()

【问题讨论】:

  • 真的吗?没人知道吗?

标签: python tkinter


【解决方案1】:

问题是,我没有配置所有列和行。您应该为该“跨度”内的每一行和每一列使用 columnconfigure、rowconfigure。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多