Related
In my app, I want to handle allocation / deallocation of sizers inside a scrolledPanel. In my first try, I was hiding / showing the contents of the sizers, but this was causing a lot of problems. The hidden sizers would "stay there", effectively ocuppying space, it would not update propely, etc. The solution that worked better was to destroy and create them over and over again.
But this that its problems too. The scrolledPanel blinks while I'm updating it and seems to be resource heavy. In my real app, I put the references of the buttons and checkboxes in lists and it becames more dificult to handle them.
So, if anyone has a better solution, I'm all ears!
I'm up to a hiding / showing solution! Something to reset the size of the scrolledPanel to accomodate only the sizer it currently has it would be nice too.
Thanks!
import wx
import wx.lib.scrolledpanel as scrolled
class WaterDataBase(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.setupSizers()
self.setupData()
def setupSizers(self):
masterSizer = wx.BoxSizer(wx.HORIZONTAL)
itemsSizer = wx.BoxSizer(wx.VERTICAL)
itemsSizer.Add(self.addSearchControl(), flag=wx.ALL, border=7)
self.scrolledPanelSizer = wx.BoxSizer(wx.VERTICAL)
self.scrolled_panel = scrolled.ScrolledPanel(self, wx.ID_ANY, size=(200, 200))
self.scrolled_panel.SetSizer(self.scrolledPanelSizer)
itemsSizer.Add(self.scrolled_panel, flag=wx.ALL, border=7)
masterSizer.Add(itemsSizer)
self.scrolled_panel.SetupScrolling()
self.SetSizer(masterSizer)
def addSearchControl(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.searchField = wx.TextCtrl(self, -1)
self.searchField.Bind(wx.EVT_TEXT, self.OnSearched)
sizer.Add(self.searchField)
return sizer
def setupData(self):
self.words = "I'm trying to make this work, please. Let's keep it on! The day is beautiful today. Together we are stronger!".split()
for word in self.words:
self.addSizerToPanel(word)
def createSizer(self, word):
# Creates a sizer with a CheckBox and a StaticText to display.
sizer = wx.BoxSizer(wx.HORIZONTAL)
checkBox = wx.CheckBox(self.scrolled_panel, -1)
text = wx.StaticText(self.scrolled_panel, -1, word)
sizer.Add(checkBox, flag=wx.ALL, border=5)
sizer.Add(text, flag=wx.LEFT, border=5)
return sizer
def addSizerToPanel(self, word):
sizer = self.createSizer(word)
self.scrolledPanelSizer.Add(sizer, flag=wx.ALL, border=5)
def OnSearched(self, event):
query = self.searchField.GetValue().lower()
result = []
# If query's empty, print all words
if not query or query.isspace():
for word in self.words:
result.append(word)
else:
for word in self.words:
if word.lower().find(query) != -1:
result.append(word)
# Destroy all panel sizers and put exactly the ones we want.
self.scrolled_panel.DestroyChildren()
for word in result:
self.addSizerToPanel(word)
self.scrolled_panel.Layout()
self.scrolled_panel.Scroll(0, 0) # Using this to cause the scrollPanel get back to the top.
app = wx.App()
frame = WaterDataBase(None).Show()
app.MainLoop()
So, it seems I've made it, finally. I still wants the advice about reseting the size of the scrolledPanel. xD
import wx
import wx.lib.scrolledpanel as scrolled
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.sizerRefs = []
self.words = []
self.setupSizers()
self.setupData()
def setupSizers(self):
masterSizer = wx.BoxSizer(wx.HORIZONTAL)
itemsSizer = wx.BoxSizer(wx.VERTICAL)
itemsSizer.Add(self.addSearchControl(), flag=wx.ALL, border=7)
self.scrolledPanelSizer = wx.BoxSizer(wx.VERTICAL)
self.scrolled_panel = scrolled.ScrolledPanel(self, wx.ID_ANY, size=(200, 200))
self.scrolled_panel.SetSizer(self.scrolledPanelSizer)
itemsSizer.Add(self.scrolled_panel, flag=wx.ALL, border=7)
masterSizer.Add(itemsSizer)
self.scrolled_panel.SetupScrolling()
self.SetSizer(masterSizer)
def addSearchControl(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.searchField = wx.TextCtrl(self, -1)
self.searchField.Bind(wx.EVT_TEXT, self.OnSearched)
sizer.Add(self.searchField)
return sizer
def setupData(self):
self.words = "I'm trying to make this work, please. Let's keep it on! The day is beautiful today. Together we are stronger!".split()
for i in range(0, len(self.words)):
self.addSizerToPanel(i)
def createSizer(self, index):
sizer = wx.BoxSizer(wx.HORIZONTAL)
checkBox = wx.CheckBox(self.scrolled_panel, -1)
text = wx.StaticText(self.scrolled_panel, -1, self.words[index])
sizer.Add(checkBox, flag=wx.ALL, border=5)
sizer.Add(text, flag=wx.LEFT, border=5)
self.sizerRefs.append(sizer)
return sizer
def addSizerToPanel(self, index):
sizer = self.createSizer(index)
self.scrolledPanelSizer.Add(sizer, flag=wx.ALL, border=5)
def hideAllSizers(self):
for sizer in self.sizerRefs:
sizer.ShowItems(False)
def unhideSizer(self, index):
self.sizerRefs[index].ShowItems(True)
def OnSearched(self, event):
query = self.searchField.GetValue().lower()
result = [] # Storing the indexes of the words found
# If query's empty, print all words
if not query or query.isspace():
for i in range(0, len(self.words)):
result.append(i)
else:
for i in range(0, len(self.words)):
if self.words[i].lower().find(query) != -1:
result.append(i)
# Hides all panel sizers and unhide exactly the ones we want.
self.hideAllSizers()
for i in range(0, len(result)):
self.unhideSizer(result[i])
self.scrolled_panel.Layout()
self.scrolled_panel.Scroll(0, 0) # Using this to cause the scrollPanel get back to the top.
app = wx.App()
frame = Frame(None).Show()
app.MainLoop()
I have a wxPython GUI layout that I would like to be user-configurable. Easiest example would be something like this:
+++++++++++++++++
+ Text1 + Text2 +
+++++++++++++++++
Then I want to add a button that will hide "Text2" and allow "Text1" to overtake that space, being centered horizontally in that space, like this:
+++++++++++++++++
+ Text1 +
+++++++++++++++++
I have tried various things with GridBagSizers and positioning these text controls in various other horizontal and vertical boxsizers, but I can't seem to figure out how to accomplish this. I can get "Text2" to be hidden of course, but it keeps that space reserved for it, meaning that "Text1" still occupies the space to the left and is not centered horizontally in the entire space.
Any ideas?
The key is the Layout() command, to ensure that the sizers recalculate themselves and their children.
Here is an example that will toggle the text:
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Show/Hide", size=(300,150))
self.t1 = wx.StaticText(self, -1, ' Text1 ')
self.t2 = wx.StaticText(self, -1, ' ')
self.t3 = wx.StaticText(self, -1, ' Text2 ')
b1 = wx.Button(self, -1, 'Adjust Sizer')
self.vsizer = wx.BoxSizer(wx.VERTICAL)
self.hsizer = wx.BoxSizer(wx.HORIZONTAL)
self.vsizer.Add(wx.StaticText(self, -1, '****************************'), 0, wx.ALIGN_CENTRE_HORIZONTAL, 0)
self.hsizer.Add(self.t1, 1, 0, 0)
self.hsizer.Add(self.t2, 0, 0, 0)
self.hsizer.Add(self.t3, 1, 0, 0)
self.vsizer.Add(self.hsizer,0,wx.ALIGN_CENTRE_HORIZONTAL)
self.vsizer.Add(wx.StaticText(self, -1, '****************************'), 0, wx.ALIGN_CENTRE_HORIZONTAL, 0)
self.vsizer.Add(b1, 0, 0, 0)
self.Bind(wx.EVT_BUTTON, self.OnAssign)
self.SetSizer(self.vsizer)
self.Show()
def OnAssign(self, event):
if self.t2.IsShown():
self.t2.Hide()
self.t3.Hide()
else:
self.t2.Show()
self.t3.Show()
self.Layout()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
app.MainLoop()
You may have to get inventive, if you want the extra * characters in there as well.
With wxpython, I want to create multiple StaticBox(s) on a panel, add some widgets to each staticbox. How can I do that? A minimal example to start with:
import wx
app = wx.App()
frame = wx.Frame(None, size=(700, 500))
panel = wx.Panel(frame)
vbox = wx.BoxSizer(wx.VERTICAL)
# Three StaticBox, with a StaticText within each
for n in range(3):
sizer = wx.StaticBoxSizer(wx.VERTICAL, wx.StaticBox(panel,label=f"Box {n}"))
sizer.Add(wx.StaticText(panel, label=f"StaticText of box {n}"))
vbox.Add(sizer, 1, wx.EXPAND | wx.ALL, 5)
panel.SetSizer(vbox)
frame.Show()
app.MainLoop()
What's wrong with my code, because I got the following window:
The manual https://wxpython.org/Phoenix/docs/html/wx.StaticBoxSizer.html gives a good example of how to use this widget container.
Here is your method modfied and a more pedantic method, commented out, using the old adage explict is better than implicit.
import wx
app = wx.App()
frame = wx.Frame(None, size=(700, 500))
panel = wx.Panel(frame)
vbox = wx.BoxSizer(wx.VERTICAL)
# Three StaticBox, with a StaticText within each
#sz1 = wx.StaticBoxSizer(wx.VERTICAL, panel, "Box1")
#sz1.Add(wx.StaticText(sz1.GetStaticBox(), wx.ID_ANY,"This window is a child of staticbox 1"))
#sz2 = wx.StaticBoxSizer(wx.VERTICAL, panel, "Box2")
#sz2.Add(wx.StaticText(sz2.GetStaticBox(), wx.ID_ANY,"This window is a child of staticbox 2"))
#sz3 = wx.StaticBoxSizer(wx.VERTICAL, panel, "Box3")
#sz3.Add(wx.StaticText(sz3.GetStaticBox(), wx.ID_ANY,"This window is a child of staticbox 3"))
#vbox.Add(sz1, 1, wx.EXPAND | wx.ALL, 5)
#vbox.Add(sz2, 1, wx.EXPAND | wx.ALL, 5)
#vbox.Add(sz3, 1, wx.EXPAND | wx.ALL, 5)
for n in range(3):
sizer = wx.StaticBoxSizer(wx.VERTICAL, panel,label=f"Box {n}")
sizer.Add(wx.StaticText(sizer.GetStaticBox(), label=f"StaticText of box {n}"))
vbox.Add(sizer, 1, wx.EXPAND | wx.ALL, 5)
panel.SetSizer(vbox)
frame.Show()
app.MainLoop()
It should be noted that some themes (on Linux, don't know about other platforms) don't display the box. So while it may work on your machine, it may not on others, depending on their chosen desktop appearance.
I am a humanities teacher, trying to adapt a simple app to help teachers manage classroom interaction - it takes attendance, then allows for calling on random attending students in class, or breaking them into groups, or recording an excused absence, and so on. I've long had a working version in PHP / MySQL running locally on my laptop, but I want to make it a portable python / sqlite app, with an eye toward releasing into the wild for other teachers to use and improve.
But I'm totally new to wxPython, and not really strong on object-oriented programming generally. I have spent hours reading (or skimming through) a fair number of tutorials and introductions and StackOverflow questions, and I've played with wxFormBuilder, but I do not feel like I'm making progress - I'm still quite confused about panels and sizers and layouts, and which bits should belong to what parents.
I think if I could just get this minimal version working, that would go a long way toward my real app. The toolbar-like buttons along the top seem to work fine, but I'd like a "display" area below with a minimal (but expandable) vertical size, a centered text area, and a row of 2-3 buttons in the display area below that text. This display area should change and clear depending on the button pushed above. Here's what I've got:
#!/usr/bin/env python3
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(-1,-1))
# self.panel = wx.Panel(self, size=(-1,300))
# buttons bar
self.top_button_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.btn_left = wx.Button(self, -1, label="left")
self.btn_right = wx.Button(self, -1, label="right")
self.btn_left.Bind(wx.EVT_BUTTON, self.OnLeft)
self.btn_right.Bind(wx.EVT_BUTTON, self.OnRight)
self.top_button_sizer.Add(self.btn_left, 1, wx.EXPAND)
self.top_button_sizer.Add(self.btn_right, 1, wx.EXPAND)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.display_sizer = wx.BoxSizer(wx.VERTICAL)
self.text_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.display_text = wx.StaticText(self, label="Push a button!")
self.display_buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.text_sizer.Add(self.display_text, 0, wx.ALIGN_CENTER, 5)
self.display_sizer.Add(self.text_sizer, 1, wx.ALIGN_CENTER, 5)
self.display_sizer.Add(self.display_buttons_sizer, 0, wx.EXPAND)
self.sizer.Add(self.top_button_sizer, 0, wx.TOP | wx.EXPAND)
# self.sizer.Add(self.panel)
self.sizer.Add(self.display_sizer, 1, wx.EXPAND)
self.SetSizerAndFit(self.sizer)
self.Show()
def OnLeft(self,e):
# for child in self.display_buttons_sizer.GetChildren():
# child.Destroy()
# ^ my attempt to "clear" causes a SegFault
for child in self.display_buttons_sizer.GetChildren():
child.Destroy()
self.display_text.SetLabel("Hey lefty!")
self.btn_hey = wx.Button(self, -1, label="Hey yourself lefty.")
self.btn_whoa = wx.Button(self, -1, label="Whoa there lefty.")
self.display_buttons_sizer.Add(self.btn_hey, 1, wx.EXPAND)
self.display_buttons_sizer.Add(self.btn_whoa, 1, wx.EXPAND)
self.sizer.Layout()
def OnRight(self,e):
# for child in self.display_buttons_sizer.GetChildren():
# child.Destroy()
# ^ my attempt to "clear" causes a SegFault!
self.display_text.SetLabel("Hey righty!")
self.btn_hey = wx.Button(self, -1, label="Hey yourself righty.")
self.btn_whoa = wx.Button(self, -1, label="Whoa there righty.")
self.display_buttons_sizer.Add(self.btn_hey, 1, wx.EXPAND)
self.display_buttons_sizer.Add(self.btn_whoa, 1, wx.EXPAND)
self.sizer.Layout()
app = wx.App(False)
frame = MainWindow(None, "MWE")
app.MainLoop()
(I'm sure this is abhorrent code in lots of ways.) Should I put in a panel somewhere? Where, and how? Would it be easier to learn about "notebooks" instead of using my top buttons? How do I properly clear what's in the "display" area below for the next button push?
Bonus points: the most complicated of the top buttons on my real app is for taking attendance. It would display many lines of text (the students in that class, which I get from sqlite; I've worked that part out I think) with radio buttons for present / absent next to each line, and then record in the database. Hints about this would also be appreciated.
Thanks in advance for your patience.
You could do this sort of thing several different ways. You can swap out panels when a button is pushed or you could just use a wx.Notebook which is basically the same idea. I wrote up a tutorial on panel switching here that you might find helpful:
https://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/
It uses a menu instead of buttons, but that wouldn't be hard to change. Here's a simple example that doesn't do panel switching, but does show how to add a multiline text control that clears itself when a button is pressed:
import wx
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
left_button = wx.Button(self, label='Left')
left_button.Bind(wx.EVT_BUTTON, self.on_left)
btn_sizer.Add(left_button, 0, wx.ALL, 5)
right_button = wx.Button(self, label='Right')
right_button.Bind(wx.EVT_BUTTON, self.on_right)
btn_sizer.Add(right_button, 0, wx.ALL, 5)
self.text_ctrl = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.main_sizer.Add(btn_sizer, 0, wx.CENTER)
self.main_sizer.Add(self.text_ctrl, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(self.main_sizer)
def on_left(self, event):
self.text_ctrl.Clear()
self.text_ctrl.SetValue('Left')
def on_right(self, event):
self.text_ctrl.Clear()
self.text_ctrl.SetValue('Right')
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(-1,-1))
panel = MainPanel(self)
self.Show()
app = wx.App(False)
frame = MainWindow(None, "MWE")
app.MainLoop()
This also demonstrates how to add a panel, which is recommended as panels give you the right "look" on all platforms and it also enables tabbing between controls.
You could easily add another set of buttons to the bottom by using the same concepts shown here and then just adding the second set of button's sizer to the main sizer.
If you want to do lines of text with some kind of radio or check button, I would recommend using a wx.ListCtrl or (better), ObjectListView (https://objectlistview-python-edition.readthedocs.io/en/latest/recipes.html#recipe-checkbox) (see also https://www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/)
You can tie an event to the checkbox that can then update your database appropriately.
I'm using wxPython to build a GUI and I'm trying to align some text but it's not working at all.
I'm trying align three different static text items in three places (right aligned, center aligned, and left aligned) in three seperate panels. The result that I'm getting though is that all three static text controls are aligned in the top left corners of their respective panels.
This is code I have:
self.lastText=wx.StaticText(self.textDisplayPanel1, label=self.lastWords, style=wx.ALIGN_RIGHT)
self.currentText=wx.StaticText(self.textDisplayPanel2, label=self.currentWords, style=wx.ALIGN_CENTRE)
self.nextText=wx.StaticText(self.textDisplayPanel3, label=self.nextWords, style=wx.ALIGN_LEFT)
Any ideas on how I fix this?
Thank you!
I'm running mac OSX 10.7.2 with Python 2.7.1 and wxPython 2.8.12.1
Edit: Although everything commented below works on windows, the first option would not work on, for example, Ubuntu due to maybe a bug. A previous post given in the comments indicate that the same problem is found in OSX.
In any case, the second option using vertical sizers works both in Ubuntu and windows so you could try it on OSX.
Your text has the instruction to align in the way you want with wx.ALIGN... and, in fact, it is aligned. However the size of the StaticText is not that of the panel but just the size of the text. Having restricted its position to its own size, you can not see the diference between the alignment modes.
You have two options to solve the problem:
Option 1. Expand the StaticText widget size and position your text on it
You could expand the size of your StaticText widget using its size parameter. This is a bad solution except for fixed size parents or frames you are not going to change size or reuse in other applications. If the size of the widget containing the text changes then the relative position of the text will be also modified because its size stays fixed. So it is always better to organize your widgets by means of sizers.
The proportion of the available space the widget occupies in the sizer slot is given by the second parameter in sizer.Add() (0 is minimal size, 1 is full occupation):
sizer_2.Add(self.label_1, 0, 0, 0)
To see the text aligned in the panel as you want you have to tell the StaticText to expand to all the space available:
sizer_2.Add(self.label_1, 1, 0, 0)
Here you have the relevant code:
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.label_1 = wx.StaticText(self.panel_1, -1, "label_1", style=wx.ALIGN_RIGHT)
self.panel_2 = wx.Panel(self, -1)
self.label_2 = wx.StaticText(self.panel_2, -1, "label_2", style=wx.ALIGN_CENTRE)
self.panel_3 = wx.Panel(self, -1)
self.label_3 = wx.StaticText(self.panel_3, -1, "label_3")
self.panel_1.SetBackgroundColour(wx.Colour(0, 255, 0))
self.panel_2.SetBackgroundColour(wx.Colour(0, 255, 255))
self.panel_3.SetBackgroundColour(wx.Colour(219, 112, 147))
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.label_1, 1, 0, 0)
sizer_3.Add(self.label_2, 1, 0, 0)
sizer_4.Add(self.label_3, 1, 0, 0)
self.panel_1.SetSizer(sizer_2)
self.panel_2.SetSizer(sizer_3)
self.panel_3.SetSizer(sizer_4)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_3, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
Note the code is longer than needed in order to mimick your example with three panels. You obtain the same frame view using one only panel. In fact it could be simplified further not using panels and setting the StaticText directly on the sizer:
class MyFrame2(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.label_1 = wx.StaticText(self, -1, "label_1", style=wx.ALIGN_RIGHT)
self.label_2 = wx.StaticText(self, -1, "label_2", style=wx.ALIGN_CENTRE)
self.label_3 = wx.StaticText(self, -1, "label_3")
self.label_1.SetBackgroundColour(wx.Colour(127, 255, 0))
self.label_2.SetBackgroundColour(wx.Colour(0, 255, 255))
self.label_3.SetBackgroundColour(wx.Colour(219, 112, 147))
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.label_1, 1, wx.EXPAND, 0)
sizer.Add(self.label_2, 1, wx.EXPAND, 0)
sizer.Add(self.label_3, 1, wx.EXPAND, 0)
self.SetSizer(sizer)
sizer.Fit(self)
self.Layout()
Option 2. Locate the widget itself in the desired position at the available space of the sizer.
You could use the position parameter of StaticText for that purpose. But this would have the same problems indicated above for the use of size. So again you want to control the geometry of your views with sizers.
You position the widget in the sizer using one of:
sizer_6.Add(self.label_5, 0, wx.ALIGN_RIGHT, 0)
or
sizer_7.Add(self.label_6, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
For some reason, for this to work you need a vertical BoxSizer (and in the same way, if you would like to use wx.ALIGN_CENTER_VERTICAL you will need an horizontal BoxSizer:
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.panel_4 = wx.Panel(self, -1)
self.label_5 = wx.StaticText(self.panel_4, -1, "label_5")
self.panel_5 = wx.Panel(self, -1)
self.label_6 = wx.StaticText(self.panel_5, -1, "label_6")
self.panel_6 = wx.Panel(self, -1)
self.label_7 = wx.StaticText(self.panel_6, -1, "label_7")
self.panel_4.SetBackgroundColour(wx.Colour(0, 255, 255))
self.panel_5.SetBackgroundColour(wx.Colour(127, 255, 0))
self.panel_6.SetBackgroundColour(wx.Colour(219, 112, 219))
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer_8 = wx.BoxSizer(wx.VERTICAL)
sizer_7 = wx.BoxSizer(wx.VERTICAL)
sizer_6 = wx.BoxSizer(wx.VERTICAL)
sizer_6.Add(self.label_5, 0, wx.ALIGN_RIGHT, 0)
sizer_7.Add(self.label_6, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
sizer_8.Add(self.label_7, 0, 0, 0)
self.panel_4.SetSizer(sizer_6)
self.panel_5.SetSizer(sizer_7)
self.panel_6.SetSizer(sizer_8)
sizer_1.Add(self.panel_4, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_5, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_6, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
This option implies a combination of panels and sizers that produces a code that is more difficult to simplify than that shown for the first option.
I think that text is not aligned because the size is not set. Try:
t1=wx.StaticText(smth, label="foo", pos=(0,0), size=(200,20), style=wx.ALIGN_RIGHT)
t2=wx.StaticText(smth, label="bar", pos=(0,0), size=(200,20), style=wx.ALIGN_CENTRE)
And it will be aligned inside a 200*20 box. By default the size is "auto" i.e. (-1,-1) i.e. just enough for the text to be visible, and aligning in that box will not give visible effect.
However, this does not always work.
I also had the same problem yesterday, I use MAC OS X, wxpython 4.0.6.
At last I find out this script works for me.
And I realise that if u don't SetSizer at last, the statictext will not align at center.
My code