Hello. I'm an academic physician and an amateur programmer
new to Python. From what I have seen so far, Python appears
to be IDEAL for evolving many of the tools sorely needed in
the clinical setting. At the moment, however, I am
struggling with how best to create a user interface. (Not
that Swing was any easier, but at least with Swing there
are a plethora of tutorials and examples available.) This
mailing list looks like a good place to ask a few questions
to help get me started.
I have lots of questions, but I'll just start out with a
simple widget. Clinical calculations frequently require the
user to enter a small decimal number into a text field.
Below is my first attempt to create a modified wxTextCtrl
that allows only numeric input, called wxDecimalField. The
following questions refer to the code below.
1. Is there a wxDecimalField equivalent already available
somewhere?
2. When subclassing a wxControl, is the convention to
prefix the class name with 'wx' (wxDecimalField) or not
(DecimalField)?
3. It appears that my OnChar() method overrides the OnChar
method of the parent wxTextCtrl, but this cannot be the
case because:
a. it does not work without the EVT_CHAR macro in
__init__
b. EVT_CHAR(self, self.Spam) and def Spam(self, event)
works just as well
c. wxTextCtrl.OnChar(self, self, event) is not found
Am I right about my OnChar method not REALLY overriding the
method in the wxTextCtrl? If I am wrong, how does this
work?
4. What is event.Skip() actually doing in this code? It
seems to work fine, but I am not sure I understand why. For
example, in my OnChar() method, I first tried to call
wxText.Ctrl.OnChar(self, self, event) to process the
accepted keystroke (which did not work).
5. Instead of capturing keystrokes, should I be using
wxValidator, wxTextValidator or wxPyValidator? If so, how
would that work?
6. I have no C++ experience. Will I be able to use wxPython
effectively without learning C++ to understanding
wxWindows?
···
=======
from wxPython.wx import *
class TestApp(wxApp):
def OnInit(self):
frame = MyFrame(NULL, -1, "Decimal Field Test")
frame.Show(true)
self.SetTopWindow(frame)
return true
class MyFrame(wxFrame):
def __init__(self, parent, ID, title):
wxFrame.__init__(self, parent, ID, title,
wxDefaultPosition,
wxSize(300, 150))
panel = wxPanel(self, -1)
df1 = DecimalField(panel, 10, "",
wxPoint(20, 10),
wxSize(50, 20))
def OnCloseMe(self, event):
self.Close(true)
def OnCloseWindow(self, event):
self.Destroy()
class wxDecimalField(wxTextCtrl):
def __init__(self, parent, ID, title,
position, size):
wxTextCtrl.__init__(self, parent, ID, title,
position, size)
EVT_CHAR(self, self.OnChar)
EVT_KILL_FOCUS(self, self.Finish)
def OnChar(self, event):
key = event.GetKeyCode()
if key > 47 and key < 58:
event.Skip()
elif key == 46:
if '.' not in self.GetValue():
if self.GetInsertionPoint() == 0:
self.WriteText("0")
event.Skip()
elif key == 8 or key == 127:
event.Skip()
elif key == 316 or key == 318:
event.Skip()
def Finish(self, event):
# replace with code to handle/use this value
print self.GetValue()
event.Skip()
app = TestApp(0)
app.MainLoop()
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/