Tkinter 可滚动和可扩展的 PanedWindow

问题描述

我已经在 Stackoverflow 上搜索了很多,还有很多博客、文档......而且我没有找到使“PanedWindow”可滚动和展开 + 填充的方法。

检查这个例子

"""Test"""
# pylint: disable=unused-wildcard-import,wildcard-import
from tkinter import Canvas,Scrollbar,Tk,Wm
from tkinter.constants import BOTH,CENTER,HORIZONTAL,LEFT,NSEW,X
from tkinter.ttk import Button,Frame,Label,PanedWindow,Style

STYLE = {
    "TButton": {
        "configure": {
            "background": "#333333","foreground": "white","padding": 8,"relief": "flat","border": 2,},"map": {
            "background": [("active","#777777")],"foreground": [("active","white")],}


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

        scrollbar = Scrollbar(self,orient="vertical")
        scrollbar.pack(side="right",fill="y")

        canvas = Canvas(self,borderwidth=2,background="red")
        canvas.pack(side=LEFT,fill=BOTH,expand=True)
        canvas.configure(yscrollcommand=scrollbar.set)

        scrollbar.configure(command=canvas.yview)
        frame = Frame(canvas,style="DEBUG.TFrame")

        canvas.create_window((0,0),window=frame,anchor=CENTER)

        # COMMENT THIS,and the paned is scrollable,but frame does NOT expand
        # frame.pack(expand=1,fill=BOTH)

        frame.bind("<Configure>",self.on_configure)

        self.scollbar = scrollbar
        self.canvas = canvas
        self.frame = frame

    def on_configure(self,event):
        self.canvas.configure(
            scrollregion=self.canvas.bbox("all"),)


def create_right_pane(master):
    frame = Frame(master)
    label = Label(frame,text="This is a cool text")
    label.pack()
    return frame


def create_left_pane(master):
    frame = ScrollableFrame(master)
    title = Label(frame.frame,text="Hello !!!")
    title.pack(expand=True,anchor=CENTER)
    for i in range(100):
        Button(frame.frame,text=f"Channel {i} ❱").pack(expand=True,fill=BOTH)
    return frame


app = Tk(className="MyApp")
style = Style()
style.theme_create("app",None,STYLE)
style.theme_use("app")
app.title("MyApp")
app.geometry("1120x550")

splitpane = PanedWindow(app,orient=HORIZONTAL)
splitpane.pack(expand=True,fill=BOTH)

leftframe = create_left_pane(splitpane)
rightframe = create_right_pane(splitpane)

splitpane.add(leftframe,weight=1)
splitpane.add(rightframe,weight=5)

app.mainloop()

ScrollableFrame 类中有注释

  • 评论 frame.pack() => 它滚动,但内容没有展开,也没有填充 X

    enter image description here

  • 取消对 frame.pack() 的注释 => 现在内容填满了空间,但它不可滚动

    enter image description here

我想做的只是让内容“填满空间”并“可滚动”。

如果您发现了问题,并且您可以解决它(或提供解决方案)...谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)