【发布时间】:2021-06-13 18:33:48
【问题描述】:
我已经在 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, *args, **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, fill=BOTH, 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类中有注释
我想做的只是让内容“填满空间”并“可滚动”。
如果您发现问题,并且可以解决(或提供解决方案)...谢谢!
【问题讨论】:
标签: python tkinter tk ttk scrollable