forked from KenT2/tboplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrolledframe.py
53 lines (42 loc) · 2.27 KB
/
scrolledframe.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from tkinter import (Frame, Scrollbar, Canvas)
# https://gist.github.com/EugeneBakin/76c8f9bcec5b390e45df
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 _configure_interior(self, event):
# update the scrollbars to match the size of the inner frame
size = (self.interior.winfo_reqwidth(),
self.interior.winfo_reqheight())
self.canvas.config(scrollregion="0 0 %s %s" % size)
if self.interior.winfo_reqwidth() != self.canvas.winfo_width():
# update the canvas's width to fit the inner frame
self.canvas.config(width=self.interior.winfo_reqwidth())
self.interior.bind('<Configure>', _configure_interior)
def _configure_canvas(self, event):
if self.interior.winfo_reqwidth() != self.canvas.winfo_width():
# update the inner frame's width to fill the canvas
self.canvas.itemconfigure(
self.interior_id, width=self.canvas.winfo_width())
self.canvas.bind('<Configure>', _configure_canvas)
return
def configure_scrolling(self):
# create a canvas object and a vertical scrollbar for scrolling it
vscrollbar = Scrollbar(self, orient='vertical')
vscrollbar.grid(row=0, column=1, sticky='nsw')
self.canvas = Canvas(self, bd=0, highlightthickness=0,
yscrollcommand=vscrollbar.set)
self.canvas.grid(row=0, column=0, sticky='nsew')
vscrollbar.config(command=self.canvas.yview)
# reset the view
self.canvas.xview_moveto(0)
self.canvas.yview_moveto(0)
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = Frame(self.canvas)
self.interior.grid(row=0, column=0, sticky='nsew')
self.interior_id = self.canvas.create_window(0, 0, window=interior,
anchor='nw')
# track changes to the canvas and frame width and sync them,
# also updating the scrollbar