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 54 55 56 57 58 59 60
| from tkinter import *
class AutoScrollbar(Scrollbar):
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise(TclError, "cannot use pack with this widget")
def place(self, **kw):
raise (TclError, "cannot use place with this widget")
global root,frame
root = Tk()
vscrollbar = AutoScrollbar(root)
vscrollbar.grid(row=0, column=1, sticky=N+S)
hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)
canvas = Canvas(root,
yscrollcommand=vscrollbar.set,
xscrollcommand=hscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)
# make the canvas expandable
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
#
# create canvas contents
frame = Frame(canvas)
#frame.rowconfigure(1, weight=1)
#frame.columnconfigure(1, weight=1)
def creation(frame):
for i in range(4,15):
button = Button(frame, padx=7, pady=7, text="[%d]" % i)
button.grid(row=i, column=0, sticky='news')
rows = 5
for i in range(1,rows):
for j in range(1,10):
button = Button(frame, padx=7, pady=7, text="[%d,%d]" % (i,j),command=lambda: creation(frame))
button.grid(row=i, column=j, sticky='news')
canvas.create_window(0, 0, anchor=NW, window=frame,state=NORMAL)
frame.update_idletasks()
canvas.config(scrollregion=canvas.bbox("all"))
root.mainloop() |
Partager