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
| 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:
self.grid_remove()
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")
root = Tk()
frame = Frame(root, bd=2, relief=SUNKEN)
## frame.grid_rowconfigure(0, weight=1)
## frame.grid_columnconfigure(0, weight=1)
## xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
## xscrollbar.grid(row=1, column=0, sticky=E+W)
yscrollbar = AutoScrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)
text = Text(frame, #wrap=NONE, bd=0,
## xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set,)
text.grid(row=0, column=0, sticky=N+S+E+W)
## xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
frame.pack()
mainloop() |
Partager