steve2
January 18, 2023, 9:14am
1
Hi,
I create a frame and a toolbar on it. Then I add a radiobox on toolbar.
But since some of the label strings on the radiobox are longer than the others, I think it sets all labels’ width same according to the longest label. Therefore, the radiobox expands to the right on the toolbar and it doesn’t look good. How do I prevent radiobox from expanding to the right? I would like each label on the radiobox to have minimum width.
Thanks
Best Regards
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Frame",
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX,
size=wx.Size(800, 200))
self.toolbar = self.CreateToolBar(True)
item_list = ["Button 1", "Long Label Button 2", "Button 3", "Button 4", "Button 5",]
self.radio_box = wx.RadioBox(self.toolbar, -1, "Box", wx.DefaultPosition, wx.DefaultSize, item_list, 0, wx.RA_SPECIFY_COLS)
self.toolbar.AddSeparator()
self.toolbar.AddControl(self.radio_box)
self.toolbar.Bind(wx.EVT_RADIOBOX, self.EvtRadioBox, self.radio_box)
def EvtRadioBox(self, event):
self.selected_item_ix = self.radio_box.GetSelection()
self.selected_item = self.radio_box.GetString(self.selected_item_ix)
print("selected term: ", self.selected_item)
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
Hi steve2,
I think the fixed width seems to be the default and cannot be changed.
If you’re not happy with that, you’ll have to create a custom control, e.g.
class RadioBox(wx.Control):
def __init__(self, parent, label, choices):
super().__init__(parent, style=wx.BORDER_NONE)
box = wx.StaticBox(self, -1, label)
sizer = wx.StaticBoxSizer(box)
for v in choices:
rb = wx.RadioButton(self, -1, v)
sizer.Add(rb, 0, wx.EXPAND | wx.ALL, 0)
self.SetSizer(sizer)
sizer.Fit(self)
Usage:
self.radio_box = RadioBox(
self.toolbar, label="Box", choices=item_list)
self.radio_box.Bind(wx.EVT_RADIOBUTTON,
lambda v: print(v.EventObject.Label))
I would like each label on the radiobox to have minimum width.
I assume you mean maximum.
So take komoto’s idea and simply add a width restriction parameter.
Another quirk, seems to be that the box overrides the individual tooltips, which is annoying but can be utilised.
Combine the choices to make a single tooltip for the radiobox but still assign each radio button its own tooltip, as this can be utilised instead of the label, for event processing.
See:
import wx
class RadioBox(wx.Control):
def __init__(self, parent, label, choices, labelwidth=None):
super().__init__(parent, style=wx.BORDER_NONE)
box = wx.StaticBox(self, -1, label)
sizer = wx.StaticBoxSizer(box)
for v in choices:
rb = wx.RadioButton(self, -1, v[:labelwidth])
rb.SetToolTip(v)
sizer.Add(rb, 0, wx.ALL, 0)
self.SetSizer(sizer)
sizer.Fit(self)
tip = "\n".join(choices)
self.SetToolTip(tip)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Frame",
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX,
size=wx.Size(800, 200))
self.toolbar = self.CreateToolBar(True)
item_list = ["Button 1", "Long Label Button 2", "Button 3", "Button 4", "Button 5",]
self.radio_box = RadioBox(self.toolbar, "Box", choices=item_list, labelwidth=10)
self.toolbar.AddSeparator()
self.toolbar.AddControl(self.radio_box)
self.radio_box.Bind(wx.EVT_RADIOBUTTON, self.EvtRadioBox)
def EvtRadioBox(self, event):
obj = event.GetEventObject()
print("selected term: ", obj.GetToolTip().GetTip())
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
1 Like