Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter columnconfigure weight not adjusting

I am new to the Tkinter module. I only have experience with PyQt5. I am playing with a couple widgets in my Frame. They are three buttons, and I am trying to expand their size relative to the size of the window. To do this I am using w.columnconfigure(n, weight=1). This should spread the 3 buttons I have across the window Frame. This is the code I am running. I have tried with the w.columnconfigure before placing the widgets in the grid, and, as seen in the posted code, after the widgets are placed in the grid. I noticed no difference or functionality. Is there a convention? Anyway, appreciate any guidance!

    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) 
like image 560
thebtcfuture Avatar asked Mar 05 '26 03:03

thebtcfuture


1 Answers

columnconfigure() or rowconfigure() functions are applied to the window or frame, of which the widget is a part of. Here you are applying it on the button itself. Apply it on on its parent basically.

Here is a small example.

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

like image 122
Miraj50 Avatar answered Mar 06 '26 15:03

Miraj50



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!