Ericson Willians
Ericson Willians

Reputation: 7845

If I'm using grid(), why the widgets don't scale when I resize the window?

I'm using tkinter with Python 3.4 in Windows 7.

I'm positioning in a non-absolute way (I'm not using place, I'm using grid), and therefore, the widgets should scale when I resize the window automatically. Nevertheless, that does not happen, and I'm failing to grasp the point. Here's my code:

import tkinter as tk

class App(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.config()
        self.grid()
        self.create_widgets()

    def config(self):
        self.master.title("Pykipedia Explorer")

    def create_widgets(self):
        self.search_label = tk.Label(self, text="Search: ")
        self.search_label.grid(row=0, column=0, sticky=tk.N+tk.SW)
        self.search_entry = tk.Entry(self)
        self.search_entry.grid(row=0, column=0, padx=60, sticky=tk.N+tk.SW)
        self.search_button = tk.Button(self, text="Explore!")
        self.search_button.grid(row=0, column=0, padx=232, sticky=tk.SW)
        self.content_area = tk.Text(self)
        self.content_area.grid(row=1, column=0)
        self.content_scroll_bar = tk.Scrollbar(self, command=self.content_area.yview)
        self.content_scroll_bar.grid(row=1, column=1, sticky=tk.NW+tk.S+tk.W)
        self.content_area["yscrollcommand"] = self.content_scroll_bar.set
        self.quit_button = tk.Button(self, text="Quit", command=self.quit)
        self.quit_button.grid(row=2, column=0, sticky=tk.SW)

def main():

    app = App()
    app.mainloop()

    return 0

if __name__ == '__main__':
    main()

Why??

Also, I've tried to use grid_columnconfigure and grid_rowconfigure just like in this answer, and it fails miserably.

Upvotes: 3

Views: 2268

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

You have several problems in your code that are working together to prevent the widgets from scaling (and by "widgets", I assume you mean the text widget).

First, you use grid to put the instance of App in the root window. However, you haven't set the sticky attribute, so the app won't grow and shrink. If it doesn't grow and shrink, neither will its contents.

Also, because you're using grid, you need to give row zero and column zero a positive weight so that tkinter will allocate extra space to it. However, since this is the only widget in the root window, you can use pack and solve the problem by replacing the call to grid with a call to pack:

self.pack(fill="both", expand=True)

Next, you use grid to add the text widget to the canvas. You haven't used the sticky option, so even if the space allocated to it grows, the widget will stay centered in the space. You need to use the sticky attribute to tell it to "stick" to all sides of the area it is given:

self.content_area.grid(row=1, column=0, sticky="nsew")

Finally, you haven't given any columns any "weight", which tells tkinter how to allocate extra space. When a window managed by grid resizes, any extra space is given to the rows and columns according to their weight. By default a row and column has zero weight, so it does not get any extra space.

To get the text area to grow as the window grows, you need to give column zero and row one a positive weight:

self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(0, weight=1)

Upvotes: 5

Related Questions