Change background color of a Canvas ScaledText - python

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.

Related

PyQt5 resize only one child widget when window is resized

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()

Adding a gridded subplot to WxPython GUI (Matplotlib)

I'm developing a simple demonstration GUI for a multi-lateral ultrasonic rangefinder. A device captures ultrasonic signals from several different sensors, performs time of flight processing on these signals, then uses the range information of detected targets to localize an object in 2D cartesian coordinates.
I'd like to create a wxPython GUI, which uses matplotlib to display each of the sensors signals and plot the target locations. I've found some example code which has roughly the same layout that I'd like to have, and have removed some of the code's unnecessary components. The python code is below:
import sys,os,csv
import numpy as N
import wx
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
class FinalProject(wx.Frame):
title = ' Ultrasound Demo '
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
self.create_menu()
self.create_status_bar()
self.create_main_panel()
def create_menu(self):
self.menubar = wx.MenuBar()
menu_file = wx.Menu()
m_expt = menu_file.Append(-1, "&Save plot\tCtrl-S", "Save plot to file")
self.Bind(wx.EVT_MENU, self.on_save_plot, m_expt)
menu_file.AppendSeparator()
m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")
self.Bind(wx.EVT_MENU, self.on_exit, m_exit)
self.menubar.Append(menu_file, "&File")
self.SetMenuBar(self.menubar)
def create_main_panel(self):
self.panel = wx.Panel(self)
self.dpi = 100
self.fig = Figure((9.5, 5.0), dpi=self.dpi)
self.canvas = FigCanvas(self.panel, -1, self.fig)
# self.axes1 = self.fig.add_subplot2grid((2,2), (0,0))
self.axes1 = self.fig.add_subplot(2,1,1)
# self.axes2 = self.fig.add_subplot2grid((2,2), (1,0))
self.axes2 = self.fig.add_subplot(2,1,2)
self.toolbar = NavigationToolbar(self.canvas)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.vbox.AddSpacer(10)
self.hbox = wx.BoxSizer(wx.HORIZONTAL)
flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def create_status_bar(self):
self.statusbar = self.CreateStatusBar()
def on_draw_button1(self, event):
self.axes1.clear()
self.axes2.clear()
i = N.arange(0,4,1)
q = i
w = N.arange(-4,0,1)
self.axes1.plot(q,i,'red')
self.axes2.plot(w,i,'yellow')
self.canvas.draw()
def on_draw_button2(self, event):
self.axes1.clear()
self.axes2.clear()
a = [0,1,2,3,4,]
b = [5.5,4.5,3.5,2.5,1.5]
c = [7.5,2.5,4,6.8,10.6]
self.axes1.plot(b,a,'purple')
self.axes2.plot(c,a,'black')
self.canvas.draw()
def on_save_plot(self, event):
file_choices = "PNG (*.png)|*.png"
dlg = wx.FileDialog(
self,
message="Save plot as...",
defaultDir=os.getcwd(),
defaultFile="plot.png",
wildcard=file_choices,
style=wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.canvas.print_figure(path, dpi=self.dpi)
self.flash_status_message("Saved to %s" % path)
def on_exit(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = FinalProject()
app.frame.Show()
app.MainLoop()
del app
So, this code would be useful for displaying two captured signals, and it's relatively easy to add additional subplots, provided they exist on a symmetrical grid, but what I would like to do is add a third subplot to the right of these two plots which spans the height of both.
To be clear what I would like to do is add a third subplot to the right of the existing plots the height of which is equal to the current canvas height.
Reading through the matplotlib documentation it looks like the appropriate function to do this would be plt.subplot2grid(). Unfortunately I get errors when I attempt to use this function to define the current subplots (the code I attempted to use is commented out below:
self.axes1 = self.fig.add_subplot2grid((2,2), (0,0)))
Has anyone else attempted this? Any ideas on where I might be going wrong?
Thanks!
Sounds like you have already figured out something to do what you wanted.
To be explicit, there is nothing specific about the fact that you are using WX python in this example - essentially the question boils down to how one would go about adding an axes to a figure which has the same height as two other axes.
This can be done very simply with:
import matplotlib.pyplot as plt
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 3)
ax3 = plt.subplot(1, 2, 2)
plt.show()
HTH

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