wxPython Events help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: wxPython Events help

 
0
  #21
Oct 4th, 2009
Also, on clicking "Send" after filling up everything correctly, it shows this 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
What should I do to solve this? Can anyone please explain to me about the scope of the variables which are causing the error?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: wxPython Events help

 
0
  #22
Oct 4th, 2009
  1. msg="To: "+to+"\nSubject: "+subject+"\n"+message
There a are variable names to and subject that need some kind of string value. Those things do not come out of thin air!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: wxPython Events help

 
0
  #23
Oct 4th, 2009
Originally Posted by sravan953 View Post
  1. wx.StaticText(self.panel,-1,"To:",pos=(10,250))
  2. wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
  3. wx.StaticText(self.panel,-1,"Message:",pos=(10,310)) toadd=wx.TextCtrl(self.panel,104,"<username@domain.server>",pos=(80,250),size=(240,20))
  4. to=toadd.GetValue()
  5. subj=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
  6. subject=subj.GetValue()
  7. mess=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
  8. message=mess.GetValue()
  9. def Send(self,event):
  10. msg="To: "+to+"\nSubject: "+subject+"\n"+message
  11. s.sendmail(us,to,msg)
  12. s.quit()
I have to in line 4. Even then it says that the global variable isn't defined!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training
 
0
  #24
Oct 6th, 2009
I defined 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:
  1. import smtplib
  2. import time
  3. import os
  4. import wx
  5.  
  6. window=wx.App()
  7.  
  8. s=smtplib.SMTP("smtp.gmail.com",587)
  9. s.starttls()
  10.  
  11. info={}
  12. user=""
  13. passw=""
  14.  
  15. class pymailer(wx.Frame):
  16.  
  17. def __init__(self):
  18. wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))
  19.  
  20. self.panel=wx.Panel(self,-1)
  21.  
  22. menubar=wx.MenuBar()
  23.  
  24. filem=wx.Menu()
  25. filem.Append(201,"Quit")
  26. self.Bind(wx.EVT_MENU,self.Quit,id=201)
  27.  
  28. viewm=wx.Menu()
  29. viewm.Append(202,"About")
  30. self.Bind(wx.EVT_MENU,self.About,id=202)
  31.  
  32. menubar.Append(filem,"File")
  33. menubar.Append(viewm,"Help")
  34.  
  35. self.SetMenuBar(menubar)
  36.  
  37. 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))
  38. wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
  39. wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))
  40.  
  41. username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
  42. user=username.GetValue()
  43.  
  44. password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
  45. passw=password.GetValue()
  46.  
  47. login=wx.Button(self.panel,103,label="Login",pos=(10,170))
  48. self.Bind(wx.EVT_BUTTON,self.Login,id=103)
  49.  
  50. wx.StaticText(self.panel,-1,"To:",pos=(10,250))
  51. wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
  52. wx.StaticText(self.panel,-1,"Message:",pos=(10,310))
  53.  
  54. to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
  55. info["To"]=to_info.GetValue()
  56.  
  57. sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
  58. info["Subject"]=sbj_info.GetValue()
  59.  
  60. msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
  61. info["Message"]=msg_info.GetValue()
  62.  
  63. send=wx.Button(self.panel,107,"Send",pos=(245,470))
  64. self.Bind(wx.EVT_BUTTON,self.Send,id=107)
  65.  
  66. self.Centre()
  67. self.Show()
  68.  
  69. def Quit(self,event):
  70. self.Close()
  71.  
  72. def About(self,event):
  73. about=wx.AboutDialogInfo()
  74.  
  75. about.SetName("Py-Mailer")
  76. about.SetCopyright("(c) 2009 Sravan")
  77. about.SetWebSite("http://www.uberpix.wordpress.com")
  78. about.AddDeveloper("Sravan")
  79. about.AddDeveloper("Dan")
  80.  
  81. wx.AboutBox(about)
  82.  
  83. def Login(self,event):
  84. try:
  85. s.login(user,passw)
  86. wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200))
  87. except:
  88. wx.StaticText(self.panel,-1,"Failed",pos=(10,200))
  89.  
  90. def Send(self,event):
  91. to=info["To"]
  92. subject=info["Subject"]
  93. message=info["Message"]
  94. msg="To: "+to+"\nSubject: "+subject+"\n"+message
  95. s.sendmail(user,to,msg)
  96. s.quit()
  97.  
  98. pymailer()
  99. window.MainLoop()

Why doesn't it work if I define the variables in the class?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 23
Reputation: winmic is an unknown quantity at this point 
Solved Threads: 1
winmic winmic is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training
 
0
  #26
Oct 6th, 2009
Here are the first few lines, I did as you told:
  1. class pymailer(wx.Frame):
  2.  
  3. self.info={}
  4. self.user=""
  5. self.passw=""
  6.  
  7. def __init__(self):
  8. .
  9. .
  10. .
  11. password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
  12. self.passw=password.GetValue()

But it still doesn't work...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 23
Reputation: winmic is an unknown quantity at this point 
Solved Threads: 1
winmic winmic is offline Offline
Newbie Poster
 
0
  #27
Oct 6th, 2009
What do you mean? Does it have an error message or does it not work as expected?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training
 
0
  #28
Oct 6th, 2009
This is the full code for reference:
  1. import smtplib
  2. import time
  3. import os
  4. import wx
  5.  
  6. window=wx.App()
  7.  
  8. s=smtplib.SMTP("smtp.gmail.com",587)
  9. s.starttls()
  10.  
  11. class pymailer(wx.Frame):
  12.  
  13. def __init__(self):
  14. wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))
  15.  
  16. self.info={}
  17. self.user=""
  18. self.passw=""
  19. self.to=""
  20. self.subject=""
  21. self.message=""
  22.  
  23. self.panel=wx.Panel(self,-1)
  24.  
  25. menubar=wx.MenuBar()
  26.  
  27. filem=wx.Menu()
  28. filem.Append(201,"Quit")
  29. self.Bind(wx.EVT_MENU,self.Quit,id=201)
  30.  
  31. viewm=wx.Menu()
  32. viewm.Append(202,"About")
  33. self.Bind(wx.EVT_MENU,self.About,id=202)
  34.  
  35. menubar.Append(filem,"File")
  36. menubar.Append(viewm,"Help")
  37.  
  38. self.SetMenuBar(menubar)
  39.  
  40. 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))
  41. wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
  42. wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))
  43.  
  44. username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
  45. self.user=username.GetValue()
  46.  
  47. password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
  48. self.passw=password.GetValue()
  49.  
  50. login=wx.Button(self.panel,103,label="Login",pos=(10,170))
  51. self.Bind(wx.EVT_BUTTON,self.Login,id=103)
  52.  
  53. wx.StaticText(self.panel,-1,"To:",pos=(10,250))
  54. wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
  55. wx.StaticText(self.panel,-1,"Message:",pos=(10,310))
  56.  
  57. to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
  58. self.info["To"]=to_info.GetValue()
  59.  
  60. sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
  61. self.info["Subject"]=sbj_info.GetValue()
  62.  
  63. msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
  64. self.info["Message"]=msg_info.GetValue()
  65.  
  66. send=wx.Button(self.panel,107,"Send",pos=(245,470))
  67. self.Bind(wx.EVT_BUTTON,self.Send,id=107)
  68.  
  69. self.Centre()
  70. self.Show()
  71.  
  72. def Quit(self,event):
  73. self.Close()
  74.  
  75. def About(self,event):
  76. about=wx.AboutDialogInfo()
  77.  
  78. about.SetName("Py-Mailer")
  79. about.SetCopyright("(c) 2009 Sravan")
  80. about.SetWebSite("http://www.uberpix.wordpress.com")
  81. about.AddDeveloper("Sravan")
  82. about.AddDeveloper("Dan")
  83.  
  84. wx.AboutBox(about)
  85.  
  86. def Login(self,event):
  87. try:
  88. s.login(user,passw)
  89. wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200))
  90. except:
  91. wx.StaticText(self.panel,-1,"Failed",pos=(10,200))
  92.  
  93. def Send(self,event):
  94. to=self.info["To"]
  95. subject=self.info["Subject"]
  96. message=self.info["Message"]
  97. msg="To: "+to+"\nSubject: "+subject+"\n"+message
  98. s.sendmail(user,to,msg)
  99. s.quit()
  100.  
  101. pymailer()
  102. 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 23
Reputation: winmic is an unknown quantity at this point 
Solved Threads: 1
winmic winmic is offline Offline
Newbie Poster
 
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. import smtplib
  2. import time
  3. import os
  4. import wx
  5.  
  6. window=wx.App()
  7.  
  8. s=smtplib.SMTP("smtp.gmail.com",587)
  9. s.starttls()
  10.  
  11. class pymailer(wx.Frame):
  12.  
  13. def __init__(self):
  14. wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))
  15.  
  16. self.info={}
  17. self.user=""
  18. self.passw=""
  19. self.to=""
  20. self.subject=""
  21. self.message=""
  22.  
  23. self.panel=wx.Panel(self,-1)
  24.  
  25. menubar=wx.MenuBar()
  26.  
  27. filem=wx.Menu()
  28. filem.Append(201,"Quit")
  29. self.Bind(wx.EVT_MENU,self.Quit,id=201)
  30.  
  31. viewm=wx.Menu()
  32. viewm.Append(202,"About")
  33. self.Bind(wx.EVT_MENU,self.About,id=202)
  34.  
  35. menubar.Append(filem,"File")
  36. menubar.Append(viewm,"Help")
  37.  
  38. self.SetMenuBar(menubar)
  39.  
  40. 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))
  41. wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
  42. wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))
  43.  
  44. self.username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
  45.  
  46. self.password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130), style=wx.TE_PASSWORD)
  47.  
  48. login=wx.Button(self.panel,103,label="Login",pos=(10,170))
  49. self.Bind(wx.EVT_BUTTON,self.Login,id=103)
  50.  
  51. wx.StaticText(self.panel,-1,"To:",pos=(10,250))
  52. wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
  53. wx.StaticText(self.panel,-1,"Message:",pos=(10,310))
  54.  
  55. self.to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
  56.  
  57. self.sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
  58.  
  59. self.msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
  60.  
  61. send=wx.Button(self.panel,107,"Send",pos=(245,470))
  62. self.Bind(wx.EVT_BUTTON,self.Send,id=107)
  63.  
  64. self.Centre()
  65. self.Show()
  66.  
  67. def Quit(self,event):
  68. self.Close()
  69.  
  70. def About(self,event):
  71. about=wx.AboutDialogInfo()
  72.  
  73. about.SetName("Py-Mailer")
  74. about.SetCopyright("(c) 2009 Sravan")
  75. about.SetWebSite("http://www.uberpix.wordpress.com")
  76. about.AddDeveloper("Sravan")
  77. about.AddDeveloper("Dan")
  78.  
  79. wx.AboutBox(about)
  80.  
  81. def Login(self,event):
  82. self.user=self.username.GetValue()
  83. self.passw=self.password.GetValue()
  84. try:
  85. s.login(self.user,self.passw)
  86. wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200))
  87. except:
  88. wx.StaticText(self.panel,-1,"Failed",pos=(10,200))
  89.  
  90. def Send(self,event):
  91. self.info["To"]=self.to_info.GetValue()
  92. self.info["Subject"]=self.sbj_info.GetValue()
  93. self.info["Message"]=self.msg_info.GetValue()
  94. to=self.info["To"]
  95. subject=self.info["Subject"]
  96. message=self.info["Message"]
  97. msg="To: "+to+"\nSubject: "+subject+"\n"+message
  98. s.sendmail(self.user,to,msg)
  99. s.quit()
  100.  
  101. pymailer()
  102. window.MainLoop()
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training
 
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?
Reply With Quote Quick reply to this message  
Reply

Tags
event, leftmouse, python, wxpython

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for event, leftmouse, python, wxpython
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC