我真的是传统知识图书馆的新人。我有以下代码,但是水平滚动条没有显示。谁能看到原因?任何帮助表示赞赏!
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
container = ttk.Frame(root)
canvas = tk.Canvas(container)
scrollbar_y = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollbar_x = ttk.Scrollbar(container, orient="horizontal", command=canvas.xview)
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_y.set, xscrollcommand=scrollbar_x.set)
for i in range(50):
ttk.Label(scrollable_frame, text="Sample scrolling label").pack()
container.pack()
canvas.pack(side="left", fill="both", expand=True)
scrollbar_y.pack(side="right", fill="y")
scrollbar_x.pack(side="bottom", fill="x")
root.mainloop()
已解决的问题:
必须替换
self.x_scrollbar = Scrollbar(self.container, orient="horizontal", command=self.canvas.xview)
与
self.x_scrollbar = Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
以便滚动条可以显示在底部,而不是被埋在框架中。