Dave
Dave

Reputation: 1208

Resizing tkinter windows and contents

I am using python tkinter to build a ui containing a matplotlib figure and some buttons, but am having difficulty with resizing the window and it's contents. I've looked at some of the examples on this site and the docs and as I understand it, for a frame containing smaller frames to resize together they all need to be configured individually. Each one gets a weight applied to it to define how much of the available space it receives (is this correct?). However, when I try to apply this as shown below none of the frames resize. Also if the wiegth is zero for columnconfigure and 1 for row configure does that mean it will only resize in one direction?

import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class Application():
    def __init__(self, master):


        frame2 = Tkinter.Frame(master, height=510, width=770, bg='red')
        frame2.grid(row=0, column=0, sticky='nsew')
        frame2.columnconfigure(0, weight=1)
        frame2.rowconfigure(0, weight=1)

        frame2a = Tkinter.Frame(frame2, height=80, width=770, bg='blue') 
        frame2a.grid(row=0, column=0, sticky='nsew')
        frame2a.columnconfigure(0, weight=1)
        frame2a.rowconfigure(0, weight=1)        

        frame2b = Tkinter.Frame(frame2, height=410, width=770, bg='green') 
        frame2b.grid(row=1, column= 0, sticky='nsew')
        frame2b.columnconfigure(0, weight=1)
        frame2b.rowconfigure(1, weight=1)    

        # add plot
        fig = Figure(figsize=(9.5,5.2), facecolor='white')
        fig.add_subplot(111)        
        canvas = FigureCanvasTkAgg(fig, master=frame2b)

        canvas.show()
        canvas.get_tk_widget().pack(side='top', fill='both', expand=1)

if __name__ == '__main__' :

    root = Tkinter.Tk()
    root.geometry("770x510")
    app = Application(root)
    root.mainloop()

Upvotes: 1

Views: 4325

Answers (2)

Alexander Hanysz
Alexander Hanysz

Reputation: 801

The rowconfigure needs to be applied to the container, not to the contained objects. You have frame 2 containing frame2a and frame2b. So along with frame2.rowconfigure(0, weight=1), giving a weight of 1 to row 0 (i.e. to frame2a), you need to add frame2.rowconfigure(1, weight=1) to weight the other row equally. On the other hand, the four lines of code of the form frame2[ab].[row|column]configure(...) are redundant.

Upvotes: 0

TidB
TidB

Reputation: 1759

Your approach using columnconfigure and rowconfigure is correct, but you forgot one thing: You haven't used the methods on the master window. So, you basically want to do this:

def __init__(self, master):
    master.columnconfigure(0, weight=1)
    master.rowconfigure(0, weight=1)

To answer your other question (Also if the wiegth is zero for columnconfigure and 1 for row configure does that mean it will only resize in one direction?): Yes, you're right, the widget/window would extend in just one or none direction then.

Additionally, since you are using grid for dynamic resizing, the height and width parameters are obsolete.

Upvotes: 1

Related Questions