我剛剛開始使用Python的Tkinter
/ttk
,我在使用網格佈局時遇到問題,無法調整我的小部件的大小。這是我的代碼的一個子集,展示與完整代碼相同的問題(我意識到這個子集非常簡單,我可能會更好使用pack
而不是grid
,但我認爲這將有助於切入主要問題有一次我明白,我可以修復它無處不在我的全程序中出現):Python:tk入門,窗口小部件沒有在網格上調整大小?
import Tkinter as tk
import ttk
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
# Create my widgets
self.tree = ttk.Treeview(self)
ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text='Path', anchor='w')
# Populate the treeview (just a single root node for this simple example)
root_node = self.tree.insert('', 'end', text='Test', open=True)
# Lay it out on a grid so that it'll fill the width of the containing window.
self.tree.grid(row=0, column=0, sticky='nsew')
self.tree.columnconfigure(0, weight=1)
ysb.grid(row=0, column=1, sticky='nse')
xsb.grid(row=1, column=0, sticky='sew')
self.grid()
master.columnconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
app = App(tk.Tk())
app.mainloop()
我想讓它讓我的樹視圖填補它在窗口的整個寬度,而是樹視圖正好在窗戶中間居中。
我猜你的意思是在第一行輸入「Tkinter as tk」? – Kevin 2014-09-19 18:38:08
@凱文 - 對。有趣的事情:我手動編寫了前3行代碼,之後纔想到「我最好只是複製並粘貼以避免犯錯誤」,然後複製並粘貼剩下的部分。 – ArtOfWarfare 2014-09-19 19:45:23