python-3.x - 如何在画布中滚动多个帧?
问题描述
我想创建一个具有更多功能的框架列表,例如按钮、标签等。但我的问题是 LabelFrame 的大小。如果我将 LabelFrame 放入容器中,它就像我想要的那样适合,但它不再可滚动。有任何想法吗?
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
container = ttk.Frame(root)
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
for i in range(50):
lf = ttk.Frame(scrollable_frame, text=i).grid(column=1, row=i)
frame_ip = tk.LabelFrame(lf, bg="white", text=i)
frame_ip.place(relwidth=0.95, relheight=0.2, relx=0.025, rely=0)
button_scroll1 = tk.Button(frame_ip, text="Start", bg="grey")
button_scroll1.place(relwidth=0.15, relx=0.025, relheight=0.15, rely=0.1)
container.pack()
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
root.mainloop()
解决方案
这是带有 , 的更新代码Button
,Canvas
并使用网格管理器Scrollbar
插入到每个代码中。LabelFrame
我还container
使用行|列配置调整大小。
似乎工作正常。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
root.geometry("341x448")
container = ttk.Frame(root)
container.rowconfigure(0, weight = 1)
container.columnconfigure(0, weight = 1)
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient = tk.VERTICAL, command = canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")))
canvas.create_window((0, 0), window = scrollable_frame, anchor = tk.NW)
canvas.configure(yscrollcommand = scrollbar.set)
for i in range(15):
L = ttk.LabelFrame(scrollable_frame, text = "Sample scrolling label")
L.grid(row = i, column = 0, sticky = tk.NSEW)
B = ttk.Button( L, text = f"Button {i}")
B.grid(row = 0, column = 0, sticky = tk.NW)
K = tk.Canvas(
L, width = 300, height = 100,
scrollregion = "0 0 400 400", background = "#ffffff")
K.grid(row = 1, column = 0, sticky = tk.NSEW)
S = ttk.Scrollbar( L, command = K.yview)
S.grid(column = 1, row = 1, sticky = tk.NSEW)
K["yscrollcommand"] = S.set
container.grid(row = 0, column = 0, sticky = tk.NSEW)
canvas.grid(row = 0, column = 0, sticky = tk.NSEW)
scrollbar.grid(row = 0, column = 1, sticky = tk.NS)
root.mainloop()
推荐阅读
- amazon-redshift - 使用 Glue 将数据从 AWS EMR 加载到 Redshift 非常慢
- tensorflow - 字符串获取方法的terraform列表失败
- javascript - 更新 mongoose 中的嵌入文档
- python - ChromeDriver 错误:“chromedriver”可执行文件需要在 PATH 中
- qt - 是否可以在 QML 中仅显示 osm 的一部分?
- ansible - 如何在 cygwin 下配置 ansible 以使用 pageant(使用密码保护密钥运行 ansible)
- python - 如何格式化分区问题的幸福来读取幸福表?
- flutter - 无法更改颤振主题颜色
- raku - Raku vs. Perl5,出人意料的结果
- javascript - React-redux 中的下拉状态