1
使用Tkinter for 2.7.5我想在下面的代碼中添加一個滾動條,以便可以查看所有的按鈕。如何添加一個滾動條到下面的代碼? (Tkinter爲2.7.5)
謝謝。
代碼:
import Tkinter, tkMessageBox root = Tkinter.Tk() def centerRoot(w = 240, h = 498): ws = root.winfo_screenwidth() hs = root.winfo_screenheight() x = (ws/2) - (w/2) y = (hs/2) - (h/2) root.geometry('%dx%d+%d+%d' % (w, h, x, y)) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=0,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=0,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=1,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=1,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=2,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=2,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=3,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=3,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=4,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=4,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=5,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=5,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=6,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=6,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=7,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=7,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=8,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=8,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=9,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=9,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=10,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=10,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=11,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=11,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=12,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=12,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=13,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=13,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=14,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=14,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=15,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=15,column=1) centerRoot() root.title('Title Here') root.mainloop()
查看這些 - 1. http://www.tutorialspoint.com/python/tk_scrollbar.htm&2. http://effbot.org/tkinterbook /scrollbar.htm 並自己嘗試! :) –
我也建議使用['for' loop](http://docs.python.org/2/tutorial/controlflow.html?highlight=loop#for-statements)來創建所有這些按鈕。 – martineau
我如何在for循環中執行它?請舉個例子? – KingMak