Dear wizards, here are two easy wxpython questions. Thanks in advance for your help!
1. How do I get wxpython to activate a button when user presses "Enter"? In the baby application below, pressing "Tab" switches the focus from one button to another, but I don't know how to make the program react to "Enter".
2. At ###, DecrEvent includes a copy of the code for RestartEvent. How do I get it to simply call RestartEvent directly? It looks like I need DecrEvent to generate an event that RestartEvent will act on, but I don't know how.
''' CountDown.py
Learning about buttons and dialog boxes in wxpython.
-- On pressing "Next" button: decrement a counter.
-- On counter value 0 or on pressing "Restart" button: input a new counter value or quit.
'''
import wx
#-------------------
class TextFrame(wx.Frame):
global InFile
def __init__(self, parent, mytitle, mysize):
wx.Frame.__init__(self, parent, -1, pos=(0,0), title=mytitle, size=mysize)
self.background = wx.Panel(self)
self.background.SetBackgroundColour("white")
self.butDecr = wx.Button(self.background,wx.ID_ANY,label = 'Next')
self.butDecr.Bind(wx.EVT_LEFT_DOWN,self.DecrEvent)
self.butRestart = wx.Button(self.background,wx.ID_ANY,label = 'Restart')
self.butRestart.Bind(wx.EVT_LEFT_DOWN,self.RestartEvent)
self.butQuit = wx.Button(self.background,wx.ID_ANY,label='Quit')
self.butQuit.Bind(wx.EVT_LEFT_DOWN,self.QuitEvent)
self.TextLabel = wx.StaticText(self.background,wx.ID_ANY,"Value:")
self.ValueDisp = wx.TextCtrl(self.background,style = wx.TE_READONLY)
self.hbox1 = wx.BoxSizer()
self.hbox1.Add(self.butDecr, proportion=0, flag=wx.EXPAND)
self.hbox1.Add(self.butRestart, proportion=0, flag=wx.EXPAND)
self.hbox1.Add(self.butQuit, proportion=0, flag=wx.EXPAND)
self.hbox1.Add((0, 0), proportion=1, flag=wx.EXPAND) # First item is spacer (width, height)
self.hbox2 = wx.BoxSizer()
self.hbox2.Add(self.TextLabel, proportion=0, flag=wx.EXPAND)
self.hbox2.Add(self.ValueDisp, proportion=0, flag=wx.EXPAND)
self.hbox1.Add((0, 0), proportion=1, flag=wx.EXPAND)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.hbox1, proportion=0, flag=wx.EXPAND)
self.vbox.Add(self.hbox2, proportion=0, flag=wx.EXPAND)
self.vbox.Add((0, 0), proportion=1, flag=wx.EXPAND)
self.background.SetSizer(self.vbox)
self.DefaultStartValue = 5
self.InitValue = self.DefaultStartValue
self.CountValue = self.InitValue
self.ValueDisp.SetValue(repr(self.CountValue))
self.Show()
def DecrEvent(self,event):
if self.CountValue > 0:
self.CountValue -= 1
self.ValueDisp.SetValue(repr(self.CountValue))
else:
QuitDlg = wx.MessageDialog(None, "Countdown complete. Restart?",
"Again?", wx.YES_NO | wx.NO_DEFAULT | wx.ICON_EXCLAMATION)
QuitDlgAns = QuitDlg.ShowModal()
QuitDlg.Destroy()
if QuitDlgAns == wx.ID_NO:
self.Close(True)
else:
### Want to call RestartEvent directly here. How?
RestartDlg = wx.TextEntryDialog(None, "Starting value:",
"Restart", repr(self.InitValue), style=wx.OK | wx.CANCEL)
RestartDlgAns = RestartDlg.ShowModal()
RestartDlg.Destroy()
if RestartDlgAns == wx.CANCEL:
self.close(True)
else:
RestartTxt = RestartDlg.GetValue()
self.InitValue = int(RestartTxt)
self.CountValue = self.InitValue
self.ValueDisp.SetValue(repr(self.CountValue))
def RestartEvent(self,event):
RestartDlg = wx.TextEntryDialog(None, "Starting value:",
"Restart", repr(self.InitValue), style=wx.OK | wx.CANCEL)
RestartDlgAns = RestartDlg.ShowModal()
RestartDlg.Destroy()
if RestartDlgAns == wx.CANCEL:
self.close(True)
else:
RestartTxt = RestartDlg.GetValue()
self.InitValue = int(RestartTxt)
self.CountValue = self.InitValue
self.ValueDisp.SetValue(repr(self.CountValue))
def QuitEvent(self,event):
self.Close(True)
#-------------------
app = wx.App(redirect = False)
mywidth = 300
myheight = 200
mytitle = "Countdown"
frame = TextFrame(None, mytitle, (mywidth,myheight))
app.MainLoop()