我正在尝试使用 Python 中的 Tkinter 编写一个简单的用户界面,但我无法让网格中的小部件调整大小。每当我调整主窗口大小时,条目和按钮小部件根本不会调整。
这是我的代码:
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, padding=(3,3,12,12))
self.grid(sticky=N+W+E+S)
self.createWidgets()
def createWidgets(self):
self.dataFileName = StringVar()
self.fileEntry = Entry(self, textvariable=self.dataFileName)
self.fileEntry.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W)
self.loadFileButton = Button(self, text="Load Data", command=self.loadDataClicked)
self.loadFileButton.grid(row=0, column=3, sticky=N+S+E+W)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
app = Application()
app.master.title("Sample Application")
app.mainloop()
最佳答案
添加一个根窗口并对其进行列配置,以便您的 Frame 小部件也展开。这就是问题所在,如果您没有指定一个隐式根窗口,那么框架本身就是无法正确扩展的。
root = Tk()
root.columnconfigure(0, weight=1)
app = Application(root)
关于python - Tk 网格无法正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464928/