我刚开始使用Python的Tkinter
/ttk
,在使用网格布局时,我在调整小部件的大小时遇到了一些问题。下面是我代码的一个子集,它显示了与完整代码相同的问题(我意识到这个子集非常简单,我可能最好使用pack
而不是{
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()
我想让树状视图填充它所在窗口的整个宽度,但是树状视图只是在窗口的中间居中。在
执行
self.grid
时,请尝试指定sticky
参数。没有它,当窗口调整大小时,框架不会调整大小。你还需要rowconfigure
掌握和自我,就像你有columnconfigure
d他们一样。在或者,不是网格化框架,}。在
^{pr2}$pack
它并指定它填充它所占据的空间。因为框架是Tk中唯一的小部件,所以不管您是pack
还是{相关问题 更多 >
编程相关推荐