以下是自動隱藏滾動條如果你只是使用電網幾何經理,從effbot.org
資料爲準,只有工作的例子:
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")
# create scrolled canvas
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)
rows = 5
for i in range(1, rows):
for j in range(1, 10):
button = Button(frame, 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()
我不認爲這個問題是相關的。我剛剛爲Windows下載了Python 2.6.6,並附帶了Tix。所以,它似乎和Tkinter一樣工作。 – 2010-10-06 04:41:44
謝謝!這是一個很大的幫助。 – reckoner 2010-10-07 20:44:29
我試圖將此答案中的代碼重構爲其可重用的'class',但未成功。如果你有時間在這裏看看我的問題,我真的很感激它:http://stackoverflow.com/questions/30018148/python-tkinter-frame-class-with-autohiding-scroll-bars另外,Rinzler ,爲什麼您將此代碼作爲編輯發佈,而不是作爲其自身的答案?現在來自2010年的評論現在沒有任何意義,而且我從這個代碼中獲得的代表最終將會被計算在內,而與代碼無關。你應該發佈一個新的答案,然後回滾你的編輯。 – ArtOfWarfare 2015-05-03 19:25:10