473,385 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to get event id's in wxPython

51
I am currently trying to write a program where all events will be handled by one function.To do this appropriately i believe i would need to use 'if' statements to create an output depending on the widget pressed.So i tried to use the following:
Expand|Select|Wrap|Line Numbers
  1. id=event.GetId()
However when i try to run the program
i get the following error:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.                 File "C:\Python25\CalculatorBlueprint.py",line 64,in handler
  3.                    id=event.GetId()
  4. NameError:global name 'event' is not defined
Can anyone show me where i have gone wrong?By the way i use python25 with IDLE and the wx module.
Apr 18 '07 #1
6 4007
bartonc
6,596 Expert 4TB
I am currently trying to write a program where all events will be handled by one function.To do this appropriately i believe i would need to use 'if' statements to create an output depending on the widget pressed
First of all, DON'T DO IT. It may seem like a fun project, but events/event handlers are designed to make programming easy and programs (sort of) easy to debug. Sometimes events share certain functions. Those are found through a process called "refactoring" whereby you go through and eliminate duplication AFTER your program works.

Secondly, event handlers take two arguments: self and event.
You can call your own class methods from within the handler and pass the event to it if it needs event info:
Expand|Select|Wrap|Line Numbers
  1. def OnThisEvent(self, event):
  2.     self.OnThatEvent(event)
Let's go back to the basic example, shall we:
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import os
  3. import re
  4.  
  5. # Don't hard code ID nubbers! Use NewID()!
  6. [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
  7. ] = [wx.NewId() for _init_ctrls in range(3)]
  8.  
  9.  
  10. class Mine(wx.Frame):
  11.     def __init__(self, parent, title, pos, size,):
  12.         wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
  13.         self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
  14.         self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
  15.         # Need to Bind() to the event
  16.         self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
  17.  
  18.         self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
  19.         # Need to Bind() to the event
  20.         self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
  21.  
  22. ##        wx.EVT_BUTTON(self,2,self.disp)
  23. ##        wx.EVT_BUTTON(self,3,self.end)
  24.  
  25.         self.Show(True)
  26.  
  27.     # Use better names for objects and handlers (conventionally start with "On").
  28.  
  29.     def OnButton1(self, event):
  30.         x = self.InputTextCtrl.GetValue()
  31. ##        d=z(x)
  32.         f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
  33.         answer = f.ShowModal()
  34.         if answer == wx.ID_OK:
  35.             print "OK"
  36.         f.Destroy()
  37.  
  38.     def OnButton2(self, event):  # event was left out
  39.         # let wx cleanup! #
  40.         self.Destroy()
  41. ##        raise SystemExit()
  42.  
  43.  
  44.  
  45. app=wx.PySimpleApp()
  46. frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
  47. app.MainLoop()
Apr 18 '07 #2
bartonc
6,596 Expert 4TB
I am currently trying to write a program where all events will be handled by one function.To do this appropriately i believe i would need to use 'if' statements to create an output depending on the widget pressed
First of all, DON'T DO IT. It may seem like a fun project, but events/event handlers are designed to make programming easy and programs (sort of) easy to debug. Sometimes events share certain functions. Those are found through a process called "refactoring" whereby you go through and eliminate duplication AFTER your program works.

Secondly, event handlers take two arguments: self and event.
You can call your own class methods from within the handler and pass the event to it if it needs event info:
Expand|Select|Wrap|Line Numbers
  1. def OnThisEvent(self, event):
  2.     self.OnThatEvent(event)
Let's go back to the basic example, shall we:
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import os
  3. import re
  4.  
  5. # Don't hard code ID nubbers! Use NewID()!
  6. [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
  7. ] = [wx.NewId() for _init_ctrls in range(3)]
  8.  
  9.  
  10. class Mine(wx.Frame):
  11.     def __init__(self, parent, title, pos, size,):
  12.         wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
  13.         self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
  14.         self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
  15.         # Need to Bind() to the event
  16.         self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
  17.  
  18.         self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
  19.         # Need to Bind() to the event
  20.         self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
  21.  
  22. ##        wx.EVT_BUTTON(self,2,self.disp)
  23. ##        wx.EVT_BUTTON(self,3,self.end)
  24.  
  25.         self.Show(True)
  26.  
  27.     # Use better names for objects and handlers (conventionally start with "On").
  28.  
  29.     def OnButton1(self, event):
  30.         x = self.InputTextCtrl.GetValue()
  31. ##        d=z(x)
  32.         f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
  33.         answer = f.ShowModal()
  34.         if answer == wx.ID_OK:
  35.             print "OK"
  36.         f.Destroy()
  37.  
  38.     def OnButton2(self, event):  # event was left out
  39.         # let wx cleanup! #
  40.         self.Destroy()
  41. ##        raise SystemExit()
  42.  
  43.  
  44.  
  45. app=wx.PySimpleApp()
  46. frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
  47. app.MainLoop()
wazzup
Something is wrong - perhaps a site bug - will investigate
Apr 18 '07 #3
dynamo
51
First of all, DON'T DO IT. It may seem like a fun project, but events/event handlers are designed to make programming easy and programs (sort of) easy to debug. Sometimes events share certain functions. Those are found through a process called "refactoring" whereby you go through and eliminate duplication AFTER your program works.

Secondly, event handlers take two arguments: self and event.
You can call your own class methods from within the handler and pass the event to it if it needs event info:
Expand|Select|Wrap|Line Numbers
  1. def OnThisEvent(self, event):
  2.     self.OnThatEvent(event)
Let's go back to the basic example, shall we:
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import os
  3. import re
  4.  
  5. # Don't hard code ID nubbers! Use NewID()!
  6. [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
  7. ] = [wx.NewId() for _init_ctrls in range(3)]
  8.  
  9.  
  10. class Mine(wx.Frame):
  11.     def __init__(self, parent, title, pos, size,):
  12.         wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
  13.         self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
  14.         self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
  15.         # Need to Bind() to the event
  16.         self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
  17.  
  18.         self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
  19.         # Need to Bind() to the event
  20.         self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
  21.  
  22. ##        wx.EVT_BUTTON(self,2,self.disp)
  23. ##        wx.EVT_BUTTON(self,3,self.end)
  24.  
  25.         self.Show(True)
  26.  
  27.     # Use better names for objects and handlers (conventionally start with "On").
  28.  
  29.     def OnButton1(self, event):
  30.         x = self.InputTextCtrl.GetValue()
  31. ##        d=z(x)
  32.         f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
  33.         answer = f.ShowModal()
  34.         if answer == wx.ID_OK:
  35.             print "OK"
  36.         f.Destroy()
  37.  
  38.     def OnButton2(self, event):  # event was left out
  39.         # let wx cleanup! #
  40.         self.Destroy()
  41. ##        raise SystemExit()
  42.  
  43.  
  44.  
  45. app=wx.PySimpleApp()
  46. frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
  47. app.MainLoop()
Thanks for replying.I understand the point you are making but my problem is i am trying to write a calculator program and if i define a function for each event,i will have to define so many functions.What really disturbs me is that i have seen 'event.GetId()' used in other programs but it doesn't seem to work in mine.
Apr 18 '07 #4
bartonc
6,596 Expert 4TB
Thanks for replying.I understand the point you are making but my problem is i am trying to write a calculator program and if i define a function for each event,i will have to define so many functions.What really disturbs me is that i have seen 'event.GetId()' used in other programs but it doesn't seem to work in mine.
Ok. That sounds smart. Here's what you're looking for:
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import os
  3. import re
  4.  
  5. # Don't hard code ID nubbers! Use NewID()!
  6. [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
  7. ] = [wx.NewId() for _init_ctrls in range(3)]
  8.  
  9.  
  10.  
  11. class Mine(wx.Frame):
  12.     def __init__(self, parent, title, pos, size,):
  13.         wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
  14.         self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
  15.         self.Button1 = wx.Button(self, wxID_BUTTON1, '1', pos=(50,50))
  16.         # Need to Bind() to the event
  17.         self.Button1.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON1)
  18.  
  19.         self.Button2 = wx.Button(self, wxID_BUTTON2, '2', pos=(120,50))
  20.         # Need to Bind() to the event
  21.         self.Button2.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON2)
  22.  
  23.  
  24.         # just one way to give a button a certain value
  25.         self.buttonDict = {self.Button1:1, self.Button2:2}
  26.  
  27.     def OnAnyButton(self, event):
  28.         thisButton = event.GetEventObject()
  29.         print self.buttonDict[thisButton]
  30.  
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     app = wx.PySimpleApp()
  35.     frame = Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
  36.     frame.Show(True)
  37.     app.MainLoop()
  38.  
Apr 19 '07 #5
dynamo
51
Ok. That sounds smart. Here's what you're looking for:
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import os
  3. import re
  4.  
  5. # Don't hard code ID nubbers! Use NewID()!
  6. [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
  7. ] = [wx.NewId() for _init_ctrls in range(3)]
  8.  
  9.  
  10.  
  11. class Mine(wx.Frame):
  12.     def __init__(self, parent, title, pos, size,):
  13.         wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
  14.         self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
  15.         self.Button1 = wx.Button(self, wxID_BUTTON1, '1', pos=(50,50))
  16.         # Need to Bind() to the event
  17.         self.Button1.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON1)
  18.  
  19.         self.Button2 = wx.Button(self, wxID_BUTTON2, '2', pos=(120,50))
  20.         # Need to Bind() to the event
  21.         self.Button2.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON2)
  22.  
  23.  
  24.         # just one way to give a button a certain value
  25.         self.buttonDict = {self.Button1:1, self.Button2:2}
  26.  
  27.     def OnAnyButton(self, event):
  28.         thisButton = event.GetEventObject()
  29.         print self.buttonDict[thisButton]
  30.  
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     app = wx.PySimpleApp()
  35.     frame = Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
  36.     frame.Show(True)
  37.     app.MainLoop()
  38.  
So for 'event' to be recognized you have to bind the object to the appropriate event.Thanks for saving me again
Apr 19 '07 #6
bartonc
6,596 Expert 4TB
So for 'event' to be recognized you have to bind the object to the appropriate event.Thanks for saving me again
You are quite welcome.

Keep posting,
Barton
Apr 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: wang xiaoyu | last post by:
Hello: i want use activex in wxpython program,but when i use MakeActiveXClass an exception occurs. this is my source code dealing the DICOM ocx.I must note that in this program "hwtxcontrol" is...
3
by: Robert | last post by:
Hello list, could somebody point me to a good reference about wxPython event handling? I have seen many examples but which one is the best. Waht are the advantages and disadvantages? Can you...
0
by: mee-shell | last post by:
Hi, I am a python newbie. I have created a tool for capturing the screen shot. I want to be able to do the screen capture with the keydown event as well. The tool worked fine when it first...
2
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming...
1
by: Raghul | last post by:
hi, I am developing a jabber client.What I need is whrn i enter text in the text area and when I press return key. The following text should be send.I found the way to send the message, the only...
0
by: gopython | last post by:
Hi, I just started coding with wxPython. The idea is to be able to change the gui dynamically. I am trying to change the static text inside the panel after the panel is created without user...
5
by: dynamo | last post by:
Hi everyone.I'm new to wxpython.And i have a little problem.I tried to run the following wxpython app code in IDLE for python25: import wx import os import re def z(a): for i in...
2
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
4
by: analfabete | last post by:
Hi everybody! I really need your help I have created a program with events. So when my event is turn on by click on a menu , the parent windows move: My code: class MenuOptions(wx.Menu): def...
3
by: lee.walczak | last post by:
Hi, I have just started writing a GUI using wxpython after finding a limitation using Tkinter. I have read most tutorials on wxpython and slowly becoming accustomed considering I started with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.