3

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) 
thebtcfuture
  • 37
  • 2
  • 4

1 Answers1

4

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

Miraj50
  • 4,257
  • 1
  • 21
  • 34
  • Thank you. I believe you are right in your answer. However, I am stuck with semantics. I am creating my buttons, columns, inside of a class.The root window is created outside of my Application class. Root or "app" window acts as a master to my Application class. `class Application(Frame): ...` `root = Tk() root.title("... Expanding GUI ...") root.geometry("500x650") app = Application(root) root.mainloop()` – thebtcfuture Dec 07 '18 at 09:00
  • I have tried `self.columnconfigure(i, weight=1)` with no luck – thebtcfuture Dec 07 '18 at 09:03
  • @thebtcfuture You haven't provided your full code. So I have no idea of how your app is working. But the concept remains the same. Try giving us the full code with the unnecessary parts removed. – Miraj50 Dec 07 '18 at 09:17
  • @thebtcfuture In your code, `self` is the parent of the Buttons, so `self.columnconfigure(...)` should work. This doesn't have any influence on how your `Application` `Frame` is placed inside `root` though.. If you need more help on that you should post a [mcve] which includes everything to run and see your problem. – fhdrsdg Dec 07 '18 at 11:35