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:
However when i try to run the program
i get the following error: - Traceback (most recent call last):
-
File "C:\Python25\CalculatorBlueprint.py",line 64,in handler
-
id=event.GetId()
-
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.
6 4007
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: - def OnThisEvent(self, event):
-
self.OnThatEvent(event)
Let's go back to the basic example, shall we: - import wx
-
import os
-
import re
-
-
# Don't hard code ID nubbers! Use NewID()!
-
[wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
-
] = [wx.NewId() for _init_ctrls in range(3)]
-
-
-
class Mine(wx.Frame):
-
def __init__(self, parent, title, pos, size,):
-
wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
-
self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
-
self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
-
# Need to Bind() to the event
-
self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
-
-
self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
-
# Need to Bind() to the event
-
self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
-
-
## wx.EVT_BUTTON(self,2,self.disp)
-
## wx.EVT_BUTTON(self,3,self.end)
-
-
self.Show(True)
-
-
# Use better names for objects and handlers (conventionally start with "On").
-
-
def OnButton1(self, event):
-
x = self.InputTextCtrl.GetValue()
-
## d=z(x)
-
f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
-
answer = f.ShowModal()
-
if answer == wx.ID_OK:
-
print "OK"
-
f.Destroy()
-
-
def OnButton2(self, event): # event was left out
-
# let wx cleanup! #
-
self.Destroy()
-
## raise SystemExit()
-
-
-
-
app=wx.PySimpleApp()
-
frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
-
app.MainLoop()
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: - def OnThisEvent(self, event):
-
self.OnThatEvent(event)
Let's go back to the basic example, shall we: - import wx
-
import os
-
import re
-
-
# Don't hard code ID nubbers! Use NewID()!
-
[wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
-
] = [wx.NewId() for _init_ctrls in range(3)]
-
-
-
class Mine(wx.Frame):
-
def __init__(self, parent, title, pos, size,):
-
wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
-
self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
-
self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
-
# Need to Bind() to the event
-
self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
-
-
self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
-
# Need to Bind() to the event
-
self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
-
-
## wx.EVT_BUTTON(self,2,self.disp)
-
## wx.EVT_BUTTON(self,3,self.end)
-
-
self.Show(True)
-
-
# Use better names for objects and handlers (conventionally start with "On").
-
-
def OnButton1(self, event):
-
x = self.InputTextCtrl.GetValue()
-
## d=z(x)
-
f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
-
answer = f.ShowModal()
-
if answer == wx.ID_OK:
-
print "OK"
-
f.Destroy()
-
-
def OnButton2(self, event): # event was left out
-
# let wx cleanup! #
-
self.Destroy()
-
## raise SystemExit()
-
-
-
-
app=wx.PySimpleApp()
-
frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
-
app.MainLoop()
wazzup
Something is wrong - perhaps a site bug - will investigate
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: - def OnThisEvent(self, event):
-
self.OnThatEvent(event)
Let's go back to the basic example, shall we: - import wx
-
import os
-
import re
-
-
# Don't hard code ID nubbers! Use NewID()!
-
[wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
-
] = [wx.NewId() for _init_ctrls in range(3)]
-
-
-
class Mine(wx.Frame):
-
def __init__(self, parent, title, pos, size,):
-
wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
-
self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
-
self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
-
# Need to Bind() to the event
-
self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
-
-
self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
-
# Need to Bind() to the event
-
self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
-
-
## wx.EVT_BUTTON(self,2,self.disp)
-
## wx.EVT_BUTTON(self,3,self.end)
-
-
self.Show(True)
-
-
# Use better names for objects and handlers (conventionally start with "On").
-
-
def OnButton1(self, event):
-
x = self.InputTextCtrl.GetValue()
-
## d=z(x)
-
f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
-
answer = f.ShowModal()
-
if answer == wx.ID_OK:
-
print "OK"
-
f.Destroy()
-
-
def OnButton2(self, event): # event was left out
-
# let wx cleanup! #
-
self.Destroy()
-
## raise SystemExit()
-
-
-
-
app=wx.PySimpleApp()
-
frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
-
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.
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: - import wx
-
import os
-
import re
-
-
# Don't hard code ID nubbers! Use NewID()!
-
[wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
-
] = [wx.NewId() for _init_ctrls in range(3)]
-
-
-
-
class Mine(wx.Frame):
-
def __init__(self, parent, title, pos, size,):
-
wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
-
self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
-
self.Button1 = wx.Button(self, wxID_BUTTON1, '1', pos=(50,50))
-
# Need to Bind() to the event
-
self.Button1.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON1)
-
-
self.Button2 = wx.Button(self, wxID_BUTTON2, '2', pos=(120,50))
-
# Need to Bind() to the event
-
self.Button2.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON2)
-
-
-
# just one way to give a button a certain value
-
self.buttonDict = {self.Button1:1, self.Button2:2}
-
-
def OnAnyButton(self, event):
-
thisButton = event.GetEventObject()
-
print self.buttonDict[thisButton]
-
-
-
-
if __name__ == "__main__":
-
app = wx.PySimpleApp()
-
frame = Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
-
frame.Show(True)
-
app.MainLoop()
-
Ok. That sounds smart. Here's what you're looking for: - import wx
-
import os
-
import re
-
-
# Don't hard code ID nubbers! Use NewID()!
-
[wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
-
] = [wx.NewId() for _init_ctrls in range(3)]
-
-
-
-
class Mine(wx.Frame):
-
def __init__(self, parent, title, pos, size,):
-
wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
-
self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
-
self.Button1 = wx.Button(self, wxID_BUTTON1, '1', pos=(50,50))
-
# Need to Bind() to the event
-
self.Button1.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON1)
-
-
self.Button2 = wx.Button(self, wxID_BUTTON2, '2', pos=(120,50))
-
# Need to Bind() to the event
-
self.Button2.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON2)
-
-
-
# just one way to give a button a certain value
-
self.buttonDict = {self.Button1:1, self.Button2:2}
-
-
def OnAnyButton(self, event):
-
thisButton = event.GetEventObject()
-
print self.buttonDict[thisButton]
-
-
-
-
if __name__ == "__main__":
-
app = wx.PySimpleApp()
-
frame = Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
-
frame.Show(True)
-
app.MainLoop()
-
So for 'event' to be recognized you have to bind the object to the appropriate event.Thanks for saving me again
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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...
| |