【问题标题】:expand scrollable ttk.notebook in python3 / gui tkinter在 python3 / gui tkinter 中展开可滚动的 ttk.notebook
【发布时间】:2019-06-17 16:13:15
【问题描述】:

用内部的 ttk.notebook 编写可滚动框架。 但是滚动条/笔记本有固定的大小。怎么改?

from tkinter import Canvas, Scrollbar, Button, Tk
from tkinter.ttk import Frame, Notebook

class VerticalScrolledFrame(Frame):
    """A pure Tkinter scrollable frame that actually works!
    * Use the 'interior' attribute to place widgets inside the scrollable frame
    * Construct and pack/place/grid normally
    * This frame only allows vertical scrolling

    """
    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)            

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = Scrollbar(self, orient='vertical')
        vscrollbar.pack(fill='y', side='right', expand='false')
        canvas = Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set)
        canvas.pack(side='left', fill='both', expand='true')
        vscrollbar.config(command=canvas.yview)

        # reset the view
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor='nw')

        # track changes to the canvas and frame width and sync them,
        # also updating the scrollbar
        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

root = Tk()

class Overview:

    def __init__(self):

        #mainframe to make a scrollable window
        self.mainframe = VerticalScrolledFrame(root)
        self.mainframe.grid(row=0, column=0)

        # create a notebook  
        self.TNotebook_Overview = Notebook(self.mainframe.interior)        
        self.TNotebook_Overview.grid(row=0, column=0)
        self.TNotebook_Overview.configure(takefocus="")     

        self.Frame_Overview = Frame(self.TNotebook_Overview) 

        self.TNotebook_Overview.add(self.Frame_Overview)    
        self.TNotebook_Overview.tab(0, text="Table", compound="left",underline="-1", )

        buttons = []
        for i in range(30):
            buttons.append(Button(self.Frame_Overview, text="Button " + str(i)))
            buttons[-1].grid(column=0, row=i)


if __name__ == "__main__":

    ov = Overview()
    root.title('Overview Items Database')
    root.geometry('800x800+10+10')
    root.configure(background="#4C7274")
    root.grab_set()
    root.mainloop() 

我希望一个扩展的可滚动笔记本/框架填充整个 Tk() 窗口。

因为stackoverflow的“ai”不允许使用其他线程的代码,这里是代码https://pastebin.com/ykJGViAz

【问题讨论】:

  • 请修正代码格式。
  • 我尝试格式化代码,但我不能,因为我使用了其他帖子中的太多代码。所以我不能编辑它。 class VerticalScrolledFrame(Frame): 是从另一个线程完成的,太多了,所以我无法编辑它

标签: python-3.x tkinter window scrollable


【解决方案1】:

在 init 中,改为执行此操作 self.mainframe.grid(row=0, column=0, sticky="nsew") self.TNotebook_Overview.grid(row=0, column=0, sticky="nsew") 这告诉框架根据需要填充尽可能多的空间

【讨论】:

  • 它不会改变笔记本的大小。
  • 尝试“nesw”而不是“nsew”
猜你喜欢
  • 2021-06-13
  • 2012-04-03
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 2020-07-16
  • 1970-01-01
相关资源
最近更新 更多