[Tkinter-discuss] Tkinter page layout

Guilherme Polo ggpolo at gmail.com
Tue Nov 4 22:15:43 CET 2008


On Tue, Nov 4, 2008 at 5:45 PM,  <btkuhn at email.unc.edu> wrote:
> Hi guys,
>
> I'm new to the list; actually just found out it existed. I'm fairly new to
> python and programming, and I'm just beginning with Tkinter. I'm trying to
> layout my page, and I'm having problems understanding the action of some of
> the frames. I actually posted about this on a general python help message
> board and someone suggested that I repost it to this list. My question is
> somewhat complex, so in attempt to simplify things a bit I am posting part
> of a response I posted in a thread on the previous message board. Hopefully
> this fully describes my problem:
>
>
> When I specify dimensions of the text boxes, no matter how high or low, the
> enclosing frame seems to adjust to fit the new boxes. So, for instance, if I
> try inserting a text widget in leftframe with dimensions 1x1, leftframe
> shrinks down to 1x1. If I make the text widget 100x100, leftframe takes that
> size. What I want, and what seems like should be happening, is if I have
> leftframe which is 500X500 pixels, and I create a text box wit effective
> dimensions of 200X200 (I understand that text widgets use lines as
> dimensions), the text box should take up a little less than half of
> leftframe, with leftframe remaining 500X500 pixels. Instead, leftframe is
> shrinking down to become 200X200, and I can't figure out why. After a while
> I tried removing the text boxes and I got the same problem. I think maybe
> this code will better illustrate the problem. This first set of code is the
> totally stripped down version, and you can see leftframe in white on the
> left, and rightframe in blue on the right.
>
> from Tkinter import *
> import tkFileDialog,tkSimpleDialog,tkMessageBox
>
>
> WINDOWWIDTH=500
> WINDOWHEIGHT=500
> TITLEFONT = ("Courier",12,"normal")
> HEADINGFONT = ("Helvetica",9,"bold")
>
>
>
> class App:
>   def __init__ (self,master):
>       self.window = Frame(master,width=WINDOWWIDTH,height=WINDOWHEIGHT)
>       self.window.pack(expand=YES, fill=BOTH)
>       self.master= master
>       self.currentfeeds = []
>       #Create frame to contain all others
>       self.display=Frame(self.window,width=WINDOWWIDTH,height=WINDOWHEIGHT,
> bg='black')
>       self.display.pack()
>
>
>
>       #Create widgets
>       self.createwidgets()
>                                                    def createwidgets(self):
>       # create a top menu
>       self.addMenu(self.master)
>       self.addToolbar()
>       self.leftframe=Frame(self.display,
> width=WINDOWWIDTH/3,height=WINDOWHEIGHT, bg='white')
>       self.rightframe=Frame(self.display,
> width=2*WINDOWWIDTH/3,height=WINDOWHEIGHT, bg='blue')
>       self.leftframe.pack(side="left", expand="yes", fill="x")
>       self.rightframe.pack(side="left", expand="yes", fill="x")
>
>
>
>           def addMenu(self,master):
>       self.menu = Menu(self.window)
>       master.config(menu=self.menu)
>              self.filemenu = Menu(self.menu)
>
>                 def addToolbar(self):
>       self.toolbar = Frame(self.display)
>
>           self.toolbar.pack(side='top',expand="yes", fill="x")
>
>
> root=Tk()
> app=App(root)
>
> root.mainloop()
>

You could have simplified a lot more actually. (*)

>
>
>
> Next, if I try to pack ANYTHING inside rightframe, it shrinks down to size
> and totally distorts my screen dimensions. Here is the exact same code,
> adding a label widget in self.rightframe. It completely shrinks rightframe
> and distorts the screen dimensions. Does my question make sense now?
>
>
>
>
> from Tkinter import *
> import tkFileDialog,tkSimpleDialog,tkMessageBox
>
>
> WINDOWWIDTH=500
> WINDOWHEIGHT=500
> TITLEFONT = ("Courier",12,"normal")
> HEADINGFONT = ("Helvetica",9,"bold")
>
>
>
> class App:
>   def __init__ (self,master):
>       self.window = Frame(master,width=WINDOWWIDTH,height=WINDOWHEIGHT)
>       self.window.pack(expand=YES, fill=BOTH)
>       self.master= master
>       self.currentfeeds = []
>       #Create frame to contain all others
>       self.display=Frame(self.window,width=WINDOWWIDTH,height=WINDOWHEIGHT,
> bg='black')
>       self.display.pack()
>
>
>
>       #Create widgets
>       self.createwidgets()
>                                                    def createwidgets(self):
>       # create a top menu
>       self.addMenu(self.master)
>       self.addToolbar()
>       self.leftframe=Frame(self.display,
> width=WINDOWWIDTH/3,height=WINDOWHEIGHT, bg='white')
>       self.rightframe=Frame(self.display,
> width=2*WINDOWWIDTH/3,height=WINDOWHEIGHT, bg='blue')
>       self.leftframe.pack(side="left", expand="yes", fill="x")
>       self.rightframe.pack(side="left", expand="yes", fill="x")
>
>
>
>       self.label=Label(self.rightframe,text='Current
> story',font=HEADINGFONT).pack(side='top')
>           def addMenu(self,master):
>       self.menu = Menu(self.window)
>       master.config(menu=self.menu)
>              self.filemenu = Menu(self.menu)
>
>                 def addToolbar(self):
>       self.toolbar = Frame(self.display)
>
>           self.toolbar.pack(side='top',expand="yes", fill="x")
>
>
> root=Tk()
> app=App(root)
>
> root.mainloop()
>

See (*).


Thinking about your problem for a moment, it is not really a problem.
You are not supposed to develop GUI applications with a completely
fixed layout, because people that will use the GUI app will adjust it
according to their preferences. It is very very bad to layout widgets
like that, and harder too. You should rethink about the problem you
are trying to solve.

Said that, it is possibly to manually position things without using
pack or grid, that is, use place for that. But.. reconsider what you
are doing. Nevertheless, follows a sample that demonstrate what you
seems to want right now:


from Tkinter import Tk, Frame, Label

WINDOWWIDTH = 500
WINDOWHEIGHT = 500

class App:
    def __init__ (self):
        #Create frame to contain all others
        self.display = Frame(width=WINDOWWIDTH, height=WINDOWHEIGHT)
        self.display.pack()

        #Create widgets
        self.createwidgets()

    def createwidgets(self):
        self.leftframe=Frame(self.display, width=WINDOWWIDTH / 3,
                             height=WINDOWHEIGHT, bg='red')
        self.rightframe=Frame(self.display, width=2 * WINDOWWIDTH / 3,
                              height=WINDOWHEIGHT, bg='blue')
        self.leftframe.pack(side="left", expand=True, fill="x")
        self.rightframe.pack(side="left", expand=True, fill="x")

        label = Label(self.rightframe, text='test')
        label.place(x=200, y=200)


root = Tk()
app = App()
root.mainloop()


It is possible that you are after paned windows, that is perfectly
understandable then. For that, take a look at Tkinter.PanedWindow

-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list