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

A

arvind

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()
 
M

Marc 'BlackJack' Rintsch

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?

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()
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top