from
tkinter
import
*
class
AutoScrollbar(Scrollbar):
def
set
(
self
, low, high):
if
float
(low) <
=
0.0
and
float
(high) >
=
1.0
:
self
.tk.call(
"grid"
,
"remove"
,
self
)
else
:
self
.grid()
Scrollbar.
set
(
self
, low, high)
def
pack(
self
,
*
*
kw):
raise
(TclError,"pack cannot be used with \
this widget")
def
place(
self
,
*
*
kw):
raise
(TclError, "place cannot be used with \
this widget")
root
=
Tk()
verscrollbar
=
AutoScrollbar(root)
verscrollbar.grid(row
=
0
, column
=
1
,
sticky
=
N
+
S)
horiscrollbar
=
AutoScrollbar(root,
orient
=
HORIZONTAL)
horiscrollbar.grid(row
=
1
, column
=
0
,
sticky
=
E
+
W)
canvas
=
Canvas(root,
yscrollcommand
=
verscrollbar.
set
,
xscrollcommand
=
horiscrollbar.
set
)
canvas.grid(row
=
0
, column
=
0
, sticky
=
N
+
S
+
E
+
W)
verscrollbar.config(command
=
canvas.yview)
horiscrollbar.config(command
=
canvas.xview)
root.grid_rowconfigure(
0
, weight
=
1
)
root.grid_columnconfigure(
0
, weight
=
1
)
frame
=
Frame(canvas)
frame.rowconfigure(
1
, weight
=
1
)
frame.columnconfigure(
1
, weight
=
1
)
rows
=
20
for
i
in
range
(
1
,rows):
for
j
in
range
(
1
,
9
):
button
=
Button(frame, padx
=
8
, pady
=
8
,
text
=
"[%d,%d]"
%
(i,j))
button.grid(row
=
i, column
=
j, sticky
=
'news'
)
canvas.create_window(
0
,
0
, anchor
=
NW, window
=
frame)
frame.update_idletasks()
canvas.config(scrollregion
=
canvas.bbox(
"all"
))
root.mainloop()