我写了一个包含Label,Entry和Treeview的自定义小部件。 现在我想要那个自定义小部件来填充我想要放置的框架。我认为rowconfigure是要做的事情。 所以我的代码如下所示:
import tkinter as tk import tkinter.ttk as ttk class CustomWidget(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) tk.Frame.rowconfigure(3, weight=1) self.Search_Text_Label = tk.Label(self, text="Search:",font=("Century Gothic",12)) self.Search_Text_Label.grid(row=1,column=1, sticky='W', padx=10) self.entry = tk.Entry(self, width=30) self.entry.grid(row=1,column=2, sticky='W') self.entry.bind('<Return>',search) self.tree = ttk.Treeview(self) self.tree.state = "disabled" self.tree.grid(row=2,column=1,columnspan=2, rowspan=2, sticky='S', pady=0, padx=20)但是我得到这个错误信息:
TypeError:grid_rowconfigure()缺少1个需要的位置参数:'index'
我需要改变什么? 这真的是要走的路吗?
I wrote a custom widget which contains a Label, Entry and a Treeview. Now I want that custom widget to fill the frame I would like to place it in. I thought rowconfigure was the thing to go. So my code looks like this:
import tkinter as tk import tkinter.ttk as ttk class CustomWidget(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) tk.Frame.rowconfigure(3, weight=1) self.Search_Text_Label = tk.Label(self, text="Search:",font=("Century Gothic",12)) self.Search_Text_Label.grid(row=1,column=1, sticky='W', padx=10) self.entry = tk.Entry(self, width=30) self.entry.grid(row=1,column=2, sticky='W') self.entry.bind('<Return>',search) self.tree = ttk.Treeview(self) self.tree.state = "disabled" self.tree.grid(row=2,column=1,columnspan=2, rowspan=2, sticky='S', pady=0, padx=20)But I get this error message:
TypeError: grid_rowconfigure() missing 1 required positional argument: 'index'
What do I have to change? And is this really the way to go?
最满意答案
你在类tk.Frame上调用tk.Frame 。 相反,你应该在自己的类实例上调用它,所以:
self.rowconfigure(3, weight=1)另外,如果你想Treeview垂直拉伸,你应该使用sticky='NS' 。
You're calling rowconfigure on the class tk.Frame. Instead, you should call it on your class instance, which is self, so:
self.rowconfigure(3, weight=1)Also, if you want the Treeview to stretch vertically, you should use sticky='NS'.
更多推荐
发布评论