Scrollbar in Tkinter Notebook frames
我无法在tkinter笔记本中正确显示滚动条。我需要每页滚动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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() |
我想要每个页面右侧的滚动条。
Question: I cant get my scrollbar to show up correctly in my tkinter notebook.
-
我想要每个页面右侧的滚动条。
可以使用.pack 管理器,但是如果使用.grid 管理器,则可以简化所需的布局。 -
我需要每页滚动
定义自己的窗口小部件并在所有Notebook 选项卡上重复使用它,而不是重复自己。
-
通过继承形式
(tk.Frame) 定义class YScrolledFrame 。
该Frame 增长到parent 小部件允许的大小。
若要表现Scrollbar 动态,必须使用.bind('<Configure>', ... 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21class 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 。1
2
3
4
5
6
7
8
9
10
11
12class 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
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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