Python:开始使用tk,小部件不在网格上调整大小?

2024-09-26 22:45:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用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()

我想让树状视图填充它所在窗口的整个宽度,但是树状视图只是在窗口的中间居中。在


Tags: theselfmastertreetkintercolumntkgrid
1条回答
网友
1楼 · 发布于 2024-09-26 22:45:10

执行self.grid时,请尝试指定sticky参数。没有它,当窗口调整大小时,框架不会调整大小。你还需要rowconfigure掌握和自我,就像你有columnconfigured他们一样。在

    #rest of code goes here...
    xsb.grid(row=1, column=0, sticky='sew')
    self.grid(sticky="nesw")
    master.columnconfigure(0, weight=1)
    master.rowconfigure(0,weight=1)
    self.columnconfigure(0, weight=1)
    self.rowconfigure(0, weight=1)

或者,不是网格化框架,pack它并指定它填充它所占据的空间。因为框架是Tk中唯一的小部件,所以不管您是pack还是{}。在

^{pr2}$

相关问题 更多 >

    热门问题