python - Tk 网格无法正确调整大小

标签 python grid resize tkinter

我正在尝试使用 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/

相关文章:

python - 当人们说 CPython 是用 C 编写的时,这意味着什么?

python - 搜索 csv 文件最快的方法是什么?

python - 为什么数据存储区日期时间在 App Engine 控制台与 App Engine 仪表板中看起来不同?

css - 在 IE 上调整窗口大小时自动换行不起作用

CSS:3 行(页眉、页脚、内容)液体布局,试图使中间的行在 window.resize 上扩展

ios - 自定义 UINavigationBar sizeThatFits 实现不返回真实的 frame.size.width

python - 无法使用 uWSGI 运行 Restplus Flask API

c++ - 动态创建网格布局并在 Qt 中管理它

jquery - 重新加载网格不适用于多个 jqgrid

angular - kendo-grid加载时如何显示kendo加载图标?