I want to hide a widget in a sizer and not leave an empty space.
I do not want to remove the widget, because I want to show it again later.
Is there a way to hide a widget and have the shown widgets after it move over one space to fill the empty spot?
Here is a simple example of a space being left:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
sizer = wx.GridSizer(1, 3, 0, 0)
a = wx.Button(self, -1, 'a')
b = wx.Button(self, -1, 'b')
c = wx.Button(self, -1, 'c')
sizer.Add(a, 0, 0, 0)
sizer.Add(b, 0, 0, 0)
sizer.Add(c, 0, 0, 0)
b.Hide()
self.SetSizer(sizer)
class MyFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="Remove Spaces")
panel = MyPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Fit()
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Do you have to use wx.GridSizer? Because it will give you a fixed layout, so even you hide a control the grid will still have that space displayed.
Have you considered using wx.GridBagSizer? Try this one:
class MyPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.parent = parent
self.sizer = wx.GridBagSizer()
self.a = wx.Button(self, -1, 'a')
self.b = wx.Button(self, -1, 'b')
self.c = wx.Button(self, -1, 'c')
self.a.Bind(wx.EVT_BUTTON, self.button_clicked)
self.sizer.Add(self.a, pos=(0, 0))
self.sizer.Add(self.b, pos=(0, 1))
self.sizer.Add(self.c, pos=(0, 2))
self.SetSizer(self.sizer)
def button_clicked(self, event):
if self.b.IsShown():
self.b.Hide()
else:
self.b.Show()
self.parent.Fit()
Using FlexGridSizer:
class MyPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.parent = parent
self.sizer = wx.FlexGridSizer(1, 3, 0, 0)
self.a = wx.Button(self, -1, 'a')
self.b = wx.Button(self, -1, 'b')
self.c = wx.Button(self, -1, 'c')
self.a.Bind(wx.EVT_BUTTON, self.button_clicked)
self.sizer.Add(self.a)
self.sizer.Add(self.b)
self.sizer.Add(self.c)
self.SetSizer(self.sizer)
def button_clicked(self, event):
if self.b.IsShown():
self.b.Hide()
else:
self.b.Show()
self.parent.Fit()
Related
I am dynamically changing the number of wx objects in my GUI. The code is working but the alignment of the objects is not perfect. The static text objects are not middle aligned with the text objects.
Notice that the word 'if' is not perfectly aligned with 'field name'.
Code:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.numRows = 0
self.frame = parent
#create sizers
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
controlSizer = wx.BoxSizer(wx.HORIZONTAL)
self.colSizer = wx.BoxSizer(wx.VERTICAL)
self.addButton = wx.Button(self, label="Add")
self.addButton.Bind(wx.EVT_BUTTON, self.onAddWidget)
controlSizer.Add(self.addButton, 0, wx.CENTER|wx.ALL, 5)
self.removeButton = wx.Button(self, label="Remove")
self.removeButton.Bind(wx.EVT_BUTTON, self.onRemoveWidget)
controlSizer.Add(self.removeButton, 0, wx.CENTER|wx.ALL, 5)
self.mainSizer.Add(controlSizer, 0, wx.CENTER)
self.mainSizer.Add(self.colSizer, 0, wx.CENTER|wx.ALL, 10)
self.SetSizer(self.mainSizer)
#----------------------------------------------------------------------
def onAddWidget(self, event):
""""""
self.numRows += 1
#create the objects
rowSizer =wx.BoxSizer(wx.HORIZONTAL)
ifText = wx.StaticText(self, -1, 'If')
fieldBox1 = wx.TextCtrl(self, -1, 'Field Name')
dropDown1 = wx.Choice(self, -1, choices=['<', '<=', '=', '>=', '>'])
fieldBox2 = wx.TextCtrl(self, -1, 'Field Name')
thenText = wx.StaticText(self, -1, 'Then')
fieldBox3 = wx.TextCtrl(self, -1, 'Field Name')
#create a list of all the objects
objects = [ifText, fieldBox1, dropDown1, fieldBox2, thenText, fieldBox3]
#add object to row sizer
for myObject in objects:
rowSizer.Add(myObject, 0, wx.ALL, 5)
#add the objects to the sizer
self.colSizer.Add(rowSizer, 0, wx.ALL, 5)
self.frame.fSizer.Layout()
self.frame.Fit()
#----------------------------------------------------------------------
def onRemoveWidget(self, event):
""""""
if self.colSizer.GetChildren():
self.colSizer.Hide(self.numRows-1)
self.colSizer.Remove(self.numRows-1)
self.numRows -= 1
self.frame.fSizer.Layout()
self.frame.Fit()
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="Add / Remove Buttons")
self.fSizer = wx.BoxSizer(wx.VERTICAL)
panel = MyPanel(self)
self.fSizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(self.fSizer)
self.Fit()
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
del app
Please could someone advise on how I can fix this alignment issue.
Thanks a lot in advance!
Duh! It turns out I just needed to change the alignment when adding items to the row sizer.
rowSizer.Add(myObject, 0, wx.ALIGN_CENTER, 5)
I'm using wxpython to create a GUI.
The idea is that whenever I select a row, something will happen on notebook1 and notebook 2, and different tabs will appear with different related information.
However, when I bind an event when selecting a row, weird beird black squares appear on the tab titles. What's wrong?
import wx
import threading
from time import sleep
class VAR():
def __init__(self):
self.result_row = ''
var = VAR()
class TabOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the first tab", (20, 20))
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 GUI(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(1000, 1000), style=wx.DEFAULT_FRAME_STYLE &
~wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, pos=(100, 0))
self.panel = wx.Panel(self)
self.hsizer = wx.BoxSizer(wx.VERTICAL)
first_panel = wx.Panel(self.panel, size=(1000, 420))
self.hsizer.Add(first_panel, 1)
self.second_panel = wx.Panel(self.panel, size=(1000, 600))
self.notebook1 = wx.Notebook(self.second_panel, size=(1000, 230))
self.notebook2 = wx.Notebook(self.second_panel, size=(1000, 400))
self.hsizer.Add(self.second_panel, 1)
self.second_panel_sizer = wx.BoxSizer(wx.VERTICAL)
self.second_panel_sizer.Add(self.notebook1, 1, wx.EXPAND)
self.second_panel_sizer.Add(self.notebook2, 2, wx.EXPAND)
self.second_panel.SetSizerAndFit(self.second_panel_sizer)
self.panel.SetSizerAndFit(self.hsizer)
var.result_row = wx.ListCtrl(
first_panel, -1, style=wx.LC_REPORT, size=(980, 245), pos=(0, 175))
var.result_row.InsertColumn(0, "No.")
var.result_row.InsertColumn(1, "2 ")
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.SelectRow, var.result_row)
def SelectRow(self, event):
while (self.notebook1.GetPageCount()):
self.notebook1.DeletePage(0)
while (self.notebook2.GetPageCount()):
self.notebook2.DeletePage(0)
tab1 = TabOne(self.notebook1)
self.notebook1.AddPage(tab1, "Tab 1")
sizer = wx.BoxSizer()
sizer.Add(self.notebook1, 1, wx.EXPAND)
self.second_panel.SetSizer(sizer)
tab2 = TabTwo(self.notebook2)
self.notebook2.AddPage(tab2, "Tab 2")
sizer = wx.BoxSizer()
sizer.Add(self.notebook2, 1, wx.EXPAND)
self.second_panel.SetSizer(sizer)
def InfiniteProcess():
for i in range(100):
sleep(0.1)
var.result_row.Append(str(i))
finish = False
a = threading.Thread(target=InfiniteProcess)
a.setDaemon(1)
a.start()
app = wx.App()
frame = GUI(None, -1, "a")
frame.Show()
app.MainLoop()
sample
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 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.