[Tutor] Tkinter packing confusion

Terry Carroll carroll at tjc.com
Tue Dec 28 08:13:26 CET 2004


First, thanks for the responses on my questions on using Tkinter for data 
entry from last week. Alan's and Harm's responses were very helpful.

I'm now playing with a tiny application that does nothing except allow the 
user to enter a variable for processing.  It provides a default value, and 
three buttons: if the user hits CANCEL, the app terminates; if he hits OK, 
it prints out the entered variable; if he hts RESET, the data entered is 
reset to the default it had to start.

The program logic works, but I'm totally lost on the pakcer layout 
manager.  What I want to see is a window with something like this (bear 
with my ascii art):

  URL: [ http://www.somewhere.com/default ]

  +--------+   +-------+  +----+   
  | Cancel |   | Reset |  | OK |
  +--------+   +-------+  +----+

What I'm getting is:

       URL:
    +--------+
    | Cancel |
    +--------+   [ http://www.somewhere.co ]
  +-------++----+
  | Reset || OK +
  +-------++----+


I'm sure my error lies in the packing, but I don't get it.

I'm probably thinking abou it wrong, but my logic is this:

I create a master frame, with two sub-frames, fBaseURL for the URL label
and entry, and fButtons for the three buttons.

In the fBaseURL frame, I pack the label widget to the top, and then the
entry widget right (i.e., the entry widget to the right of the label
widget).  Then I pack in the fBaseURL frame.

In the fButtons frame, I pack in the "Cancel" button as the top of 
fButtons; then "Reset" to its right, and "OK" to its right.  Then I pack 
in the fButtons frame.

Finally, I pack the parent frame.

Well, I'm lost.  I'm trying dozens of variations, but just don't get it.  
Can someone explain to me how to pack in more simple terms?

Here's my code:

import Tkinter as tk

class DEApp:
    def __init__(self):

        top=tk.Tk()
        
        self.DefaultBaseURL = "http://www.somewhere.com/default/"
        self.tvDefaultBaseURL=tk.StringVar()
        self.tvDefaultBaseURL.set(self.DefaultBaseURL)

        self.F=tk.Frame(top)

        fBaseURL=tk.Frame(self.F)
        lBaseURL=tk.Label(self.F, text="URL:")
        eBaseURL=tk.Entry(textvariable=self.tvDefaultBaseURL)
        lBaseURL.pack(side="top")
        eBaseURL.pack(side="right")
        fBaseURL.pack(side="top")

        fButtons=tk.Frame(self.F)
        bCancel=tk.Button(fButtons, text="Cancel", command=self.evCancel)
        bReset=tk.Button(fButtons, text="Reset", command=self.evReset)
        bOK=tk.Button(fButtons, text="OK", command=self.evOK)
        bCancel.pack(side="top")
        bReset.pack(side="right")
        bOK.pack(side="right")
        fButtons.pack(side="left")
        
        self.F.pack(side="top")

    def evCancel(self):
        print "Cancel hit"
        self.F.quit()
        return

    def evReset(self):
        print "Reset hit"
        self.tvDefaultBaseURL.set(self.DefaultBaseURL)
        return
    
    def evOK(self):
        print "OK hit"
        print "BaseURL is now", self.tvDefaultBaseURL.get()
        self.F.quit()
        return

app = DEApp()
app.F.mainloop()
    
        
        
    




More information about the Tutor mailing list