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 61 62 63 64 65
| import tkinter as tk
class AutoScrollbar(tk.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()
tk.Scrollbar.set(self, lo, hi)
class MaFrame(tk.Frame) :
def __init__(self, parent) :
self._vscrollbar = AutoScrollbar(parent)
self._vscrollbar.grid(row=0, column=1, sticky=tk.NS)
self._hscrollbar = AutoScrollbar(parent, orient=tk.HORIZONTAL)
self._hscrollbar.grid(row=1, column=0, sticky=tk.EW)
self._can = tk.Canvas(parent, bg='red',
yscrollcommand=self._vscrollbar.set,
xscrollcommand=self._hscrollbar.set)
self._can.grid(row=0, column=0, sticky=tk.NSEW)
self._vscrollbar.config(command=self._can.yview)
self._hscrollbar.config(command=self._can.xview)
super().__init__(self._can)
self._can.create_window(0, 0, anchor=tk.NW, window=self, state=tk.HIDDEN)
self.bind('<Configure>', self._actualiserDimension)
def _actualiserDimension(self, evt) :
self._can.config(scrollregion=self._can.bbox("all"))
def grid(self, **dargs) :
''' Ne pas grid la Frame '''
pass
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
frame = MaFrame(root)
frame2 = MaFrame(root)
def creer():
for i in range(11, 20):
button = tk.Button(frame, padx=7, pady=7, text="[%d]" % i)
button.grid(row=i, column=0, sticky='news')
def continuer():
frame._can.create_window(0, 0, anchor=tk.NW, window=frame, state=tk.HIDDEN)
frame2._can.create_window(0, 0, anchor=tk.NW, window=frame2, state=tk.NORMAL)
button = tk.Button(frame, padx=7, pady=7, text="creer bouton", command=creer)
button.grid(row=00, column=0, sticky='news')
button2 = tk.Button(frame, padx=7, pady=7, text="next", command=continuer)
button2.grid(row=0, column=1, sticky='news')
button3 = tk.Button(frame2, padx=7, pady=7, text="coucou")
button3.grid(row=0, column=0, sticky='news')
frame._can.create_window(0, 0, anchor=tk.NW, window=frame, state=tk.NORMAL)
root.mainloop() |
Partager