【问题标题】:Scrollbar in Tkinter Notebook framesTkinter Notebook 框架中的滚动条
【发布时间】:2019-09-22 02:46:26
【问题描述】:

我的滚动条无法正确显示在我的 tkinter 笔记本中。我需要为每一页滚动

root = tk.Tk()
root.title('Client Intake Form')

n = ttk.Notebook(root)
f1 = ttk.Frame(n)
f2 = ttk.Frame(n)
f3 = ttk.Frame(n)
n.add(f1, text='Page 1', compound=tk.TOP)
n.add(f2, text='Page 2', compound=tk.TOP)
n.add(f3, text='Page 3', compound=tk.TOP)
n.pack()

canvas = tk.Canvas(f1, width=1000, height=1000, relief='raised')
canvas.pack()

scroll = tk.Scrollbar(canvas, command=canvas.yview, orient=tk.VERTICAL)
canvas.config(yscrollcommand=scroll.set, scrollregion=(1000,0,1000,1000))
scroll.pack(side=tk.RIGHT, fill=tk.Y, expand=True)

#######PAGE 2#######
page2 = tk.Canvas(f2, width=1000, height=1000, relief='raised')
page2.pack()

######PAGE 3######
page3 = tk.Canvas(f3, width=1000, height=1000, relief='raised')
page3.pack()

root.mainloop()

我想要每个页面右侧的滚动条。

【问题讨论】:

  • 如果你想让它出现在所有页面上,为什么要把它放在画布里?
  • 哦,我只是不知道怎么做,期间。或者放在哪里?但是,是的,我希望它出现在所有页面上。

标签: python canvas tkinter scrollbar


【解决方案1】:

问题:我的滚动条无法正确显示在我的 tkinter 笔记本中。

  • 我想要每个页面右侧的滚动条。
    使用.pack 管理器是可能的,但如果你使用.grid 管理器,你会得到更简单的布局。

  • 我需要为每一页滚动
    与其重复自己,不如定义自己的小部件并在所有Notebook 标签上重复使用它。


  • 通过继承形式(tk.Frame)定义一个class YScrolledFrame
    这个Frame 增长到parent 小部件允许的大小。
    要表现Scrollbar 动态,您必须使用.bind('<Configure>', ...

    class YScrolledFrame(tk.Frame):
        def __init__(self, parent, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
            self.grid_rowconfigure(0, weight=1)
            self.grid_columnconfigure(0, weight=1)
    
            self.canvas = canvas = tk.Canvas(self, bg='white', relief='raised')
            canvas.grid(row=0, column=0, sticky='nsew')
    
            scroll = tk.Scrollbar(self, command=canvas.yview, orient=tk.VERTICAL)
            canvas.config(yscrollcommand=scroll.set)
            scroll.grid(row=0, column=1, sticky='nsew')
    
            self.content = tk.Frame(canvas)
            self.canvas.create_window(0, 0, window=self.content, anchor="nw")
    
            self.bind('<Configure>', self.on_configure)
    
        def on_configure(self, event):
            bbox = self.content.bbox('ALL')
            self.canvas.config(scrollregion=bbox)
    
  • 通过继承形式(ttk.Notebook)定义class Notebook
    这允许通过label text 访问Notebook tab

    class Notebook(ttk.Notebook):
        def __init__(self, parent, tab_labels):
            super().__init__(parent)
    
            self._tab = {}
            for text in tab_labels:
                self._tab[text] = YScrolledFrame(self)
                # layout by .add defaults to fill=tk.BOTH, expand=True
                self.add(self._tab[text], text=text, compound=tk.TOP)
    
        def tab(self, key):
            return self._tab[key].content
    

用法

import tkinter as tk
import tkinter.ttk as ttk

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        notebook = Notebook(self, ['Page 1', 'Page 2', 'Page 3'])
        notebook.grid(row=0, column=0, sticky='nsew')

        # Fill content, to see scroll action
        tab = notebook.tab('Page 1')
        for n in range(20):
            label = tk.Label(tab, text='Page 1 - Label {}'.format(n))
            label.grid()

if __name__ == '__main__':
    App().mainloop()

用 Python 测试:3.5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 2019-04-15
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多