Frame not resizing.

Post here if you need help with creating a Graphical User Interface in Python.

Frame not resizing.

Postby rkamarowski » Thu Sep 19, 2013 1:33 pm

With the following code, the frame on the left side of the screen is not resizing (vertical) with the window.
Code: Select all
from tkinter import *
from tkinter import ttk
#from tkinter.ttk import *

dictColors = {
    'FrameBackground' :'gray16',
    'WindowBackground':'black'
}

root = Tk()
root.geometry("600x400+300+300")
root.config(bg=dictColors['WindowBackground'])

style = ttk.Style()
style.configure('My.TFrame', borderwidth=0, relief=FLAT, background=dictColors['FrameBackground'])
frameMain = ttk.Frame(root)
frameLeft = ttk.Frame(frameMain, style='My.TFrame', width=200, height=400)
frameLeft.rowconfigure(0, weight=1, minsize=400)

frameMain.grid(column=0, row=0, sticky=NSEW)
frameLeft.grid(column=0, row=0, sticky=(N,E))

root.mainloop()
 


What am I doing wrong?
rkamarowski
 
Posts: 5
Joined: Thu Sep 19, 2013 12:49 pm

Re: Frame not resizing.

Postby TheFunk » Thu Sep 19, 2013 6:48 pm

Code: Select all
frameLeft = ttk.Frame(frameMain, style='My.TFrame', width=200, height=400)


When you set the width and height with this to 200x400, aren't you statically assigning the left frame that specific width and height?

I have no idea if this would work, but you could try setting width and height of the left frame by getting your main windows width and height, and doing some form of multiplication or division on those numbers. For example, if you wanted your left frame to be roughly one third of the width of your main window, you could make something like framewidth = (topwidth / 3). That's my best bet, but I'm pretty new to Tkinter as well, so don't take my word for it.
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: Frame not resizing.

Postby rkamarowski » Thu Sep 19, 2013 7:17 pm

I've been trying code similar to: width=(frameMain.width / 3)

but no luck.

Obviously I'm very new to this.
rkamarowski
 
Posts: 5
Joined: Thu Sep 19, 2013 12:49 pm

Re: Frame not resizing.

Postby TheFunk » Thu Sep 19, 2013 7:45 pm

Try setting the width of your root window, which was 600x400 to begin, to something like 1200x800, and seeing if when you start your program, your left frame is now twice the width it was before. If it is, you'll know your setting the left frame's width variable correctly with that bit of math. Once we know that for sure, then we can move on to resizing your frame. In order to get the frame to resize properly, you might have to 'repaint' it every time the main window is resized. I'll try practicing what I'm preaching here, and if it works I'll post some example code for you. You should try it too, and we'll bounce some ideas back and forth.

First thing I noticed, width is not a valid function for a frame, so we need to find another means of getting your main frames width, or getting your root windows width, since they'll be the same consistently.
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: Frame not resizing.

Postby TheFunk » Thu Sep 19, 2013 7:59 pm

Alright check this out:

Code: Select all
from tkinter import *
from tkinter import ttk
#from tkinter.ttk import *

dictColors = {
    'FrameBackground' :'gray16',
    'WindowBackground':'black'
}

root = Tk()
root.geometry("600x400+300+300")
root.config(bg=dictColors['WindowBackground'])
root.update()
widthvar = root.winfo_width()
heightvar = root.winfo_height()
style = ttk.Style()
style.configure('My.TFrame', borderwidth=0, relief=FLAT, background=dictColors['FrameBackground'])
frameMain = ttk.Frame(root)
frameLeft = ttk.Frame(frameMain, style='My.TFrame', width=(widthvar/3), height=heightvar)
frameLeft.rowconfigure(0, weight=1, minsize=400)
frameMain.grid(column=0, row=0, sticky=NSEW)
frameLeft.grid(column=0, row=0, sticky=(N,E))
root.mainloop()


Your width/height of your left frame are now relative to your main windows size, instead of being statically assigned numbers. Now, you just need to find a way to 'repaint' the frames when the window is resized. To do this you'll probably have to make use of an event. Basically, the user will resize the window, the resizing will trigger an event, which will run a few short lines of code, which will tell your program to rerun the root.geometry command, and set width = (the new window width / 3) and the new window height = to the new window height. Does that make sense?

In pseudo code:

Code: Select all
on window resize event:
    update leftframesize
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: Frame not resizing.

Postby rkamarowski » Fri Sep 20, 2013 11:37 am

Yes, it makes sense.

Code: Select all
frameLeft = ttk.Frame(frameMain, style='My.TFrame',width=(widthvar/3), height=heightvar)


The left frame has no width (it's not visible).
rkamarowski
 
Posts: 5
Joined: Thu Sep 19, 2013 12:49 pm

Re: Frame not resizing.

Postby TheFunk » Fri Sep 20, 2013 12:16 pm

Try copying my code above. I just checked and the left panel (the grey one) is showing up for me. Once you have that working, do some Googling for 'tkinter window events' and see if you can figure out how to capture resize events, and repaint your frames when you catch them.
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: Frame not resizing.

Postby rkamarowski » Fri Sep 20, 2013 12:20 pm

That did it. Thank you. I'm new to Python so I need to find info on the re-size event.
rkamarowski
 
Posts: 5
Joined: Thu Sep 19, 2013 12:49 pm

Re: Frame not resizing.

Postby rkamarowski » Fri Sep 20, 2013 2:29 pm

I'm having a difficult time with the resize event. The info I've found on the web suggests that setting 'weight=1' should be enough for a grid. Any help would be appreciated.
rkamarowski
 
Posts: 5
Joined: Thu Sep 19, 2013 12:49 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 4 guests