Tkinter笔记本框架中的滚动条

时间:2019-09-22 02:46:26

标签: python canvas tkinter scrollbar

我无法在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()

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

1 个答案:

答案 0 :(得分:0)

  

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

  • 我希望滚动条位于每页的右侧。
    可以使用.pack管理器,但是如果改用.grid管理器,则可以简化所需的布局。

  • 每页我都需要滚动
    定义自己的小部件并在所有Notebook标签上重复使用,而不是重复自己


  • 通过继承格式class YScrolledFrame来定义(tk.Frame)
    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)
    
  • 通过继承格式class Notebook来定义(ttk.Notebook)
    这允许通过Notebook tab访问label text

    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