#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    9
    Rep Power
    0

    Simple Question for an Intermediate Programmer


    I have a few pieces of code here, the first piece is not written by myself and is taken from another program, basically I cannot reference the entry widgets in the standard fashion(second piece of code, ie. def newHost(self)). When the style of writing is that of the code below, How do you reference the Entry widget? :

    iframe2 = Frame(frame, bd=2, relief=RIDGE)
    Label(iframe2, text='Hostname widget:').pack(side=LEFT, padx=5)
    hn_var = StringVar()
    Entry(iframe2, textvariable=hn_var, bg='white').pack(side=RIGHT, padx=5)
    hn_var.set('Entry widget')
    iframe2.pack(expand=1, fill=X, pady=10, padx=5)

    def newHost(self):
    'Clear the fields in the window(except the listbox)'
    self.hn_var.delete(0, END)

    Here is the error message :

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
    File "C:\mynewapp.py", line 228, in newHost
    self.hn_var.delete(0, END)
    AttributeError: App instance has no attribute 'hn_var'
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    delete is a method of the Entry, not the StringVar. So you can write something like this:
    Code:
    self.hn_entry = Entry(...)
    ...
    self.hn_entry.delete(0,END)
    Regards, mawe
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    9
    Rep Power
    0

    Same problem here:


    I have changed the code to below to make things clearer to read. This is inside a class. It should be accessible from within the class without being global; surely?

    iframe2 = Frame(frame, bd=2, relief=RIDGE)
    Label(iframe2, text='Hostname widget:').pack(side=LEFT, padx=5)
    hn_entry = Entry(iframe2, bg='white').pack(side=RIGHT, padx=5)
    iframe2.pack(expand=1, fill=X, pady=10, padx=5)


    def newHost(self):
    'Clear the fields in the window(except the listbox)'
    hn_entry.delete(0, END)

    Traceback (most recent call last):
    File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
    File "C:\mynewapp.py", line 231, in newHost
    hn_entry.delete(0, END)
    NameError: global name 'hn_entry' is not defined


    How can this be possible still ? Thanks for your help its sort of getting me a bit closer now. I can feel it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Code:
    hn_entry = Entry(iframe2, bg='white').pack(side=RIGHT, padx=5)
    Here, hn_entry is the return value of pack(), not an Entry. You have to write it like this
    Code:
    hn_entry = Entry(...)
    hn_entry.pack()
    You have to write self.hn_entry (as I showed you in my example ) to make it accessible in the whole class.

    Regards, mawe
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    9
    Rep Power
    0
    Thats great i think I have solved that issue now. Thanks very much for all your help. You must be a pretty good programmer mawa, I must buy you a beer !

    I just have one more small question. I wondered if you could take a quick browse at my program at :

    http://www.artificialevolution.net/mynewapp.py

    and tell me why it is not saving. It does everything I need it to at the moment except save to a file. Its sort of the same type of error.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Code:
    def saveFile(self):
            file = tkFileDialog.asksaveasfilename(filetypes=[("Profile files", "nte")])
            if filepath:
                f = open(filepath, 'w')
                cPickle.dump(Host.Hosts, f)
                f.close()
    Do you mean here? Well, the filename is stored in file, and then you ask for it in filepath. Try it like this:
    Code:
    def saveFile(self):
            filename = tkFileDialog.asksaveasfilename(filetypes=[("Profile files", "nte")])
            if filename:
                f = open(filename, 'w')
                cPickle.dump(Host.Hosts, f)
                f.close()
    Regards, mawe
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    9
    Rep Power
    0
    You are great ....


    Thanks very much.

IMN logo majestic logo threadwatch logo seochat tools logo