Tk网格无法正常调整大小

mjn*_*n12 6 python grid resize tkinter

我正在尝试用python中的Tkinter编写一个简单的ui,我无法在网格中获取小部件来调整大小.每当我调整主窗口的大小时,入口和按钮小部件根本不会调整.

这是我的代码:

 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()
Run Code Online (Sandbox Code Playgroud)

Joh*_*Jr. 14

添加根窗口并对其进行配置,以便您的Frame小部件也可以展开.这就是问题,如果你没有指定一个隐藏的根窗口,那么框架本身就是没有正确扩展的东西.

root = Tk()
root.columnconfigure(0, weight=1)
app = Application(root)
Run Code Online (Sandbox Code Playgroud)

  • 添加了rowconfigure以允许垂直扩展 (2认同)