How can I access the 'Entry' string in another function?

Discussion in 'Python' started by arvind, Jul 4, 2006.

  1. arvind

    arvind Guest

    hi all,
    i've created the myclass instance and calles the "function second()".
    i want to access the text entered in 'w' through Entry widget in
    "function third()"
    i am getting the 'fuction not having 'w' attribute error.
    how to overcome it?


    class myclass:
    senter='arvind'

    def __init__(self):
    return None

    def third(self):
    self.senter=self.w.get()
    print senter

    def second(self):
    top=Tk()
    frame=Frame(top)
    frame.master.title("second")

    strobj=StringVar()
    w=Entry(top)

    b1=Button(top,text='Next',command=self.third)

    w.grid()
    b1.grid()
    frame.grid()

    mainloop()
     
    arvind, Jul 4, 2006
    #1
    1. Advertisements

  2. Make `w` an attribute of the object. When you create the widget in
    `second()` you just bind it to the local name `w` instead of `self.w`.
    You made a similar mistake when printing `senter` in `third()`. This
    time it's the other way around: you are trying to print a non-existing
    local `senter` instead of `self.senter`. This works:

    import Tkinter as tk

    class MyClass:
    senter = 'arvind'

    def third(self):
    self.senter = self.w.get()
    print self.senter

    def second(self):
    top = tk.Tk()
    top.title('second')
    frame = tk.Frame(top)

    self.w = tk.Entry(top)

    b1 = tk.Button(top, text='Next', command=self.third)

    self.w.grid()
    b1.grid()
    frame.grid()

    top.mainloop()

    a = MyClass()
    a.second()
     
    Marc 'BlackJack' Rintsch, Jul 4, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.