Hi, I'm new to this list, and I've searched the archives for an answer (as well as the net in general), but I'm stuck and need help.
I'm just beginning with wxPython, and I've been using Greg Brunet's "SmallApp" sample application as a base for testing. (A big "Thank You" to Greg for providing that!)
After adding a few controls successfully, I tried to add a wxRadioBox, but can't seem to catch the event. I'm including the whole source below, just in case. If this is bad form on this list, please don't hestitate to flame me--it's so hot here, I don't think I'd notice anyway...
I'm running this on a Win2000 system with Python 2.3 and wxPython 2.4.1. Here's hoping someone can point out where I'm going wrong!
Regards,
Robert M. Anderson
Shizuoka, Japan
-----------------LISTING FOLLOWS-----------------
#!/usr/bin/env python
# -------------------------------------------------------------------------------
# Based on the "SmallApp" demonstration code submitted by Greg Brunet.
# See the wxPython Wiki at http://wiki.wxpython.org/index.cgi/SmallApp
# -------------------------------------------------------------------------------
import os, sys
try:
from wxPython.wx import *
except:
print ‘This application requires the wxPython module to run.’
print ‘Download from http://wxpython.org/’
sys.exit()
try:
from mx.DateTime import *
except:
print ‘This application requires the mxDateTime module to run.’
print ‘Download from http://www.egenix.com/files/python/mxDateTime.html’
sys.exit()
# -------------------------------------------------------------------------------
# Version and date info.
APP_VERSION = '0.101'
APP_DATE = 'September 5, 2003'
APP_NAME = "Test Tools"
# Menu and control ID's.
ID_NEW = wxNewId()
ID_OPEN = wxNewId()
ID_SAVE = wxNewId()
ID_SAVEAS = wxNewId()
ID_EXIT = wxNewId()
ID_ABOUT = wxNewId()
ID_IMGSAMP = wxNewId()
ID_LISTBOX1 = wxNewId()
ID_BUTTON1 = wxNewId()
ID_RBOX1 = wxNewId()
SB_INFO = 0
SB_ROWCOL = 1
SB_DATETIME = 2
# Location of converted images.
IMAGEDIRBASE = 'images'
# -------------------------------------------------------------------------------
# Frame class.
class NKAppFrame(wxFrame):
""" Derive a new class of wxFrame. """
def __init__(self, parent, id, title):
wxFrame.__init__(self,
parent = None,
id = -1,
title = APP_NAME,
size = wxSize(600, 400),
name = 'mainwindow',
style = wxDEFAULT_FRAME_STYLE)
# Set the background colour.
self.SetBackgroundColour(wxNamedColour('LIGHT STEEL BLUE'))
# Add a menu, first build the menus (with accelerators).
fileMenu = wxMenu()
fileMenu.Append(ID_NEW, "&New\tCtrl+N", "Creates a new file")
EVT_MENU(self, ID_NEW, self.OnFileNew)
fileMenu.Append(ID_OPEN, "&Open\tCtrl+O", "Open a new db")
EVT_MENU(self, ID_OPEN, self.OnFileOpen)
fileMenu.Append(ID_SAVE, "&Save settings\tCtrl+S", "Save the current settings")
EVT_MENU(self, ID_SAVE, self.OnFileSave)
fileMenu.Append(ID_SAVEAS, "Save &As...\tCtrl+Shift+S", "Save the current settings with a new name")
EVT_MENU(self, ID_SAVEAS, self.OnFileSaveAs)
fileMenu.AppendSeparator()
fileMenu.Append(ID_EXIT, "E&xit\tAlt+Q", "Quit the program")
EVT_MENU(self, ID_EXIT, self.OnFileExit)
helpMenu = wxMenu()
helpMenu.Append(ID_ABOUT, "&About", "Display information about this program")
EVT_MENU(self, ID_ABOUT, self.OnHelpAbout)
# Now add them to a menubar & attach the menubar to the frame.
menuBar = wxMenuBar()
menuBar.Append(fileMenu, "&File")
menuBar.Append(helpMenu, "&Help")
self.SetMenuBar(menuBar)
# Add a statusBar (with date/time panel).
sb = self.CreateStatusBar(3)
sb.SetStatusWidths([-1, 65, 150])
sb.PushStatusText("Ready", SB_INFO)
# Set up a timer to update the date/time (every 5 seconds).
self.timer = wxPyTimer(self.Notify)
self.timer.Start(5000)
# Update the time right away.
self.Notify()
# ----------------- Add controls. -----------------
# ----------------- Image sample.
sampleimgfile = os.path.join(IMAGEDIRBASE, 'no-sample-image.png')
try:
png = self.GetPNGimage(sampleimgfile)
self.imgSample = wxStaticBitmap(self, ID_IMGSAMP, png, wxPoint(5, 5),
wxSize(png.GetWidth(), png.GetHeight()))
except:
pass
# ----------------- List box 1.
sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen']
self.listbox1_txt = wxStaticText(self, -1,
"Select one:",
wxPoint(15, 50),
style = wxALIGN_RIGHT)
self.listbox1 = wxListBox(self, ID_LISTBOX1,
wxPoint(10, 10),
wxSize(80, 120),
sampleList,
wxLB_EXTENDED)
EVT_LISTBOX(self, ID_LISTBOX1, self.EvtMultiListBox)
self.listbox1.SetSelection(0)
# ----------------- Test button.
self.testbutton = wxButton(self, ID_BUTTON1, "Test button", wxPoint(20, 20))
EVT_BUTTON(self, ID_BUTTON1, self.OnClick)
# ----------------- Test radio box.
sampleList2 = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']
self.rb_test1 = wxRadioBox(self, ID_RBOX1, "wxRadioBox Test", wxDefaultPosition, wxDefaultSize, sampleList2, 2, wxRA_SPECIFY_COLS)
EVT_RADIOBOX(self, ID_RBOX1, self.EvtRadioBox)
#self.rb_test1.SetBackgroundColour(wxNamedColour('LIGHT STEEL BLUE'))
# ----------------- Finished controls. -----------------
# ----------------- Sizers for the window.
# Main sizer.
self.mainSizer = wxBoxSizer(wxVERTICAL)
# Set minimum size of the main sizer.
self.mainSizer.SetMinSize(wxSize(600, 400))
# Sizer for 1st row of items.
self.row1sizer = wxBoxSizer(wxHORIZONTAL)
# Add some space on the left.
self.row1sizer.Add(2, 2, 0, wxLEFT, 2)
# Add the controls in this row.
self.row1sizer.Add(self.imgSample, 0)
self.row1sizer.Add(4, 4, 0, wxLEFT, 4)
self.row1sizer.Add(self.listbox1_txt, 0)
self.row1sizer.Add(1, 1, 0, wxLEFT, 1)
self.row1sizer.Add(self.listbox1, 0)
# Sizer for 2nd row of items.
self.row2sizer = wxBoxSizer(wxHORIZONTAL)
# Add some space on the left.
self.row2sizer.Add(2, 2, 0, wxLEFT, 2)
# Add the controls in this row.
self.row2sizer.Add(self.testbutton, 0)
self.row2sizer.Add(5, 5, 0, wxLEFT, 5)
self.row2sizer.Add(self.rb_test1, 0)
# Add some space at the top of the main sizer.
self.mainSizer.Add(2, 2, 0, wxTOP, 2)
# Add the rows to the panel sizer.
self.mainSizer.Add(self.row1sizer)
self.mainSizer.Add(2, 2, 0, wxTOP, 2)
self.mainSizer.Add(self.row2sizer)
# Set up the whole window.
self.SetSizer(self.mainSizer)
self.SetAutoLayout(True)
self.mainSizer.SetSizeHints(self)
self.mainSizer.Fit(self)
# Finally - show it!
self.Show(True)
# -------------------------------------------------------------------------------
def __del__(self):
""" Class delete event: don't leave timer hanging around! """
self.timer.stop()
del self.timer
# -------------------------------------------------------------------------------
def GetPNGimage(self, imgFname):
""" Converts the specified png to wxImage. """
try:
return wxImage(imgFname, wxBITMAP_TYPE_PNG).ConvertToBitmap()
except:
raise
# -------------------------------------------------------------------------------
def Notify(self):
""" Timer event. """
st = now().Format(' %b-%d-%Y %I:%M %p')
self.SetStatusText(st, SB_DATETIME)
# -------------------------------------------------------------------------------
def OnFileExit(self, e):
""" File|Exit event """
self.Close(True)
# -------------------------------------------------------------------------------
def OnFileNew(self, e):
""" File|New event. """
self.PushStatusText('File-new selected.', SB_INFO)
# -------------------------------------------------------------------------------
def OnFileOpen(self, e):
""" File|Open event. """
dlg = wxFileDialog(self, 'Open', str(os.getcwd()), '', '*.*', wxOPEN | wxCHANGE_DIR )
# User selected "OK".
if (dlg.ShowModal() == wxID_OK):
self.fileName = dlg.GetFilename()
self.dirName = dlg.GetDirectory()
# Open the requested file
try:
self.SetStatusText('Opened file: ' + self.fileName, SB_INFO)
except:
self.PushStatusText('Error opening file.', SB_INFO)
# User cancelled dialog.
else:
self.PushStatusText('Open operation cancelled.', SB_INFO)
dlg.Destroy()
# -------------------------------------------------------------------------------
def OnFileSave(self, e):
self.PushStatusText('File-save selected.', SB_INFO)
# -------------------------------------------------------------------------------
def OnFileSaveAs(self, e):
self.PushStatusText('File-save as selected.', SB_INFO)
# -------------------------------------------------------------------------------
def OnHelpAbout(self, e):
""" Help|About event. """
title = self.GetTitle()
d = wxMessageDialog(self, 'About ' + title, title, wxICON_INFORMATION | wxOK)
d.ShowModal()
d.Destroy()
# -------------------------------------------------------------------------------
def EvtListBox(self, event):
""" Single item listbox event. """
self.PushStatusText(str(event.GetSelection()), SB_INFO)
print
print 'Listbox ID:', event.GetId()
print 'String:', event.GetString()
print 'Selection', event.GetSelection()
# -------------------------------------------------------------------------------
def EvtRadioBox(self, event):
""" Single item listbox event. """
print event.GetInt()
print event
self.PushStatusText('Radio box selected: %d' % event.GetInt(), SB_INFO)
# -------------------------------------------------------------------------------
def OnClick(self, event):
if event.GetId() != ID_BUTTON1:
event.Skip()
self.PushStatusText('Button clicked: ' + str(event.GetId()), SB_INFO)
# -------------------------------------------------------------------------------
def EvtMultiListBox(self, event):
""" Multiple item listbox event. """
# Get the ID of the listbox passing this event.
listbox_id = event.GetId()
# Get the selected items from the listbox.
listbox_selected = event.GetEventObject().GetSelections()
print listbox_selected
# Output to status bar.
self.PushStatusText('Multiple listbox #' + str(listbox_id), SB_INFO)
# ========== end [NKAppFrame] class
if __name__ == '__main__':
app = wxPySimpleApp()
frame = NKAppFrame(NULL, -1, 'Small wxPython Application')
app.MainLoop()