I'm a newbie in wxpython and have been trying to put some widgets within books(treebook, notebook, choicebook) I have often ended up with some widgets placed inside containers not responding to events. I'm not sure what I'm doing wrong. Below is one of my code
import wx
class ChoicePanelTwo(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('blue')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.List = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
for i in range(seed):
self.List.InsertStringItem(i, str(i))
sizer.Add(self.List, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class ChoicePanelOne(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('green')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.RegisterList = wx.Choicebook(self, wx.ID_ANY)
sizer.Add(self.RegisterList, 1, wx.ALL|wx.EXPAND, 5)
for i in range(seed):
self.RegisterList.AddPage(ChoicePanelTwo(self, seed*50), str(i))
self.SetSizer(sizer)
class TreePanel(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
self.SetBackgroundColour('cyan')
self.Choicbook = wx.Choicebook(self, wx.ID_ANY)
for i in range(seed):
self.Choicbook.AddPage(ChoicePanelOne(self, seed*2), str(i))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.Choicbook, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class AppFrame(wx.Frame):
""" The main frame of the application
"""
title = 'Application'
WindowSize = (1024, 768)
seed = 2
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title, size=self.WindowSize, style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
self.create_main_panel()
def create_main_panel(self):
self.panel = TreePanel(self, self.seed)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = AppFrame()
app.frame.Show()
app.MainLoop()
In this example. The choice book and list does seem to be working. What is that I'm doing wrong ?
It's a parenting issue. You are setting all the ChoicePanelOne instance's parents as the TreePanel when it should be the ChoicBook. And you're doing the same thing in ChoicePanelOne where you create a whole bunch of ChoicePanelTwo's whose parents are ChoicePanelOne when they should be RegisterList. Check out the slightly changed code below:
import wx
class ChoicePanelTwo(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('blue')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.List = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
for i in range(seed):
self.List.InsertStringItem(i, str(i))
sizer.Add(self.List, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class ChoicePanelOne(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('green')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.RegisterList = wx.Choicebook(self, wx.ID_ANY)
sizer.Add(self.RegisterList, 1, wx.ALL|wx.EXPAND, 5)
for i in range(seed):
self.RegisterList.AddPage(ChoicePanelTwo(self.RegisterList, seed*50), str(i))
self.SetSizer(sizer)
class TreePanel(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
self.SetBackgroundColour('cyan')
self.Choicbook = wx.Choicebook(self, wx.ID_ANY)
for i in range(seed):
self.Choicbook.AddPage(ChoicePanelOne(self.Choicbook, seed*2), str(i))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.Choicbook, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class AppFrame(wx.Frame):
""" The main frame of the application
"""
title = 'Application'
WindowSize = (1024, 768)
seed = 2
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title, size=self.WindowSize,
style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
self.create_main_panel()
def create_main_panel(self):
self.panel = TreePanel(self, self.seed)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = AppFrame()
app.frame.Show()
app.MainLoop()
You should download the wxPython demo as it has lots of good examples in it. Also see the wiki or my blog article.
You could use the Widget Inspection Tool to help you diagnose the problem as well as it will show you what parent is where and how your sizers are laid out, among other things.
Related
I want to recreate a grid. For example: old grid is 4x4, I want change it to 5x5.
Here is my code:
import wx
import wx.xrc
import wx.grid
class MyFrame2(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
bSizer4 = wx.BoxSizer(wx.VERTICAL)
self.m_grid2 = wx.grid.Grid(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0)
# Grid
self.m_grid2.CreateGrid(4, 4)
bSizer4.Add(self.m_grid2, 0, wx.ALL, 5)
self.m_button3 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
bSizer4.Add(self.m_button3, 0, wx.ALL, 5)
self.m_button3.Bind(wx.EVT_BUTTON, self.OnClick)
self.SetSizer(bSizer4)
self.Layout()
def OnClick(self, event):
self.m_grid2.CreateGrid(5, 5)
self.Layout()
app = wx.App()
frame = MyFrame2(None)
frame.Show(True)
app.MainLoop()
It raise an error when I run that:
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\grid.py", line 1221, in CreateGrid
return _grid.Grid_CreateGrid(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "!m_created" failed at ..\..\src\generic\grid.cpp(2325) in wxGrid::CreateGrid(): wxGrid::CreateGrid or wxGrid::SetTable called more than once
It seems I can't recreate this grid again. How can do this job?
If you don't want to Append the rows and columns, then you'll just have to recreate the grid itself. Here's a fairly simple demo that demonstrates how to do that:
import wx
import wx.grid as gridlib
########################################################################
class MyGrid(gridlib.Grid):
#----------------------------------------------------------------------
def __init__(self, parent, rows, cols):
gridlib.Grid.__init__(self, parent)
self.CreateGrid(rows, cols)
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.grid_created = False
row_sizer = wx.BoxSizer(wx.HORIZONTAL)
col_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
rows_lbl = wx.StaticText(self, label="Rows", size=(30, -1))
row_sizer.Add(rows_lbl, 0, wx.ALL|wx.CENTER, 5)
self.rows = wx.TextCtrl(self)
row_sizer.Add(self.rows, 0, wx.ALL|wx.EXPAND, 5)
cols_lbl = wx.StaticText(self, label="Cols", size=(30, -1))
col_sizer.Add(cols_lbl, 0, wx.ALL|wx.CENTER, 5)
self.cols = wx.TextCtrl(self)
col_sizer.Add(self.cols, 0, wx.ALL|wx.EXPAND, 5)
grid_btn = wx.Button(self, label="Create Grid")
grid_btn.Bind(wx.EVT_BUTTON, self.create_grid)
self.main_sizer.Add(row_sizer, 0, wx.EXPAND)
self.main_sizer.Add(col_sizer, 0, wx.EXPAND)
self.main_sizer.Add(grid_btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(self.main_sizer)
#----------------------------------------------------------------------
def create_grid(self, event):
""""""
rows = int( self.rows.GetValue() )
cols = int( self.cols.GetValue() )
if self.grid_created:
for child in self.main_sizer.GetChildren():
widget = child.GetWindow()
if isinstance(widget, gridlib.Grid):
self.main_sizer.Remove(widget)
grid = MyGrid(self, rows, cols)
self.main_sizer.Add(grid, 0, wx.ALL, 5)
self.grid_created = True
self.main_sizer.Layout()
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="grids", size=(800, 600))
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
You may only call the CreateGrid function once. if you want to change the size, you need to use the functions AppendCols, AppendRows, DeleteCols or DeleteRows.
def OnClick(self, event):
self.m_grid2.AppendCols(1)
self.m_grid2.AppendRows(1)
self.Layout()
Lokla
I want to ask is it possible to add wx.Panel with event button in wxpython? There are plenty examples how to switch panels Hide first one and show second, but they are useless for me. I want to create panel with add button. For example I have panel something like this
import wx
import wx.grid as grid
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent = parent)
class SecondPanel(wx.Panel):
def __init__(self, parent,a,b):
wx.Panel.__init__(self, parent=parent)
MyGrid=grid.Grid(self)
MyGrid.CreateGrid(a, b)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(MyGrid, 0, wx.EXPAND)
self.SetSizer(sizer)
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="test",
size=(800,600))
self.splitter = wx.SplitterWindow(self)
self.panelOne = MainPanel(self.splitter)
self.panelTwo = SecondPanel(self.splitter, 1, 1)
txtOne = wx.StaticText(self.panelOne, -1, label = "piradoba", pos = (20,10))
self.txtTwo = wx.StaticText(self.panelOne, -1, label = "", pos = (40,80))
self.txtPlace = wx.TextCtrl(self.panelOne, pos = (20,30))
button = wx.Button(self.panelOne, label = "search", pos = (40,100))
button.Bind(wx.EVT_BUTTON, self.Onbutton)
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.splitter.SetMinimumPaneSize(20)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.splitter, 1, wx.EXPAND)
self.SetSizer(sizer)
def Onbutton(self, event):
var=self.txtPlace.GetValue()
if len(var) == 9 or len(var) == 11:
???????????????????????????????????????????????
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
for example now I want to add new panel with this event what can I do? and I want to create this panel with event.
I don't know if it is what you need but in this example you have:
panel with button and event
button call function in mainframe
mainframe add next panel (with grid) to boxsizer
Tested on Linux Mint + Python 2.7.4
import wx
import wx.grid as grid
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent = parent)
self.txtOne = wx.StaticText(self, -1, label = "piradoba", pos = (20,10))
self.txtPlace = wx.TextCtrl(self, pos = (20,30))
self.txtTwo = wx.StaticText(self, -1, label = "", pos = (20,40))
button = wx.Button(self, label = "search", pos = (20,70))
button.Bind(wx.EVT_BUTTON, self.onButton)
def onButton(self, event):
var=self.txtPlace.GetValue()
if len(var) == 9 or len(var) == 11:
print "???"
# MainPanel->SplitterWindow->MainFrame ( 2x GetParent() )
self.GetParent().GetParent().AddPanel()
class SecondPanel(wx.Panel):
def __init__(self, parent,a,b):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
MyGrid=grid.Grid(self)
MyGrid.CreateGrid(a, b)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(MyGrid, 0, wx.EXPAND)
self.SetSizer(sizer)
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="test", size=(800,600))
self.splitter = wx.SplitterWindow(self)
self.panelOne = MainPanel(self.splitter)
self.panelTwo = SecondPanel(self.splitter, 1, 1)
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.splitter.SetMinimumPaneSize(20)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.splitter, 2, wx.EXPAND)
self.SetSizer(self.sizer)
def AddPanel(self):
self.newPanel = SecondPanel(self, 1, 1)
self.sizer.Add(self.newPanel, 1, wx.EXPAND)
self.sizer.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
I want to limit the size of a list control box. Let us take the following code:
import wx
class Students(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(380, 230))
hbox = wx.BoxSizer(wx.HORIZONTAL)
panel = wx.Panel(self, -1)
self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT)
self.list.InsertColumn(0, 'name')
self.list.InsertColumn(1, 'age')
hbox.Add(self.list, 1,wx.EXPAND)
panel.SetSizer(hbox)
self.Centre()
self.Show(True)
app = wx.App()
Students(None, -1, 'studs')
app.MainLoop()
If I make the horizontal box sizer's proportion=0, like this:
hbox.Add(self.list, 0,wx.EXPAND)
then there is a different problem. The problem with proportion=1 is that after 'Age' column, there is a lot of empty space the list control box is eating up for the third column which doesn't exist.
The problem with proportion=0 is that it is too short in width.
I want to display the list control box with 'Name' and 'Age' columns only and save the rest of the space. How do I do that?
You can set the width of your list control manually and then set the proportion to 0.
The width of columns can be set manually:
import wx
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour(wx.GREEN)
self.list = wx.ListCtrl(self, style=wx.LC_REPORT, size=(200, -1))
column_size = self.list.GetSize()[0] / 2 - 2
self.list.InsertColumn(0, 'Name')
self.list.InsertColumn(1, 'Age')
self.list.SetColumnWidth(0, column_size)
self.list.SetColumnWidth(1, column_size)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.list, proportion=0, flag=wx.EXPAND)
self.sizer.Add(self.panel, proportion=1, flag=wx.EXPAND)
self.SetSizerAndFit(self.sizer)
self.SetSize((600, 400))
self.Show()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Or the ListCtrlAutoWidthMixin may be used:
import wx
import wx.lib.mixins.listctrl as listmix
class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
def __init__(self, *args, **kwargs):
wx.ListCtrl.__init__(self, *args, **kwargs)
listmix.ListCtrlAutoWidthMixin.__init__(self)
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour(wx.GREEN)
self.list = TestListCtrl(self, style=wx.LC_REPORT, size=(200, -1))
self.list.InsertColumn(0, 'Name')
self.list.InsertColumn(1, 'Age')
self.sizer = wx.BoxSizer()
self.sizer.Add(self.list, proportion=0, flag=wx.EXPAND)
self.sizer.Add(self.panel, proportion=1, flag=wx.EXPAND)
self.SetSizerAndFit(self.sizer)
self.SetSize((600, 400))
self.Show()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Don't use wx.EXPAND if you don't want the control to resize. You could also put a spacer into the horizontal sizer right after the list control and make it expand instead. Then the spacer will take up all the available space.
with this code:
import wx
import wx.aui
class MyFrame(wx.Frame):
def __init__(self, parent, id=-1, title='wx.aui Test',
pos=wx.DefaultPosition, size=(800, 600),
style=wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self._mgr = wx.aui.AuiManager(self)
# create several text controls
text1 = wx.TextCtrl(self, -1, 'Pane 1 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
text2 = wx.TextCtrl(self, -1, 'Pane 2 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
info = wx.aui.AuiPaneInfo()
info.CaptionVisible(True)
info.BottomDockable(False)
info.LeftDockable(False)
info.RightDockable(False)
info.PaneBorder(False)
info.Top()
info.Row(1)
info2 = wx.aui.AuiPaneInfo()
info2.CaptionVisible(True)
info2.BottomDockable(False)
info2.LeftDockable(False)
info2.RightDockable(False)
info2.Top()
info2.Row(2)
self._mgr.AddPane(text1, info, 'Pane Number One')
self._mgr.AddPane(text2, info2, 'Pane Number Two')
self._mgr.Update()
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
self._mgr.UnInit()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
the two panes that I create are docked in the Top.
The info.Row(1) and info2.Row(2) put the two panes one after another:
_TOP_
Pane1
Pane2
Now, if I move on Pane2, this docks in the Top and this situation occurs:
_TOP_
Pane1|Pane2
I want:
1. to avoid this situation (only one pane per row!)
2. if I move, dock the pane in the bottom/top of another pane
Is this possible?
Maybe the AuiNotebook wxPython sample works for you?
import wx
import wx.aui
########################################################################
class TabPanel(wx.Panel):
"""
This will be the first notebook tab
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
class DemoPanel(wx.Panel):
"""
This will be the first notebook tab
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
# create the AuiNotebook instance
nb = wx.aui.AuiNotebook(self)
# add some pages to the notebook
pages = [(TabPanel(nb), "Tab 1"),
(TabPanel(nb), "Tab 2"),
(TabPanel(nb), "Tab 3")]
for page, label in pages:
nb.AddPage(page, label)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(nb, 1, wx.EXPAND)
self.SetSizer(sizer)
########################################################################
class DemoFrame(wx.Frame):
"""
Frame that holds all other widgets
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"AUI-Notebook Tutorial",
size=(600,400))
panel = DemoPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = DemoFrame()
app.MainLoop()
I was trying to get my first wxWindow application to work and I ran into following difficulty:
I create wxPanel and add a wxNotebook object to it. Then I add a page to notebook created from another wxPanel object. How do I access a value of TextCtrl from first wxPanel in the second one?
import wx
class BasicApp(wx.App):
def OnInit(self):
frame = BasicFrame(None, -1, "Test App")
panel = BasicPanel(frame, -1);
frame.Show(True)
self.SetTopWindow(frame)
return True;
class BasicFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title,
wx.DefaultPosition, wx.Size(400, 300))
self.CreateStatusBar()
class BasicPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.lblText1 = wx.StaticText(self, -1, "Text1:");
self.txtText1 = wx.TextCtrl(self, 1001, "Text1", size = wx.Size(140, -1));
self.line1 = wx.BoxSizer(wx.HORIZONTAL);
self.line1.Add(self.lblText1, 0, wx.EXPAND);
self.line1.Add(self.txtText1, proportion=1, flag=wx.LEFT, border=5);
self.nb = wx.Notebook(self, -1);
tab1 = Tab1(self.nb, -1);
self.nb.AddPage(tab1, "Tab1");
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.line1, 0, wx.EXPAND)
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show(1)
class Tab1(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id);
self.lblText2 = wx.StaticText(self, -1, "Text2:");
self.txtText2 = wx.TextCtrl(self, 1101, "Text2", size = wx.Size(140, -1));
self.line1 = wx.BoxSizer(wx.HORIZONTAL);
self.line1.Add(self.lblText2, 0, wx.EXPAND);
self.line1.Add(self.txtText2, 0, wx.EXPAND);
self.lblMessage = wx.StaticText(self, -1, "Message:");
self.txtMessage = wx.TextCtrl(self, 1102, "", style = wx.TE_MULTILINE);
self.cmdCreate = wx.Button(self, 1103, "Create");
self.cmdCreate.Bind(wx.EVT_BUTTON, self.Create_OnClick)
self.line3 = wx.BoxSizer(wx.HORIZONTAL);
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.line1, 0, wx.EXPAND)
self.sizer.Add(self.lblMessage, 0, wx.EXPAND)
self.sizer.Add(self.txtMessage, 1, wx.EXPAND)
self.sizer.Add(self.cmdCreate, 0, wx.LEFT)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show(1)
def Create_OnClick(self, event):
text1 = "";
text2 = self.txtText2.GetValue();
self.txtMessage.SetValue(text1 + " " + text2);
app = BasicApp(0)
app.MainLoop()
To be more specific I want to be able to access value of txtText1 in Create_OnClick() method. How could this be achieved?
One solution is to pass the control to the constructor of the tab, then you can directly reference it. For example:
class Tab1(wx.Panel):
def __init__(self, parent, id, textCtrl1):
wx.Panel.__init__(self, parent, id);
self.textCtrl1 = textCtrl1
...
def Create_OnClick(self, event):
text1 = self.textCtrl1
Another solution is to move the Create_OnClick handler to the base panel since it knows about all of the other panels (and thus, their children). Or, create a separate controller class that knows about the various widgets and can have handlers that act on behalf of them all.