Tkinter列配置权重未调整

时间:2018-12-07 07:38:00

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

我是Tkinter模块的新手。我只有PyQt5的经验。我在Frame中玩几个小部件。它们是三个按钮,我正在尝试相对于窗口大小来扩展它们的大小。为此,我使用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) 

1 个答案:

答案 0 :(得分:2)

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