I just started with wxPython and have problems with the binding.
Usually the examples I find to bind a button event are done withself.Bind(wx.EVT_BUTTON, self.OnWhatEverFunction, button)
. I have a frame with a panel inside and a button and what ever I tried, the outcome is always a flashing frame that appears in a split second and that's it. Since the code isn't that much I attach it here and hope one of you can show me the way out of my little problem.
Thanks in advance, Thomas
#!/usr/bin/python
import wx
class CPFSFrame(wx.Frame):
def __init__(self, parent, title):
super(CPFSFrame, self).__init__(parent, title=title,
style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
size=(350, 200))
panel = wx.Panel(self, -1)
pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNumberText.SetInsertionPoint(0)
pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNameText.SetInsertionPoint(0)
pButton = wx.Button(panel, label='Create')
pButton.Bind(wx.EVT_BUTTON, self.OnCreate)
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
panel.SetSizer(sizer)
statusBar = self.CreateStatusBar()
def OnCreate(self, evt):
self.Close(True)
self.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
CPFSFrame(None, title='Create New Project Folder Structure')
app.MainLoop()
You need to put the "self.Show()" method in the init section of the code. At first I thought OnCreate() was running and if it was, then it would just close the frame immediately so you wouldn't see anything. I would call it OnClose instead. Here's a working example:
import wx
class CPFSFrame(wx.Frame):
def __init__(self, parent, title):
super(CPFSFrame, self).__init__(parent, title=title,
style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
size=(350, 200))
panel = wx.Panel(self, -1)
pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNumberText.SetInsertionPoint(0)
pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNameText.SetInsertionPoint(0)
pButton = wx.Button(panel, label='Create')
pButton.Bind(wx.EVT_BUTTON, self.OnClose)
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
panel.SetSizer(sizer)
statusBar = self.CreateStatusBar()
self.Show()
def OnClose(self, evt):
self.Close(True)
if __name__ == '__main__':
app = wx.App()
CPFSFrame(None, title='Create New Project Folder Structure')
app.MainLoop()