我需要在 tkinter 库中为 Frame 创建一个滚动条。
我尝试为 Frame 设置 yview
scrollbar = Scrollbar(master, command=frame.yview)
但框架没有 yview 方法。
我也尝试为滚动条设置 yscrollcommand,但它不起作用
请帮助我。
你可以做什么:
创建一个画布并使其可滚动。正如acw1668所说。
我为此创建了一个课程
import tkinter as tk
class Scroll_Frame(tk.Frame):
""" frame_options is the scrollable frame"""
def __init__(self, master=None, bg="grey22", height=300, width=300, *args, **kwargs):
self.master = master
self.height_ = height
self.width_ = width
self.background = bg
tk.Frame.__init__(self, self.master)
self.grid()
self.rowconfigure((0), weight=1)
self.columnconfigure((0), weight=1)
self.flag_focus_canvas = False
self.canvas_scroll = tk.Canvas(self, bg=self.background, height=self.height_, width=self.width_, *args, **kwargs)
self.canvas_scroll.grid(row=0, column=0, sticky="wesn")#, padx=20, pady=20)
self.scrollbar_v = tk.Scrollbar(self, orient="vertical",
command=self.canvas_scroll.yview)
self.scrollbar_v.grid(row=0, column=2, sticky="ns")
self.canvas_scroll.configure(yscrollcommand=self.scrollbar_v.set)
self.scrollbar_h = tk.Scrollbar(self, orient="horizontal",
command=self.canvas_scroll.xview)
self.scrollbar_h.grid(row=1, column=0, sticky="we")
self.canvas_scroll.configure(xscrollcommand=self.scrollbar_h.set)
self.frame_options = tk.Frame(self.canvas_scroll, bg=self.background, *args, **kwargs)
self.frame_options.grid(row=0, column=0, sticky="wesn")#, padx=20, pady=20)
self.frame_options.bind("<Configure>",
lambda e: self.canvas_scroll.configure(scrollregion=self.canvas_scroll.bbox("all")))
self.canvas_scroll.create_window(0,0, window=self.frame_options, anchor="nw", tags="scrollable")
self.canvas_scroll.bind_all("<Button-4>", lambda e: self._on_mousewheel(e))
self.canvas_scroll.bind_all("<Button-5>", lambda e: self._on_mousewheel(e))
self.canvas_scroll.bind("<Enter>", lambda e: self.set_focus_scroll(e))
self.canvas_scroll.bind("<Leave>", lambda e: self.unset_focus_scroll(e))
def set_focus_scroll(self, event):
self.flag_focus_canvas = True
def unset_focus_scroll(self, event):
self.flag_focus_canvas = False
def _on_mousewheel(self, event):
if event.num == 4:
if self.flag_focus_canvas:
self.canvas_scroll.yview_scroll(int(-1), "units")
if event.num == 5:
if self.flag_focus_canvas:
self.canvas_scroll.yview_scroll(int(1), "units")
def get_scroll_frame(self):
"""returns the real scroll frame,
this is the place you put all your widgets in"""
return self.frame_options
if __name__ == "__main__":
root = tk.Tk()
app = Scroll_Frame(root)
scroll_frame = app.get_scroll_frame()
for i in range(20):
tk.Button(scroll_frame, text=f"Button {i}").grid(sticky="we")
root.mainloop()
现在你可以获取框架scroll_frame = app.get_scroll_frame()并放入你想要的任何内容,出于演示目的,我放置了按钮。
享受