Tkinter pack Problem

H J van Rooyen mail at microcorp.co.za
Wed Jul 26 06:46:39 EDT 2006


Hi,

I am struggling to get the pack method to do what I intend.
I am trying to display user input in a seperate window, along with
a little description of the field, something like this:

             Current entry
Company :               entered co. name
First entry :               entered stuff
The second entry:     more entered stuff
Entry number three :  Last entered stuff

This seems so simple - but I have now spent a day or two mucking around -
and no matter what I do with the side, after and anchor parameters,
I just seem to get weird results - I must be doing something terribly stupid,
but I cant see it

So I have boiled the thing down as far as I can. - to an entry window with a
start and quit button,
one entry method, and the display window for the entered content - aside from
displaying, the programme does nothing.

Its doing the same thing on Linux and on Windows

This code should run and illustrate the hassles I am having:

8<-----------------------------start of code section--------------------------

#!/usr/bin/python
#   The original of this module is supposed to do an accounting entry -

#   we get the gui stuff

from Tkinter import *

class Entryscreen(Frame):
    """This screen is used to construct a new Entry."""

#   we define the variables we use

    des_string = ''     # Description of what we want to do
    req_string = ''     # Prompt string
    dis_string = ''     # Description of field for display

    Company =  "New Entry Screen"   # we dont have a company yet
    Entry_1 = ''                    # or any entries
    Entry_2 = ''
    Entry_3 = ''

    def start_new(self):
        """This is the routine that assembles a new record"""

        if self.Company == "New Entry Screen":  # if its the first time,

#   we make a new window

            show = Tk()
            show.title('Accounting entry display window')

#   we get an instance to display results in

            self.disp = Showscreen("Current entry",master=show)

#   then we ask for the company:

            des_string = "Getting the Company "
            req_string = "Enter the Company for this session"
            dis_string = 'Company:'
            error = self.GetEntry(des_string, req_string, dis_string, self.disp)
            self.Company = self.Amount

#   Then or else we ask for entry details

        des_string = "Getting first entry"
        req_string = "Enter first field"
        dis_string = "First entry:"
        error = self.GetEntry(des_string, req_string, dis_string, self.disp)

        des_string = "Getting second entry"
        req_string = "Enter second field"
        dis_string = "The second entry:"
        error = self.GetEntry(des_string, req_string, dis_string, self.disp)

        des_string = "Getting third entry"
        req_string = "Enter third field"
        dis_string = "Entry number three:"
        error = self.GetEntry(des_string, req_string, dis_string, self.disp)

    def GetEntry(self, des_string, req_string, dis_string, disp):
        """Entry routine for one field"""

        line = Entryline(des_string, req_string, dis_string, disp, master=root)
        error = line.mainloop()
        self.Amount = line.retstring
        line.destroy()

    def say_bye(self):
        print 'Have a nice day, Bye!'
        self.quit()

    def createWidgets(self):

        self.descr = Label(self)
        self.descr["text"] = self.Company
        self.descr["fg"] = 'purple'
        self.descr['bg'] = 'yellow'
        self.descr.pack({'expand': 'yes', 'fill': 'both', "side": "top"})

        Start = Button(self)
        Start["text"] = "Start a new entry"
        Start["fg"]   = "blue"
        Start['bg']   = 'green'
        Start["command"] =  self.start_new
        Start.pack({'expand': 'yes', 'fill': 'both', "side": "left", 'after':
self.descr})

        QUIT = Button(self)
        QUIT["text"] = "QUIT"
        QUIT["fg"]   = "black"
        QUIT['bg']   = 'red'
        QUIT["command"] =  self.say_bye
        QUIT.pack({'expand': 'yes', 'fill': 'both', "side": "left", 'after':
Start})

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

class Showscreen(Frame):
    """This is supposed to show the entries as they occur."""

    def CreateWidgets(self, Description):

        self.descr = Label(self)
        self.descr["text"] = Description
        self.descr["fg"] = 'purple'
        self.descr['bg'] = 'yellow'
        self.descr.pack({'expand': 'yes', 'fill': 'x', "side": "top", "anchor":
"n"})

    def __init__(self, Description, master=None):
        Frame.__init__(self,master)
        self.pack()
        self.CreateWidgets(Description)

class Entryline(Frame):
    """This asks for an entry from the user and displays the result"""

    retstring = ''

    def entryend(self,S):
        """This gets done on carriage return and is where the hassles
originate"""

        self.retstring = self.estring.get() # get the entered string

        self.disp.Amount_des = Label(self.disp)       # and put it into the
display window
        self.disp.Amount_des["text"] = self.dis_string
        self.disp.Amount_des["fg"] = 'purple'
        self.disp.Amount_des['bg'] = 'yellow'
        self.disp.Amount_des.pack({'expand': 'yes', 'fill': 'x', "side":
"left"})

        self.disp.Amount = Label(self.disp)
        self.disp.Amount["text"] = self.retstring
        self.disp.Amount["fg"] = 'blue'
        self.disp.Amount['bg'] = 'green'
        self.disp.Amount.pack({'expand': 'yes', 'fill': 'x', "after":
self.disp.Amount_des})

        self.quit()                         # That's it, folks

    def createWidgets(self, des_string, req_string):
        """makes the stuff to ask the question"""

        descr = Label(self)
        descr["text"] = des_string
        descr["fg"] = 'purple'
        descr['bg'] = 'yellow'
        descr.pack({'expand': 'yes', 'fill': 'both', "side": "top"})

        req = Label(self)
        req ["text"] = req_string
        req ["fg"] = 'yellow'
        req ['bg'] = 'blue'
        req.pack({'expand': 'yes', 'fill': 'both', "side" :"top",'after':
descr})

        self.estring = Entry(self)
        self.estring['fg'] = 'black'
        self.estring['bg'] = 'white'
        self.estring.pack({'expand':'yes','fill': 'both', 'side': 'top',
'after': req})
        self.estring.bind('<Key-Return>', self.entryend)
        self.estring.focus_set()

    def __init__(self, des_stringP, req_stringP, dis_stringP, dispP, master):

        self.disp = dispP       # keep the passed params in instance vars
        self.des_string = des_stringP
        self.req_string = req_stringP
        self.dis_string = dis_stringP

        Frame.__init__(self, master)
        self.pack()
        self.createWidgets(self.des_string, self.req_string)

root = Tk()
root.title('Accounting Entry Window')
app = Entryscreen(master=root)
app.mainloop()
root.destroy()

8<----------------------------------end of code section-------------------

can anybody see what I am doing wrong?

- Hendrik




More information about the Python-list mailing list