如何在python中访问函数外部的帧

taor4pac  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(384)

如何访问函数外部的滚动框,使新的滚动框位于下面代码中的滚动框内。

def scroll():
    container = LabelFrame(root)

    my_canvas = Canvas(container)
    my_canvas.pack(side=LEFT, fill=BOTH, expand=1)

    scroll_y = Scrollbar(container, orient=VERTICAL, command=my_canvas.yview)
    scroll_y.pack(side=RIGHT, fill='y')

    my_canvas.configure(yscrollcommand=scroll_y.set)
    my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox('all')))

    scroll_frame = Frame(my_canvas)
    my_canvas.create_window((0, 0), window=scroll_frame, anchor=NW)

    container.pack(fill=BOTH, expand=1)

new_frame = Frame(scroll_frame)

for i in range(25):
    Button(new_frame, text='click'+str(i)).pack()

scroll()

root.mainloop()
nbewdwxp

nbewdwxp1#

如果您想访问 scroll_frame ,您应该从 scroll() .
建议返回 container 也一样,不要打电话 container.pack(...) 在…内 scroll() 因为它更灵活。
还建议将家长传递给 scroll() 也不是硬编码的 root 里面。

from tkinter import *

def scroll(parent):
    container = LabelFrame(parent)

    my_canvas = Canvas(container, highlightthickness=0)
    my_canvas.pack(side=LEFT, fill=BOTH, expand=1)

    scroll_y = Scrollbar(container, orient=VERTICAL, command=my_canvas.yview)
    scroll_y.pack(side=RIGHT, fill=Y)

    my_canvas.configure(yscrollcommand=scroll_y.set)

    scroll_frame = Frame(my_canvas)
    my_canvas.create_window((0, 0), window=scroll_frame, anchor=NW)

    # bind <Configure> on scroll_frame instead of my_canvas
    scroll_frame.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox('all')))

    return container, scroll_frame

root = Tk()

container, scroll_frame = scroll(root)
container.pack(fill=BOTH, expand=1)

# Is new_frame necessary? Why not use scroll_frame directly?

new_frame = Frame(scroll_frame)
new_frame.pack()

for i in range(25):
    Button(new_frame, text='click'+str(i)).pack(fill=X)

root.mainloop()

请注意,如果要使用 scroll() 反复使用,更好地使用类而不是函数。
更新:使用类实现:

import tkinter as tk

class ScrollableFrame(tk.LabelFrame):
    def __init__(self, parent,**kw):
        super().__init__(parent,**kw)

        canvas = tk.Canvas(self, highlightthickness=0)
        canvas.pack(side="left", fill="both", expand=1)

        vscrollbar = tk.Scrollbar(self, orient="vertical", command=canvas.yview)
        vscrollbar.pack(side="right", fill="y")
        canvas.config(yscrollcommand=vscrollbar.set)

        self.scroll_frame = tk.Frame(canvas)
        self.scroll_frame.bind("<Configure>", lambda e: canvas.config(scrollregion=canvas.bbox("all")))
        canvas.create_window(0, 0, window=self.scroll_frame, anchor="nw")

if __name__ == "__main__":
    root = tk.Tk()
    container = ScrollableFrame(root)
    container.pack(fill="both", expand=1)
    for i in range(25):
        tk.Button(container.scroll_frame, text=f"click {i}").pack(fill="x")
    root.mainloop()

相关问题