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

wxPython Events help

 
-1
  #1
Sep 22nd, 2009
Hey guys,

I have made a small program called 'Py-mailer' which allows you to login with your Gmail credentials and send text-only messages to anyone. I uploaded it on SourceForge( http://www.tinyurl.com/m4ans4).

The next step I had to take was to make a GUI for it. So, I started with wxPython:
  1. import wx
  2.  
  3. window=wx.App()
  4.  
  5. class pymailer(wx.Frame):
  6. def __init__(self):
  7. wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))
  8.  
  9. panel=wx.Panel(self,-1)
  10.  
  11. menu=wx.MenuBar()
  12. items=wx.Menu()
  13.  
  14. items.Append(201,"Quit")
  15. self.Bind(wx.EVT_LEFT_DOWN,self.Quit,id=201)
  16.  
  17. menu.Append(items,"File")
  18. self.SetMenuBar(menu)
  19.  
  20. wx.StaticText(panel,-1,"Please enter your Gmail login ID: ",pos=(10,10))
  21. wx.StaticText(panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,40))
  22.  
  23. username=wx.TextCtrl(panel,101,"Login ID",pos=(220,10))
  24. password=wx.TextCtrl(panel,102,"Password",pos=(220,40))
  25.  
  26. self.Centre()
  27. self.Show()
  28.  
  29. def Quit(self,event):
  30. self.Close()
  31.  
  32. pymailer()
  33. window.MainLoop()

But, on clicking 'Quit' in the 'File' menu, nothing happens! I think it is because in line 29, I have not passed the ID of the event, so how do I pass the ID? Or, is because of some thing else?

Thanks
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

Re: wxPython Events help

 
0
  #2
Sep 22nd, 2009
You need to bind the event to the menu, not left_click. Try self.Bind(wx.EVT_MENU,self.Quit,id=201) instead of
self.Bind(wx.EVT_LEFT_DOWN,self.Quit,id=201)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 176
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 51
snippsat's Avatar
snippsat snippsat is offline Offline
Junior Poster

Re: wxPython Events help

 
0
  #3
Sep 22nd, 2009
Change to this.
  1. items.Append(201,"Quit")
  2. self.Bind(wx.EVT_MENU,self.Quit,id=201)
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
  #4
Sep 22nd, 2009
Originally Posted by winmic View Post
You need to bind the event to the menu, not left_click. Try self.Bind(wx.EVT_MENU,self.Quit,id=201) instead of
self.Bind(wx.EVT_LEFT_DOWN,self.Quit,id=201)
Thanks mate, it worked!
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
  #5
Sep 22nd, 2009
Thanks snippsat, it worked!
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
  #6
Sep 22nd, 2009
I have advanced till here:
  1. import smtplib
  2. import time
  3. import os
  4. import wx
  5.  
  6. window=wx.App()
  7.  
  8. class pymailer(wx.Frame):
  9. def __init__(self):
  10. wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))
  11.  
  12. panel=wx.Panel(self,-1)
  13.  
  14. menu=wx.MenuBar()
  15. items=wx.Menu()
  16.  
  17. items.Append(201,"Quit")
  18. self.Bind(wx.EVT_MENU,self.Quit)
  19.  
  20. menu.Append(items,"File")
  21. self.SetMenuBar(menu)
  22.  
  23. wx.StaticText(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))
  24. wx.StaticText(panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
  25. wx.StaticText(panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))
  26.  
  27. username=wx.TextCtrl(panel,101,"Login ID",pos=(220,100))
  28. password=wx.TextCtrl(panel,102,"Password",pos=(220,130))
  29.  
  30. login=wx.Button(panel,103,label="Login",pos=(10,170))
  31. self.Bind(wx.EVT_LEFT_DOWN,self.login)
  32.  
  33. self.Centre()
  34. self.Show()
  35.  
  36. def Quit(self,event):
  37. self.Close()
  38.  
  39. def login(self,event):
  40. s=smtplib.SMTP("smtp.gmail.com",587)
  41. s.starttls()
  42.  
  43. try:
  44. wx.StaticText(panel,-1,"Logging in...",pos=(10,200))
  45. s.login(user,passw)
  46. except:
  47. wx.StaticText(panel,-1,"Failed",pos=(10,220))
  48.  
  49. pymailer()
  50. window.MainLoop()

But, after entering the login details and clicking on 'Login', nothing happens. Any help?

Thank you
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

Re: wxPython Events help

 
1
  #7
Sep 22nd, 2009
use self.Bind(wx.EVT_BUTTON, self.login) instead of self.Bind(wx.EVT_LEFT_DOWN,self.login)

also everywhere you put 'panel' put 'self.panel' instead so it can be accessed during login
Last edited by winmic; Sep 22nd, 2009 at 1:50 pm. Reason: found bug
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
  #8
Sep 22nd, 2009
What's the difference between EVT_BUTTON and EVT_LEFT_DOWN ?

Also, can you explain to me why I should use self.panel ?
Last edited by sravan953; Sep 22nd, 2009 at 11:02 pm. Reason: Type error
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

Re: wxPython Events help

 
0
  #9
Sep 23rd, 2009
EVT_LEFT_DOWN doesn't work because wx.Button doesn't propagate EVT_LEFT_DOWN, it will only propagate wx.EVT_BUTTON. The thing is that objects like Button and ListCtrl will only propagate certain events, not all events. Take a look at the wxpython docs to see what events are used. http://www.wxpython.org/docs/api/wx.Button-class.html

As for using self.panel, in the constructor the variable panel is created, but when the constructor is done, panel is also destroyed. So in the login function, when 'panel' is used it errors because panel was destroyed. Using self.panel will allow access to the variable throughout the class until the class is destroyed. I'm not sure if I'm explaining it well enough, but if there are lots of tutorials, ect. on class structure floating about the web that can help.

Good luck, and I'm looking forward to seeing this in action!
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
  #10
Sep 23rd, 2009
winmic, if I use panel , it gives this error:
Traceback (most recent call last):
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 52, in Login
wx.StaticText(panel,-1,"Failed",pos=(10,220))
NameError: global name 'panel' is not defined
If I use self.panel , it gives this error:
Traceback (most recent call last):
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 64, in <module>
pymailer()
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 36, in __init__
login=wx.Button(self.panel,103,label="Login",pos=(10,170))
AttributeError: 'pymailer' object has no attribute 'panel
Anyways, this is the code till now:
  1. import smtplib
  2. import time
  3. import os
  4. import wx
  5.  
  6. window=wx.App()
  7.  
  8. class pymailer(wx.Frame):
  9. def __init__(self):
  10. wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))
  11.  
  12. panel=wx.Panel(self,-1)
  13.  
  14. menubar=wx.MenuBar()
  15.  
  16. filem=wx.Menu()
  17. filem.Append(201,"Quit")
  18. self.Bind(wx.EVT_MENU,self.Quit)
  19.  
  20. viewm=wx.Menu()
  21. viewm.Append(202,"About")
  22. self.Bind(wx.EVT_MENU,self.About)
  23.  
  24. menubar.Append(filem,"File")
  25. menubar.Append(viewm,"Help")
  26.  
  27. self.SetMenuBar(menubar)
  28.  
  29. wx.StaticText(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))
  30. wx.StaticText(panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
  31. wx.StaticText(panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))
  32.  
  33. username=wx.TextCtrl(panel,101,"Login ID",pos=(220,100))
  34. password=wx.TextCtrl(panel,102,"Password",pos=(220,130))
  35.  
  36. login=wx.Button(self.panel,103,label="Login",pos=(10,170))
  37. self.Bind(wx.EVT_BUTTON,self.Login)
  38.  
  39. self.Centre()
  40. self.Show()
  41.  
  42. def Quit(self,event):
  43. self.Close()
  44.  
  45. def Login(self,event):
  46. s=smtplib.SMTP("smtp.gmail.com",587)
  47. s.starttls()
  48. try:
  49. wx.StaticText(panel,-1,"Logging in...",pos=(10,200))
  50. s.login(user,passw)
  51. except:
  52. wx.StaticText(panel,-1,"Failed",pos=(10,220))
  53.  
  54. def About(self,event):
  55. about=wx.AboutDialogInfo()
  56.  
  57. about.SetName("Py-Mailer")
  58. about.SetCopyright("(c) 2009 Sravan")
  59. about.SetWebSite("http://www.uberpix.wordpress.com")
  60. about.AddDeveloper("Sravan\nDan")
  61.  
  62. wx.AboutBox(about)
  63.  
  64. pymailer()
  65. window.MainLoop()

Also, when I used self.panel , I replaced panel with self.panel everywhere, just thought I'd let you know!

Any other bugs in the code?

Thanks
Last edited by sravan953; Sep 23rd, 2009 at 6:09 am. Reason: Mistake
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