画布内部的tkinter框架未扩展到填充区域

时间:2020-09-16 19:44:28

标签: python tkinter

我有一个可滚动的框架类,我从发现的一些代码中借来了,但是在调整它以适应我的需求时遇到了麻烦。它是由.pack()管理的,但是我需要使用.grid(),因此我只是将一个框架(self.region)打包到其中,以便可以在其中嵌入小部件。但是,此框架内的小部件将不会扩展以适应容器的边缘,我不确定为什么。那里有很多与我类似的问题,但是似乎所有解决方案都无济于事。我尝试全部使用.grid_columnconfigure.columnconfigure().bind("Configure")无济于事。有没有人建议将小部件放在我的可滚动区域中以向东和向西扩展以填充窗口?

import tkinter as tk
from tkinter import ttk

class ScrollableFrame(ttk.Frame):
    """taken from https://blog.tecladocode.com/tkinter-scrollable-frames/ and modified to
    allow for the use of grid inside self.region
    Class that allows for the creation of a frame that is scrollable"""
    def __init__(self, container, *args, **kwargs):
        super().__init__(container, *args, **kwargs)
        canvas = tk.Canvas(self)
        scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
        self.scrollable_frame = ttk.Frame(canvas)
        canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
        canvas.configure(yscrollcommand=scrollbar.set)
        canvas.pack(side="left", fill="both", expand=True)
        canvas.rowconfigure(0, weight=1)
        canvas.columnconfigure(0, weight=1)
        scrollbar.pack(side="right", fill="y")
        self.scrollable_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(
                scrollregion=canvas.bbox("all")
            )
        )
        self.scrollable_frame.rowconfigure(0, weight=1)
        self.scrollable_frame.columnconfigure(0, weight=1)
        self.region=ttk.Frame(self.scrollable_frame)
        self.region.pack(fill='both', expand=1)
        self.region.grid_rowconfigure(0, weight=1)
        self.region.grid_columnconfigure(0, weight=1)

class OtherWindow():
    """data collector object and window"""
    def __init__(self, window):
            self.window=tk.Toplevel(window)
            self.window.grid_columnconfigure(0, weight=1)
            self.window.grid_columnconfigure(1, weight=1)
            self.window.grid_columnconfigure(2, weight=1)
            self.window.grid_columnconfigure(3, weight=1)
            self.window.grid_rowconfigure(3, weight=1)
            self.lbl1=ttk.Label(self.window, text="this is a sort of long label")
            self.lbl1.grid(row=0, column=0, columnspan=2)
            self.lbl2=ttk.Label(self.window, text="this is another label")
            self.lbl2.grid(row=0, column=2)
            self.lbl3=ttk.Label(self.window, text="Other information: blah blah blah blah")
            self.lbl3.grid(row=0, column=3)
            self.directions=ttk.Label(self.window, text='These are instructions that are kind of long and take'+\
'up about this much space if I had to guess so random text random text random text', wraplength=700)
            self.directions.grid(row=1, column=0, columnspan=4)
            self.scrolly=ScrollableFrame(self.window)
            self.scrolly.grid(row=2, column=0, columnspan=4,sticky='nsew')
            self.frame=self.scrolly.region
            self.fillScrollRegion()
            self.continueBtn=ttk.Button(self.window, text="Do Something", command=self.do)
            self.continueBtn.grid(row=3, column=0, columnspan=4, sticky='nsew')

    def fillScrollRegion(self):
        """fills scrollable region with label widgets"""
        for i in range(15):
            for j in range(5):
                lbl=ttk.Label(self.frame, text="Sample text"+str(i)+' '+str(j))
                lbl.grid(row=i, column=j, sticky='nsew')

    def do(self):
        pass

root=tk.Tk()
app=OtherWindow(root)
root.mainloop()

enter image description here

1 个答案:

答案 0 :(得分:1)

问题是滚动框容器Get-ChildItem没有水平填充Get-Item * -Include (Get-Content filenames.txt) 。不用费心去修复一些复制/粘贴(例如滚动框架)并进行说明,我只给您我的滚动框架。它比您所使用的坚固得多,并且它所存在的问题并不存在。我已经将其插入下面的脚本版本中。

可在我的-Force方法中找到解决滚动框问题的方法。它只是告诉画布Frame事件中的容器框架与画布的宽度相同。

enter image description here

Canvas