PyQt5 resize only one child widget when window is resized - python

I'm building a GUI in PyQt5. Its structre will look like on the attached picture.
What I need is to managed that when window is resized:
Blue area resizes both horizontally and vertically
Read and green areas resize only vertically
purple area resizes only horizontally
user can change size of widget sizes manually as he desires, but the should keep its width/heigh when whole window resizes
Here is code for this example:
def initUI(self):
topHBoxLayout = QHBoxLayout(self)
topHBoxLayout.setContentsMargins(0,0,0,0)
sstFrame = QFrame()
sstFrame.setMinimumSize(QSize(100, 100))
self.colorFrame(sstFrame, 'red')
objectivesFrame = QFrame()
objectivesFrame.setMinimumSize(QSize(100, 100))
self.colorFrame(objectivesFrame, 'green')
wsTop = QFrame()
wsTop.setMinimumSize(QSize(100, 100))
self.colorFrame(wsTop, 'blue')
wsBottom = QFrame()
wsBottom.setMinimumSize(QSize(100, 100))
self.colorFrame(wsBottom, 'purple')
workspaceSplitter = QSplitter(Qt.Vertical)
workspaceSplitter.addWidget(wsTop)
workspaceSplitter.addWidget(wsBottom)
mainSplitter = QSplitter(Qt.Horizontal)
mainSplitter.addWidget(sstFrame)
mainSplitter.addWidget(objectivesFrame)
mainSplitter.addWidget(workspaceSplitter)
topHBoxLayout.addWidget(mainSplitter)
self.setLayout(topHBoxLayout)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('')
self.show()
Thanks for any advice, I couldn't figure it out myself.

No matter what I tried, I could not make anything work for three widgets attached to a single splitter instance (I tried SizePolicy settings, QSplitter stretch factors, ...).
So a tried to wrap it with few more QFrames and seems to work. But I'm still opened for solution for the original code.
Here's my result:
def initUI(self):
topHBoxLayout = QHBoxLayout(self)
topHBoxLayout.setContentsMargins(0,0,0,0)
sstObjWrapper = QSplitter(Qt.Horizontal)
sstObjWrapper.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
sstFrame = QFrame()
sstFrame.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
sstFrame.setMinimumSize(QSize(100, 100))
self.colorFrame(sstFrame, 'red')
# --- sst frame ---
objectivesFrame = QFrame()
objectivesFrame.setMinimumSize(QSize(100, 100))
objectivesFrame.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.colorFrame(objectivesFrame, 'green')
sstObjWrapper.addWidget(sstFrame)
sstObjWrapper.addWidget(objectivesFrame)
tmp1 = QFrame(self)
tmp1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
tmp1.setMinimumSize(QSize(200, 200))
self.colorFrame(tmp1, 'purple')
tmp2 = QFrame(self)
tmp2.setMinimumSize(QSize(100, 100))
tmp2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.colorFrame(tmp2, 'blue')
plotConsoleSplitter = QSplitter(Qt.Vertical)
plotConsoleSplitter.addWidget(tmp1)
plotConsoleSplitter.addWidget(tmp2)
plotConsoleSplitter.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
objPlotSplitter = QSplitter(Qt.Horizontal)
objPlotSplitter.addWidget(sstObjWrapper)
objPlotSplitter.addWidget(plotConsoleSplitter)
objPlotSplitter.setStretchFactor ( 0, 0 )
objPlotSplitter.setStretchFactor ( 1, 1 )
topHBoxLayout.addWidget(objPlotSplitter)
self.setLayout(topHBoxLayout)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('')
self.show()

Related

Change background color of a Canvas ScaledText

I'm using wxPython Canvas' AddScaledTextBox(...) to create a scaled Text Box.
See here for example:
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
2,
BackgroundColor = "Yellow",
LineColor = "Red",
LineStyle = "Solid",
PadSize = 5,
Family = wx.TELETYPE,
Position = 'bl')
How to change the Background color, later, after the textbox has been defined? (for example user input => background color change)
Pretty sure this is a bug. See lines 1841 - 1845 in the wxPython source - there is a reference to self.BackgroundColor but it's never used.
So for your example, you need to set the color of the brush associated with the text box. I've based the following off of Robin Dunn's example. The function SetBoxBackground is the important part.
As noted in the comments, just calling box.Brush.SetColour(color) or box.Pen.SetColour(color) can cause issues: if you have two text boxes with the same color, changing the brush/pen color for one will also affect the other. (I'm not 100% sure, but I think this is because of caching, see for example this comment in the source.)
import wx
from wx.lib.floatcanvas import FloatCanvas
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.canvas = FloatCanvas.FloatCanvas(self, BackgroundColor = "black")
MainSizer = wx.BoxSizer(wx.VERTICAL)
MainSizer.Add(self.canvas, 4, wx.EXPAND)
self.SetSizer(MainSizer)
self.box = self.canvas.AddScaledTextBox("A Two Line\nString",
(0,10),
2,
BackgroundColor = "Yellow",
LineColor = "Red",
LineStyle = "Solid",
PadSize = 5,
Family = wx.TELETYPE,
Position = 'bl')
self.box2 = self.canvas.AddScaledTextBox("Second Box",
(0,0),
2,
BackgroundColor = "Yellow",
LineColor = "Red",
LineStyle = "Solid",
PadSize = 5,
Family = wx.TELETYPE,
Position = 'bl')
self.box.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnRectDown)
wx.CallAfter(self.canvas.ZoomToBB)
def OnRectDown(self, event):
print ('Rectangle: Left Button down clicked at:', event.HitCoords)
if self.box.Color == 'red':
self.SetBoxBackground('green', "Solid")
self.box.SetColor('black')
else:
self.SetBoxBackground('white', "Solid")
self.box.SetColor('red')
self.canvas.ClearBackground()
self.canvas.Refresh()
self.canvas.Draw(True)
def SetBoxBackground(self, color, style, linewidth=1):
# See https://github.com/wxWidgets/wxPython/blob/master/wx/lib/floatcanvas/FloatCanvas.py#L1841
# Create a new brush (fill) with specified color
self.box.SetBrush(color, style)
# Create a new pen (line) with specified color
self.box.SetPen(color, style, linewidth)
self.box.SetBackgroundColor(color) # Now this works correctly
app = wx.App(0)
frame = TestFrame(None, title="Test")
frame.Show(True)
app.MainLoop()
The ScaledTextBox class derives from TextObjectMixin which has SetColor and SetBackgroundColor methods.
Code:
import wx
from wx.lib.floatcanvas import FloatCanvas
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.canvas = FloatCanvas.FloatCanvas(self, BackgroundColor = "black")
MainSizer = wx.BoxSizer(wx.VERTICAL)
MainSizer.Add(self.canvas, 4, wx.EXPAND)
self.SetSizer(MainSizer)
self.A = self.canvas.AddScaledTextBox('yoda', (0,0), Position = 'tl', Alignment = 'center', PadSize = 10, Size = 15, Width = 150, BackgroundColor = 'white')
self.A.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnRectDown)
wx.CallAfter(self.canvas.ZoomToBB)
def OnRectDown(self, event):
print 'Rectangle: Left Button down clicked at:', event.HitCoords
if self.A.Color == 'red':
self.A.SetColor('black')
else:
self.A.SetColor('red')
self.canvas.Draw(True)
app = wx.App(0)
frame = TestFrame(None, title="Test")
frame.Show(True)
app.MainLoop()
EDIT:
Forced a redraw of the canvas after the color change. Also, changing the background color isn't sticking for some reason, so I switched to demoing changing the text color.
Removed secondary edit because it was just creating a new text box instead of updating the original.

Python Qt bindings: How to increase the width of lines with width 0

With the following simple example (which works well with either PySide or PyQt4 in my computer):
import sys
import random
import numpy
from PySide import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.resize(800, 500)
self.view = QtGui.QGraphicsView()
self.scene = QtGui.QGraphicsScene()
self.view.setScene(self.scene)
self.setWindowTitle('Example')
# Layout
layout = QtGui.QGridLayout()
layout.addWidget(self.view, 0, 0)
self.setLayout(layout)
# Styles
self.pen = QtGui.QPen(QtCore.Qt.black, 0, QtCore.Qt.SolidLine)
self.brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 255))
def addLine(self, x0, y0, x1, y1):
line = QtCore.QLineF(x0, -y0, x1, -y1)
pen = QtGui.QPen(QtGui.QColor(250, 0, 0, 255), 0, QtCore.Qt.SolidLine)
pen.setStyle(QtCore.Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
l = self.scene.addLine(line, pen)
def addRect(self, left, top, width, height):
rect = QtCore.QRectF(left, -top, width, abs(height))
r = self.scene.addRect(rect, self.pen, self.brush)
def fit(self):
self.view.fitInView(self.scene.sceneRect())
def resizeEvent(self, event = None):
self.fit()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
window.addRect(0, 1, 1, 1)
window.addLine(-1, -1, 2, 2)
window.addLine(0, 1, 1, 0)
window.fit()
sys.exit(app.exec_())
I am able to paint a two red lines that have constant width; which means that they do not change no matters the size (coordinates) of the square and lines and no matters if I re-size the window:
This is because I am using a QtGui.Qpen with width 0. However, if I use another width > 0, then the observed line width will change on window re-size (or will change if the square and lines have other dimensions too):
Is it possible to change (increase) the line width so that the observed lines are thicker than those obtained when the width is set to 0, while maintaining the same "observed" width on window resize or when the dimensions of the square/lines vary?
EDIT
Using setCosmetic(True), as suggested by o11c, has an unexpected behavior (at least I would not expect that to happen); it adds margins to the image (increases the size of scene.sceneRect()). These margins seem to be proportional to the width of the QPen when isCosmetic() == True:
A new question has been created related to this issue. See question 26231374 for more details:
According to the Qt C++ documentation, use the setCosmetic function on QPen. I assume the python wrapper exposes this.

wxPython ScrolledWindow too small

I have a panel to control editing of a matplotlib graph in a wxPython frame. The wxPython install was recently updated to 2.8.12.1 from 2.6.4.0 and it broke a few things, namely, the scroll panel no longer fills a block but instead stays at a minimum size. I'm just now picking this up from a year ago so I'm a bit rusty. Any help would be much appreciated!
Below is a stripped-down version of the code that can be run on its own and displays the problem. The ScrolledWindow should expand up to 400px. When I run it self.scroll.GetSize() returns (292, 257) but it clearly is not displaying at that size.
# testing scroll panel for PlotEditFrame
import wx
# spoof the necessary matplotlib objects
class FakePlot:
def __init__(self):
self.figure = FakeFigure()
def get_figure(self):
return self.figure
class FakeFigure:
def __init__(self):
self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
def __init__(self):
self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
def __init__(self,i):
self.label = "line #%s"%i
def get_label(self):
return self.label
class PlotEditFrame(wx.Frame):
"""
This class holds the frame for plot editing tools
"""
def __init__(self, parent, plot):
"""Constructor for PlotEditFrame"""
wx.Frame.__init__(self, parent, -1, "Edit Plot")
self.parent = parent
self.plot = plot
self.figure = plot.get_figure()
self.advanced_options = None
self.scroll = wx.ScrolledWindow(self, -1)
self.InitControls()
def InitControls(self):
"""Create labels and controls based on the figure's attributes"""
# Get current axes labels
self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
wx.StaticText(self.scroll, -1, "Color:"),
wx.StaticText(self.scroll, -1, ""))]
for axis in self.figure.axes:
for line in axis.lines:
color = wx.Colour(255,0,0,0)
lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
lineBtn = wx.Button(self.scroll, -1, size=(25,25))
lineBtn.SetBackgroundColour(color)
self.lineCtrls.append((lineTxt, lineColor, lineBtn))
# Place controls
boxSizer = wx.BoxSizer(wx.VERTICAL)
lineBox = wx.StaticBox(self, -1, "Lines")
lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
for ctrls in self.lineCtrls:
lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
(ctrls[1], 0, wx.ALIGN_LEFT),
(ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
((3,3), 0, wx.ALIGN_CENTER)])
lineSizer.AddGrowableCol(0)
# Set size
self.scroll.SetSizer(lineSizer)
width = self.scroll.GetBestSize().width
height = self.scroll.GetBestSize().height
if height > 400:
height = 400
width = width + 25 # button size
self.scroll.SetSize((width, height))
self.scroll.SetScrollbars(0, 1, 1,1)
print "set scrollbars at %s x %s"%(width, height)
lineBoxSizer.Add(self.scroll, 0, wx.EXPAND)
boxSizer.AddMany([ (lineBoxSizer, 0, wx.EXPAND) ])
self.SetSizer(boxSizer)
self.SetAutoLayout(1)
self.Fit()
height = self.GetSize().GetHeight()
self.SetSizeHints(minH=height, maxH=height,
minW=width, maxW=width*5)
if __name__ == '__main__':
app = wx.PySimpleApp(0)
parent = wx.Frame(None, wx.ID_ANY, 'test', size=(300,300))
plot = FakePlot()
panel = PlotEditFrame(parent, plot)
panel.Show()
app.MainLoop()
I can't figure out what panel needs resized. Some things I've tried, to no avail:
# These have no visible effect
boxSizer.SetMinSize((width, height))
self.scroll.SetVirtualSize((width, height))
lineBoxSizer.Fit(self.scroll)
lineBoxSizer.SetVirtualSizeHints(self.scroll)
# This makes the window the right size, but not the scroll panel
lineBoxSizer.SetMinSize((width, height))
I edited your code a bit to get it to work:
import wx
# spoof the necessary matplotlib objects
class FakePlot:
def __init__(self):
self.figure = FakeFigure()
def get_figure(self):
return self.figure
class FakeFigure:
def __init__(self):
self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
def __init__(self):
self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
def __init__(self,i):
self.label = "line #%s"%i
def get_label(self):
return self.label
class PlotEditFrame(wx.Frame):
"""
This class holds the frame for plot editing tools
"""
def __init__(self, parent, plot, size):
"""Constructor for PlotEditFrame"""
wx.Frame.__init__(self, parent, -1, "Edit Plot", size=size)
self.parent = parent
self.plot = plot
self.figure = plot.get_figure()
self.advanced_options = None
self.scroll = wx.ScrolledWindow(self, -1)
self.InitControls()
def InitControls(self):
"""Create labels and controls based on the figure's attributes"""
# Get current axes labels
self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
wx.StaticText(self.scroll, -1, "Color:"),
wx.StaticText(self.scroll, -1, ""))]
for axis in self.figure.axes:
for line in axis.lines:
color = wx.Colour(255,0,0,0)
lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
lineBtn = wx.Button(self.scroll, -1, size=(25,25))
lineBtn.SetBackgroundColour(color)
self.lineCtrls.append((lineTxt, lineColor, lineBtn))
# Place controls
boxSizer = wx.BoxSizer(wx.VERTICAL)
lineBox = wx.StaticBox(self, -1, "Lines")
lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
for ctrls in self.lineCtrls:
lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
(ctrls[1], 0, wx.ALIGN_LEFT),
(ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
((3,3), 0, wx.ALIGN_CENTER)])
lineSizer.AddGrowableCol(0)
# Set size
self.scroll.SetSizer(lineSizer)
width = self.scroll.GetBestSize().width
height = self.scroll.GetBestSize().height
if height > 400:
height = 400
width = width + 25 # button size
self.scroll.SetSize((width, height))
self.scroll.SetScrollbars(0, 1, 1,1)
print "set scrollbars at %s x %s"%(width, height)
lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)
boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)
self.SetSizer(boxSizer)
self.SetAutoLayout(1)
#self.Fit()
height = self.GetSize().GetHeight()
self.SetSizeHints(minH=height, maxH=height,
minW=width, maxW=width*5)
if __name__ == '__main__':
app = wx.App(False)
plot = FakePlot()
frame = PlotEditFrame(None, plot, size=(300,300))
frame.Show()
app.MainLoop()
The main thing was to set the proportion to "1" on the following two lines:
lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)
boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)
I changed the way you start the program as it's a little silly to put a frame inside another frame for this case. Also PySimpleApp is deprecated, so I changed that too. I have almost never found a good use for the "Fit()" method, so I took that out as it was squashing the initial GUI too much.
Hope that helps!

Scrolling windows with wxPython

I am making a frame with a scrollbar and some images inside. The scrollbar works fine when the frame is empty. However, when I add a picture in, the scrollbars seem to get pushed up into the top left corner of the frame. How can I implement my code so that the scrollbars stay where they are after I add pictures?
Working Code;
import wx
import wx.animate
class ScrollbarFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Scrollbar Example', pos = (100, 50), size=(1000, 1000))
self.scroll = wx.ScrolledWindow(self, -1)
self.scroll.SetScrollbars(1, 1, 1000, 1000)
#self.button = wx.Button(self.scroll, -1, "Scroll Me", pos=(50, 20))
#self.Bind(wx.EVT_BUTTON, self.OnClickTop, self.button)
#self.button2 = wx.Button(self.scroll, -1, "Scroll Back", pos=(500, 350))
#self.Bind(wx.EVT_BUTTON, self.OnClickBottom, self.button2)
self.SetBackgroundColour("gray")
imageName = "01 background.png"
gifName = "Jill.gif"
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
def update(self, imageName, gifName):
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(100, 100))
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
def OnClickTop(self, event):
self.scroll.Scroll(600, 400)
def OnClickBottom(self, event):
self.scroll.Scroll(1, 1)
app = wx.PySimpleApp()
frame = ScrollbarFrame()
frame.Show()
app.MainLoop()
if you comment out this part:
gifName = "Jill.gif"
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
the window displays properly with the scrollbar. But include either (or both) of the image files, and the problem occurs.
If you want your images inside the scrolled window panel, then you have to put your static bipmap and gifImage inside it. So the parent of your images should not be self (the wx.Frame instance) but self.scroll.
Modify the 4 lines indicated:
...................
wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight())) # <- this one
gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(160, 74)) # <- this one
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
def update(self, imageName, gifName):
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight())) # <- this one
gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(100, 100)) # <- this one
...................
This puts your two images one over the other. If you want to put them separately (column or row), then you should add them to a sizer inserted in your scrolled window

multiline checkbox in wxpython

I'm working with wxpython (2.8) with python 2.5.
is it possible to force a wx.CheckBox to display its label on multiple
lines?
I'd like to be able to do the same as wx.StaticText.Wrap(width)
See the attached example: the wx.CheckBox is 200 px wide, but it's
label does not fit in this space.
Any help is really appreciated!
Thanks a lot
Mauro
#example starts here
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Hello World", size=
(300,200))
self.panel = wx.Panel(self, -1)
myVSizer = wx.BoxSizer(wx.VERTICAL)
#instantiating a checkbox 200 px wide. but the label is too
long
cb = wx.CheckBox(self.panel, -1, label="This is a very very
long label for 200 pixel wide cb!", size =wx.Size(200, -1))
myVSizer.Add( cb, 1)
self.panel.SetSizer(myVSizer)
myVSizer.Layout()
app = wx.App(redirect=True)
top = MyFrame()
top.Show()
app.MainLoop()
what about something like this? Flex!
(I've made it a radio button to show that it still behaves like one)
import wx
import textwrap
class MultilineRadioButton(wx.RadioButton):
def __init__(self, parent, id=-1, label=wx.EmptyString, wrap=10, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.RadioButtonNameStr):
wx.RadioButton.__init__(self,parent,id,'',pos,size,style,validator,name)
self._label = label
self._wrap = wrap
lines = self._label.split('\n')
self._wrappedLabel = []
for line in lines:
self._wrappedLabel.extend(textwrap.wrap(line,self._wrap))
self._textHOffset = 20
dc = wx.ClientDC(self)
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
dc.SetFont(font)
maxWidth = 0
totalHeight = 0
lineHeight = 0
for line in self._wrappedLabel:
width, height = dc.GetTextExtent(line)
maxWidth = max(maxWidth,width)
lineHeight = height
totalHeight += lineHeight
self._textHeight = totalHeight
self.SetInitialSize(wx.Size(self._textHOffset + maxWidth,totalHeight))
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.Draw(dc)
self.RefreshRect(wx.Rect(0,0,self._textHOffset,self.GetSize().height))
event.Skip()
def Draw(self, dc):
dc.Clear()
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
dc.SetFont(font)
height = self.GetSize().height
if height > self._textHeight:
offset = height / 2 - self._textHeight / 2
else:
offset = 0
for line in self._wrappedLabel:
width, height = dc.GetTextExtent(line)
dc.DrawText(line,self._textHOffset,offset)
offset += height
class HFrame(wx.Frame):
def __init__(self,pos=wx.DefaultPosition):
wx.Frame.__init__(self,None,title="Hello World",size=wx.Size(600,400),pos=pos)
self.panel = wx.Panel(self,-1)
sizer = wx.BoxSizer(wx.HORIZONTAL)
cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
sizer.Add(cb,1)
cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
sizer.Add(cb,1)
cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
sizer.Add(cb,1)
self.panel.SetSizer(sizer)
sizer.Layout()
class VFrame(wx.Frame):
def __init__(self,pos=wx.DefaultPosition):
wx.Frame.__init__(self,None,title="Hello World",size=wx.Size(600,400),pos=pos)
self.panel = wx.Panel(self,-1)
sizer = wx.BoxSizer(wx.VERTICAL)
cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
sizer.Add(cb,1)
cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
sizer.Add(cb,1)
cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
sizer.Add(cb,1)
self.panel.SetSizer(sizer)
sizer.Layout()
app = wx.App(redirect=False)
htop = HFrame(pos=wx.Point(0,50))
htop.Show()
vtop = VFrame(pos=wx.Point(650,50))
vtop.Show()
app.MainLoop()
Instead of using checkbox with text, use a no label checkbox with static text for desired effect e.g.
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Hello World", size=(300,200))
self.panel = wx.Panel(self, -1)
myVSizer = wx.BoxSizer(wx.VERTICAL)
# use checkbox + static text to wrap the text
myHSizer = wx.BoxSizer(wx.HORIZONTAL)
cb = wx.CheckBox(self.panel, -1, label="")
label = wx.StaticText(self.panel, label="This is a very very long label for 100 pixel wide cb!", size=(100,-1))
label.Wrap(100)
myHSizer.Add(cb, border=5, flag=wx.ALL)
myHSizer.Add(label, border=5, flag=wx.ALL)
myVSizer.Add(myHSizer)
self.panel.SetSizer(myVSizer)
myVSizer.Layout()
app = wx.App(redirect=True)
top = MyFrame()
top.Show()
app.MainLoop()
this has added benefit that with different layouts you can make text centre to checkbox, or on left or right or any other place
Changing your label to
label="This is a very very\n long label for 200\n pixel wide cb!"
should do it.
That is, put in explicit \n characters.

Categories

Resources