| | |
wxPython Events help
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Also, on clicking "Send" after filling up everything correctly, it shows this error:
What should I do to solve this? Can anyone please explain to me about the scope of the variables which are causing the error?
•
•
•
•
Traceback (most recent call last):
File "C:\Documents and Settings\sravan953\My Documents\Sravan\PYTHON\GUI.py", line 85, in Send
msg="To: "+to+"\nSubject: "+subject+"\n"+message
NameError: global name 'to' is not defined
python Syntax (Toggle Plain Text)
msg="To: "+to+"\nSubject: "+subject+"\n"+message
May 'the Google' be with you!
•
•
•
•
python Syntax (Toggle Plain Text)
wx.StaticText(self.panel,-1,"To:",pos=(10,250)) wx.StaticText(self.panel,-1,"Subject:",pos=(10,280)) wx.StaticText(self.panel,-1,"Message:",pos=(10,310)) toadd=wx.TextCtrl(self.panel,104,"<username@domain.server>",pos=(80,250),size=(240,20)) to=toadd.GetValue() subj=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20)) subject=subj.GetValue() mess=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150)) message=mess.GetValue() def Send(self,event): msg="To: "+to+"\nSubject: "+subject+"\n"+message s.sendmail(us,to,msg) s.quit()
to in line 4. Even then it says that the global variable isn't defined! 0
#24 Oct 6th, 2009
I defined
Why doesn't it work if I define the variables in the class?
user,passw,to,subject,message in the class to make it available to all functions, but it says variable not defined, so I had to define everything outside the class like this: python Syntax (Toggle Plain Text)
import smtplib import time import os import wx window=wx.App() s=smtplib.SMTP("smtp.gmail.com",587) s.starttls() info={} user="" passw="" class pymailer(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700)) self.panel=wx.Panel(self,-1) menubar=wx.MenuBar() filem=wx.Menu() filem.Append(201,"Quit") self.Bind(wx.EVT_MENU,self.Quit,id=201) viewm=wx.Menu() viewm.Append(202,"About") self.Bind(wx.EVT_MENU,self.About,id=202) menubar.Append(filem,"File") menubar.Append(viewm,"Help") self.SetMenuBar(menubar) wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10)) wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100)) wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130)) username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100)) user=username.GetValue() password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130)) passw=password.GetValue() login=wx.Button(self.panel,103,label="Login",pos=(10,170)) self.Bind(wx.EVT_BUTTON,self.Login,id=103) wx.StaticText(self.panel,-1,"To:",pos=(10,250)) wx.StaticText(self.panel,-1,"Subject:",pos=(10,280)) wx.StaticText(self.panel,-1,"Message:",pos=(10,310)) to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20)) info["To"]=to_info.GetValue() sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20)) info["Subject"]=sbj_info.GetValue() msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150)) info["Message"]=msg_info.GetValue() send=wx.Button(self.panel,107,"Send",pos=(245,470)) self.Bind(wx.EVT_BUTTON,self.Send,id=107) self.Centre() self.Show() def Quit(self,event): self.Close() def About(self,event): about=wx.AboutDialogInfo() about.SetName("Py-Mailer") about.SetCopyright("(c) 2009 Sravan") about.SetWebSite("http://www.uberpix.wordpress.com") about.AddDeveloper("Sravan") about.AddDeveloper("Dan") wx.AboutBox(about) def Login(self,event): try: s.login(user,passw) wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200)) except: wx.StaticText(self.panel,-1,"Failed",pos=(10,200)) def Send(self,event): to=info["To"] subject=info["Subject"] message=info["Message"] msg="To: "+to+"\nSubject: "+subject+"\n"+message s.sendmail(user,to,msg) s.quit() pymailer() window.MainLoop()
Why doesn't it work if I define the variables in the class?
•
•
Join Date: Jun 2009
Posts: 23
Reputation:
Solved Threads: 1
0
#25 Oct 6th, 2009
They won't work inside the class unless you put 'self.' in front of them. If you just use the variable user inside __init__ user is destroyed at the end of __init__. What you'll want to do is use self.user to make it a class variable and not a function variable. Then you won't need to make them global.
0
#26 Oct 6th, 2009
Here are the first few lines, I did as you told:
But it still doesn't work...
python Syntax (Toggle Plain Text)
class pymailer(wx.Frame): self.info={} self.user="" self.passw="" def __init__(self): . . . password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130)) self.passw=password.GetValue()
But it still doesn't work...
0
#28 Oct 6th, 2009
This is the full code for reference:
On running, after entering all details (Login--> 'Failed', don't know why) and clicking on 'Send' it says:
python Syntax (Toggle Plain Text)
import smtplib import time import os import wx window=wx.App() s=smtplib.SMTP("smtp.gmail.com",587) s.starttls() class pymailer(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700)) self.info={} self.user="" self.passw="" self.to="" self.subject="" self.message="" self.panel=wx.Panel(self,-1) menubar=wx.MenuBar() filem=wx.Menu() filem.Append(201,"Quit") self.Bind(wx.EVT_MENU,self.Quit,id=201) viewm=wx.Menu() viewm.Append(202,"About") self.Bind(wx.EVT_MENU,self.About,id=202) menubar.Append(filem,"File") menubar.Append(viewm,"Help") self.SetMenuBar(menubar) wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10)) wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100)) wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130)) username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100)) self.user=username.GetValue() password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130)) self.passw=password.GetValue() login=wx.Button(self.panel,103,label="Login",pos=(10,170)) self.Bind(wx.EVT_BUTTON,self.Login,id=103) wx.StaticText(self.panel,-1,"To:",pos=(10,250)) wx.StaticText(self.panel,-1,"Subject:",pos=(10,280)) wx.StaticText(self.panel,-1,"Message:",pos=(10,310)) to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20)) self.info["To"]=to_info.GetValue() sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20)) self.info["Subject"]=sbj_info.GetValue() msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150)) self.info["Message"]=msg_info.GetValue() send=wx.Button(self.panel,107,"Send",pos=(245,470)) self.Bind(wx.EVT_BUTTON,self.Send,id=107) self.Centre() self.Show() def Quit(self,event): self.Close() def About(self,event): about=wx.AboutDialogInfo() about.SetName("Py-Mailer") about.SetCopyright("(c) 2009 Sravan") about.SetWebSite("http://www.uberpix.wordpress.com") about.AddDeveloper("Sravan") about.AddDeveloper("Dan") wx.AboutBox(about) def Login(self,event): try: s.login(user,passw) wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200)) except: wx.StaticText(self.panel,-1,"Failed",pos=(10,200)) def Send(self,event): to=self.info["To"] subject=self.info["Subject"] message=self.info["Message"] msg="To: "+to+"\nSubject: "+subject+"\n"+message s.sendmail(user,to,msg) s.quit() pymailer() window.MainLoop()
On running, after entering all details (Login--> 'Failed', don't know why) and clicking on 'Send' it says:
•
•
•
•
Traceback (most recent call last):
File "C:\Documents and Settings\sravan953\My Documents\Sravan\PYTHON\GUI.py", line 98, in Send
s.sendmail(user,to,msg)
NameError: global name 'user' is not defined
•
•
Join Date: Jun 2009
Posts: 23
Reputation:
Solved Threads: 1
1
#29 Oct 7th, 2009
Well, there are two problems.
1) in line 98, user should be self.user which will solve the error message. in line 88 user, passw should be self.user, self.passw
2) After fixing those two lines, try printing self.user and self.passw in the login function. When I did that it said that self.user is 'Login ID' and self.passw is 'Password' even after changing the user and password. The problem lies in where you GetValue(), you have it in __init__ where it will get the starting values when it should be in the functions that use it so user input is read.
Here's the code that seems to be working, with the changes from above. I've also added style=wx.TE_PASSWORD in line 46 to mask the password.
1) in line 98, user should be self.user which will solve the error message. in line 88 user, passw should be self.user, self.passw
2) After fixing those two lines, try printing self.user and self.passw in the login function. When I did that it said that self.user is 'Login ID' and self.passw is 'Password' even after changing the user and password. The problem lies in where you GetValue(), you have it in __init__ where it will get the starting values when it should be in the functions that use it so user input is read.
Here's the code that seems to be working, with the changes from above. I've also added style=wx.TE_PASSWORD in line 46 to mask the password.
Python Syntax (Toggle Plain Text)
import smtplib import time import os import wx window=wx.App() s=smtplib.SMTP("smtp.gmail.com",587) s.starttls() class pymailer(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700)) self.info={} self.user="" self.passw="" self.to="" self.subject="" self.message="" self.panel=wx.Panel(self,-1) menubar=wx.MenuBar() filem=wx.Menu() filem.Append(201,"Quit") self.Bind(wx.EVT_MENU,self.Quit,id=201) viewm=wx.Menu() viewm.Append(202,"About") self.Bind(wx.EVT_MENU,self.About,id=202) menubar.Append(filem,"File") menubar.Append(viewm,"Help") self.SetMenuBar(menubar) wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10)) wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100)) wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130)) self.username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100)) self.password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130), style=wx.TE_PASSWORD) login=wx.Button(self.panel,103,label="Login",pos=(10,170)) self.Bind(wx.EVT_BUTTON,self.Login,id=103) wx.StaticText(self.panel,-1,"To:",pos=(10,250)) wx.StaticText(self.panel,-1,"Subject:",pos=(10,280)) wx.StaticText(self.panel,-1,"Message:",pos=(10,310)) self.to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20)) self.sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20)) self.msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150)) send=wx.Button(self.panel,107,"Send",pos=(245,470)) self.Bind(wx.EVT_BUTTON,self.Send,id=107) self.Centre() self.Show() def Quit(self,event): self.Close() def About(self,event): about=wx.AboutDialogInfo() about.SetName("Py-Mailer") about.SetCopyright("(c) 2009 Sravan") about.SetWebSite("http://www.uberpix.wordpress.com") about.AddDeveloper("Sravan") about.AddDeveloper("Dan") wx.AboutBox(about) def Login(self,event): self.user=self.username.GetValue() self.passw=self.password.GetValue() try: s.login(self.user,self.passw) wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200)) except: wx.StaticText(self.panel,-1,"Failed",pos=(10,200)) def Send(self,event): self.info["To"]=self.to_info.GetValue() self.info["Subject"]=self.sbj_info.GetValue() self.info["Message"]=self.msg_info.GetValue() to=self.info["To"] subject=self.info["Subject"] message=self.info["Message"] msg="To: "+to+"\nSubject: "+subject+"\n"+message s.sendmail(self.user,to,msg) s.quit() pymailer() window.MainLoop()
0
#30 Oct 7th, 2009
Thanks, I will surely make the corrections try it out. And, in
wx.TE_PASSWORD , what does TE stand for? ![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- wxPython Events!!!!!!!!???? (Python)
- wxPython: wx.RadioButtons SetValue has no affect in Linux (Python)
- Help Needed with wxPython Configuration (Python)
- wxPython Event Handler (Python)
Other Threads in the Python Forum
- Previous Thread: saving variables to a file in python
- Next Thread: My program (major flaws in it)
| Thread Tools | Search this Thread |
Tag cloud for event, leftmouse, python, wxpython
accessdenied address advanced advice ajax anydbm applet arax beginner c++ class code console convert coordinates copy corners csv curves custom dictionary draw dynamic dynamically edit enter event examples excel exe file filename function google gui handler images input ip jaunty java launcher linux list listener lists module movingimageswithpygame multiple newb numbers opensource parameters path programming projects push py py-mailer py2exe pygame pygtk python rails random raw_input read recursion recursive redirect return reverse ruby server simple software sqlite ssh string strings table thread threading time tkinter tricks tutorial twitter ubuntu unicode urllib urllib2 variable verify web-scrape whileloop windows wordgame write wxpython







