python - Tkinter columnconfigure 重量不调整

标签 python python-3.x tkinter tkinter-layout

我是 Tkinter 模块的新手。我只有 PyQt5 的经验。我正在我的框架中玩几个小部件。它们是三个按钮,我正在尝试相对于窗口大小扩大它们的大小。为此,我使用 w.columnconfigure(n, weight=1)。这应该将我拥有的 3 个按钮分布在窗口框架上。这是我正在运行的代码。在将小部件放置在网格中之前,我已经尝试使用 w.columnconfigure,并且如发布的代码所示,在将小部件放置在网格中之后。我没有注意到任何区别或功能。有约定吗?无论如何,感谢任何指导!

    def create_widgets(self):
        """ Create three buttons that do nothing. """
        self.bttn1 = Button(self, text="I do nothing")

        self.bttn2 = Button(self)
        self.bttn2.configure(text="Me too!")   

        self.bttn3 = Button(self)
        self.bttn3["text"] = "Same here!"

        self.bttnCt = Button(self)
        self.bttnCt["text"] = "Total Clicks: 0"
        self.bttnCt["command"] = self.update_count

        self.bttn1.grid(row=0, column=0, sticky=W+E)
        self.bttn2.grid(row=0, column=1, sticky=W+E)
        self.bttn3.grid(row=0, column=2, sticky=W+E)
        self.bttnCt.grid(row=1, column=1, sticky=W+E)

        bttn_list = [self.bttn1, self.bttn2, self.bttn3, self.bttnCt]

        for k, i in enumerate(bttn_list):
            i.columnconfigure(k, weight=1)

        #self.bttn1.columnconfigure(0, weight=1)
        #self.bttn2.columnconfigure(1, weight=3)        
        #self.bttn3.columnconfigure(2, weight=1)
        #self.bttnCt.columnconfigure(3, weight=1) 

最佳答案

columnconfigure()rowconfigure() 函数应用于窗口或框架,窗口部件是其中的一部分。在这里,您将它应用于按钮本身。基本上将其应用于其父级

这是一个小例子。

import tkinter as tk

app = tk.Tk()

bttn1 = tk.Button(app, text="I do nothing")
bttn2 = tk.Button(app, text='Me too!')
bttn3 = tk.Button(app, text='Same here!')
bttnCt = tk.Button(app, text='Total Clicks: 0')

bttn1.grid(row=0, column=0, sticky="ew")
bttn2.grid(row=0, column=1, sticky="ew")
bttn3.grid(row=0, column=2, sticky="ew")
bttnCt.grid(row=1, column=1, sticky="ew")

bttn_list = [bttn1, bttn2, bttn3, bttnCt]

for i in range(len(bttn_list)):
    app.columnconfigure(i, weight=1) ## Not the button, but the parent

app.mainloop()

enter image description here

关于python - Tkinter columnconfigure 重量不调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665173/

相关文章:

python - Django 模板不迭代列表

python-3.x - 减少 return 语句的数量

python - Tkinter 向文本添加描边

python - 无法调用 "event"命令 : application has been destroyed

python - 计算 pandas 数据框中的匹配组合

python - Python 中的重复数据删除

python-3.x - 尽管使用存储 key 进行连接,azure storage gen2 上的 set_access_control 仍会引发权限错误

python - 我可以使用最新的 C2x 语言功能来开发 Python 扩展模块吗?

python - 如何修复 python 3.4 tkinter "Index Error: list index out of range"中的此错误

python - Pycharm 调试器 - 框架不可用