Reputation: 452
I am new to wxPython and still trying to understand a basic workflow, I have managed to create this window that has a Menu-bar, and tabs below it, however the buttons are all not clickable, tabs not selectable and the event for button in the first frame is called without being clicked at all by the user upon first run
Gallery
Code
############################################################################################################################################
#BEGINNING OF TABS
############################################################################################################################################
# Define the tab content as classes:
class TabOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
button = wx.Button(self, label = "Hello World")
button.Bind(wx.EVT_BUTTON, self.showDialog())
def showDialog(self):
wx.MessageBox("A Dialog Example", "Information", wx.OK | wx.ICON_INFORMATION)
class TabTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the second tab", (20,20))
class TabThree(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the third tab", (20,20))
class TabFour(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the last tab", (20,20))
############################################################################################################################################
#END OF TABS
############################################################################################################################################
############################################################################################################################################
## Class MAIN WINDOW
############################################################################################################################################
class mainWindow(wx.MDIParentFrame):
def __init__(self):
wx.MDIParentFrame.__init__(self, None, -1, "Cocab Tech Solutions Main Window", size=(600, 400))
#BEGIN MENU
menu = wx.Menu()
menu.Append(5000, "New Window")
menu.Append(5001, "Exit")
menubar = wx.MenuBar()
menubar.Append(menu, "File")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.OnNewWindow, id=5000)
self.Bind(wx.EVT_MENU, self.OnExit, id=5001)
#Define Sizers
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
bSizer_topFrame = wx.BoxSizer(wx.VERTICAL)
bsecSizer_topFrame = wx.BoxSizer(wx.VERTICAL)
self.main_Panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
main_Panel_box_Sizer = wx.BoxSizer(wx.VERTICAL)
mainPanel_secondSizer = wx.BoxSizer(wx.VERTICAL)
#BEGIN TABS
# Create a panel and notebook (tabs holder)
nb = wx.Notebook(self.main_Panel, id=4, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.NB_DEFAULT, name=wx.NotebookNameStr)
# Create the tab windows
tab1 = TabOne(nb)
tab2 = TabTwo(nb)
tab3 = TabThree(nb)
tab4 = TabFour(nb)
# Add the windows to tabs and name them.
nb.AddPage(tab1, "Tab 1")
nb.AddPage(tab2, "Tab 2")
nb.AddPage(tab3, "Tab 3")
nb.AddPage(tab4, "Tab 4")
mainPanel_secondSizer.Add(nb, 1, wx.EXPAND)
# self.m_staticText1 = wx.StaticText(self.main_Panel, wx.ID_ANY, u"Frame2", wx.DefaultPosition, wx.DefaultSize, 0)
# self.m_staticText1.Wrap(-1)
# mainPanel_secondSizer.Add(self.m_staticText1, 0, wx.ALL, 5)
self.testButton = wx.Button(self.main_Panel, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
mainPanel_secondSizer.Add(self.testButton, 0, wx.ALL, 5)
self.testButton.Bind(wx.EVT_BUTTON, self.on_open_frame)
main_Panel_box_Sizer.Add(mainPanel_secondSizer, 1, wx.ALL | wx.EXPAND, 0)
self.main_Panel.SetSizer(main_Panel_box_Sizer)
self.main_Panel.Layout()
main_Panel_box_Sizer.Fit(self.main_Panel)
bsecSizer_topFrame.Add(self.main_Panel, 1, wx.EXPAND | wx.ALL, 0)
bSizer_topFrame.Add(bsecSizer_topFrame, 1, wx.ALL | wx.EXPAND, 0)
self.SetSizer(bSizer_topFrame)
self.Layout()
self.Centre(wx.BOTH)
def OnNewWindow(self, evt):
win = wx.MDIChildFrame(self, -1, "Child Window")
win.Show(True)
def OnExit(self, evt):
self.Close(True)
def on_open_frame(self, event):
print("Clicked")
Upvotes: 0
Views: 106
Reputation: 1080
For the event for the button emitted without a click:
when you bind it, instead of this:
button.Bind(wx.EVT_BUTTON, self.showDialog())
you have to do this:
button.Bind(wx.EVT_BUTTON, self.showDialog)
for the other questions, can you be more specific and clear?
Upvotes: 2