Hi!
I'm trying to combine some frames in a GUI as a toolbar and content area. Currently I'm on windows and using python 3, and I want preferrably my app to work on *nix and mac as well.

The problem is that in the following app, when I resize it the toolbar area is increasing/decreasing, and the content area is staying the same. I would like the opposite to happen. In other words, the area with the 'up' button shouldn't be resized, but the area with the 'down' button should.

My reasoning is that since the lower area, b, is using a sticky in all directions, and the upper button is sticking north, so the lower area should be resized.

Thanks for your help!

from tkinter import *

class app( Frame ):
    def __init__( self, master=None ):
        Frame.__init__( self, master )
        self.grid( sticky='nswe' )
        self.top = root.winfo_toplevel()
        self.top.rowconfigure( 0, weight=1 )
        self.top.columnconfigure( 0, weight=1 )
        self.rowconfigure( 0, weight=1 )
        self.columnconfigure( 0, weight=1 )
        a = Button( self, text="up" )
        a.grid( row=0, column=0, sticky='n')
        self.b = Canvas( self, bg='black' )
        self.b.grid( row=1, column=0, sticky='nswe' )
        c = Button( self.b, text="down" )
        c.grid( sticky='nswe')
        d = self.b.create_window( 10, 10, window=c, anchor=NW )
if __name__ == "__main__":
    root = Tk()
    appl = app( master=root )
    appl.mainloop()

Recommended Answers

All 3 Replies

Make the weight for row 1 very high to keep row 0 almost constant, like this eaxample shows ...

from tkinter import *

class app( Frame ):
    def __init__( self, master=None ):
        Frame.__init__( self, master )
        self.grid( sticky='nswe' )
        top = root.winfo_toplevel()
        top.rowconfigure( 0, weight=1 )
        top.columnconfigure( 0, weight=1 )
        self.rowconfigure( 0, weight=1 )
        # make weight for row 1 very high to keep row 0 constant
        self.rowconfigure( 1, weight=1000 )
        self.columnconfigure( 0, weight=1 )
        a = Button( self, text="up" )
        a.grid( row=0, column=0, sticky='n')
        self.b = Canvas( self, bg='black' )
        self.b.grid( row=1, column=0, sticky='nswe' )
        c = Button( self.b, text="down" )
        c.grid( sticky='nswe')
        d = self.b.create_window( 10, 10, window=c, anchor=NW )
if __name__ == "__main__":
    root = Tk()
    appl = app( master=root )
    appl.mainloop()

Ah I see, it works perfectly!

Thank you indeed vegaseat!

Ok, using the new code, I would like to face yet another resizing challange. That is when adding an element into a top-level window I want the behaviour to be as if it were maximized. In other words, when the top-level window is maximized, and an element is added, the top-level window remains the same.

The problem is, that when adding an element, the top-level window is resized automatically. I bind a configure-callback and stop the normal behaviour by returning "break". This works, but only after I have resized the window manually.

I have edited the code above as an example. When clicking the down-button a new button is added at the bottom of the window.

from tkinter import *
class app( Frame ): # # 
    def configurecb( self, event ): # # 
        return "break" #  #
    def downbuttoncb( self, event ): # # 
        x = Button( self, text='New Button' )
        x.grid()
    def __init__( self, master=None ): # # 
        Frame.__init__( self, master )
        self.grid( sticky='nswe' )
        self.top = root.winfo_toplevel()
        self.top.rowconfigure( 0, weight=1 )
        self.top.columnconfigure( 0, weight=1 )
        self.rowconfigure( 0, weight=1 )
        self.rowconfigure( 1, weight=1000 )
        self.columnconfigure( 0, weight=1 )
        self.top.bind( '<Configure>', self.configurecb )
        a = Button( self, text="up" )
        a.grid( row=0, column=0, sticky='n')
        self.b = Canvas( self, bg='black' )
        self.b.grid( row=1, column=0, sticky='nswe' )
        c = Button( self.b, text="down" )
        c.grid( sticky='nswe' )
        c.bind( '<Button-1>', self.downbuttoncb )
        d = self.b.create_window( 10, 10, window=c, anchor=NW )
if __name__ == "__main__":
    root = Tk()
    appl = app( master=root )
    appl.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.