Is it possible to put a label on wxPython gizmos ledctrl? - python

I would like to put a label on wxPython gizmos led, like this.
But I can't find how to make a label on the led.
My code is like this. I used wxPython StaticText to make the led label, and adjusted the position.
import wx.lib.gizmos.ledctrl as led
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, title= "LED Test", size = (300,200))
self.panel = wx.Panel(self)
self.myLed = led.LEDNumberCtrl(self.panel, -1, size = (100, 30), pos = (100,50))
self.label = wx.StaticText(self.panel, -1, "my LED" , pos=(130,57))
self.myLed.SetBackgroundColour("green")
self.label.SetBackgroundColour("green")
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()
How can I make a gizmos led label?

In short, the answer is no, as the question is written.
It is limited to the characters that can be displayed.
The LEDNumberCtrl can be used to display a series of digits, (plus spaces, colons or dashes,) using a style reminiscent of old-timey segmented digital displays.
import wx.lib.gizmos.ledctrl as led
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, title= "LED Test", size = (300,200))
self.panel = wx.Panel(self)
self.myLed = led.LEDNumberCtrl(self.panel, -1, size = (200,50), pos = (50,50))
self.myLed.SetValue(" 22:01")
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()

Related

Dropdown of wxPython combobox doesn't work on popup window

When I put a combo box on wxPython popup window, dropdown function doesn't work.
My example code is this.
import wx
class TestPopup(wx.PopupWindow):
def __init__(self, parent):
"""Constructor"""
wx.PopupWindow.__init__(self, parent = parent)
self.popUp = wx.Panel(self, size = (200,200))
self.popUp.SetBackgroundColour("white")
self.st = wx.StaticText(self.popUp, -1, " Select Comport", pos=(10,10))
self.selCom = wx.ComboBox(self.popUp, -1, pos=(85, 50), choices=["Com1", "Com2"])
self.mySizer = wx.BoxSizer(wx.VERTICAL)
self.mySizer.Add(self.popUp)
self.SetSizerAndFit(self.mySizer)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, title= "ComboBox Test", size = (300,200))
self.panel = wx.Panel(self)
self.selComButton = wx.Button(self.panel, -1, "Select Comport")
self.selComButton.Bind(wx.EVT_BUTTON, self.selectPopUp)
self.selCom = wx.ComboBox(self.panel, -1, pos = (85, 50),choices=["Com1", "Com2"])
def selectPopUp(self, event):
win = TestPopup(self.GetTopLevelParent())
btn = event.GetEventObject()
pos = btn.ClientToScreen((0, 0))
sz = btn.GetSize()
win.Position(pos, (0, sz[1]))
win.Show(True)
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()
In the code, combo box in main frame works well. But, in the popup window, which is shown when 'select Comport' button is clicked, combobox doesn't work.
What's wrong with this?
It works well.
It doesn't work.
The ComboBox certainly works in a popup window under Linux, so it's difficult to address your question directly. However, I would suggest that in this case, you might well be better served, if you were to use a Dialog rather than a PopUpWindow, as it will do the heavy lifting for you.
For example:
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, title= "Communication Port", size = (300,200))
self.panel = wx.Panel(self)
self.selComButton = wx.Button(self.panel, -1, "Select Comport")
self.selComButton.SetToolTip("Select Comport")
self.selComButton.Bind(wx.EVT_BUTTON, self.selectPopUp)
def selectPopUp(self, event):
dlg = wx.SingleChoiceDialog(None,"Pick a com port", "Com ports",["Com1","Com2","Com3","Com4"],wx.CHOICEDLG_STYLE)
if dlg.ShowModal() == wx.ID_OK:
res = dlg.GetStringSelection()
self.selComButton.SetLabel(res)
dlg.Destroy()
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()

How to change wxpython gizmo LED color when button is clicked

I want to change the colour of a wx gizmos led when the wx button is clicked.
My example is as follows.
import wx
import wx.lib.gizmos.ledctrl as led
class Main(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, title ="LED test")
panel = wx.Panel(self)
self.myLED = led.LEDNumberCtrl(panel, -1, pos = (150,50), size = (100,100))
self.myLED.SetBackgroundColour("gray")
self.myButton = wx.Button(panel, -1, "myButton", pos =(50, 50))
self.myButton.Bind(wx.EVT_BUTTON, self.changeLEDColor)
def changeLEDColor(self,event):
self.myLED.SetBackgroundColour("green")
if __name__ == "__main__":
app = wx.App()
frame = Main()
frame.Show()
app.MainLoop()
I expected the led colour to change to 'green', when I click 'mybutton', but it is still 'gray'.
What's wrong with my example?
Adding self.Refresh() or self.myLED.Refresh() will trigger the repaint. Here's the link to the docs. If it flickers, look into wx.Frame.SetDoubleBuffered(True) - docs

wxPython return value from Wizard to calling Frame

My problem is as follows:
I am designing a wizard for the construction of an object to be added to a list of objects in the calling frame of my program. At the end of the wizard I would like to pass the newly created object back to the calling frame to be inserted into the list. In order to simulate this basic functionality on an abstract basis I have constructed the following, scaled down app:
mainframe.py
import wx
import wiz_test
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,title="Main")
panel = wx.Panel(self)
callButton = wx.Button(panel, label = "Call Wizard")
callButton.Bind(wx.EVT_BUTTON,self.launchWizard)
self.Show()
def launchWizard(self,event):
wiz = wiz_test.WizObj(self)
a = 0
if wiz == wx.wizard.EVT_WIZARD_FINISHED:
a = wiz.answer
print a
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
wiz_test.py
import wx
import wx.wizard as wiz
class WizPage(wiz.WizardPageSimple):
def __init__(self, parent):
self.answer = 3
wiz.WizardPageSimple.__init__(self, parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
title = wx.StaticText(self, -1, "Wizard Page")
title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
sizer.Add(title, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
class WizObj(object):
def __init__(self,parent):
wizard = wx.wizard.Wizard(None, -1, "Simple Wizard")
page1 = WizPage(wizard)
wizard.FitToPage(page1)
wizard.RunWizard(page1)
wizard.Destroy()
if __name__ == "__main__":
app = wx.App(False)
main()
app.MainLoop()
The ultimate goal in this small example is to get the MainFrame instance to output the value '3' derived from the .answer member variable of the WizObj instance when the wx.wizard.EVT_WIZARD_FINISHED event is triggered. However it is clearly not working at this point as the current code only returns '0'. Am I approaching this the correct way? Should I be binding the EVT_WIZARD_FINISHED event instead, and if so, how would I access that from Mainframe?
I was able to solve this problem through the use of the "pubsub" capability within the wxPython library. Specifically, I added a pub.subscribe() instance immediately prior to the instantiation of my wizard within the calling frame. Inside of the wizard I pass the value via pub.sendMessage() just before destroying the wizard. It is important to note that the pass value had to be specified in order for the pubsub send would work effectively.
The following code is the modified version of the original code which now functions.
MainFrame.py
import wx
import wiz_test
from wx.lib.pubsub import pub
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,title="Main")
panel = wx.Panel(self)
callButton = wx.Button(panel, label = "Call Wizard")
callButton.Bind(wx.EVT_BUTTON,self.launchWizard)
self.Show()
def catch_stuff(self,a):
print a
def launchWizard(self,event):
pub.subscribe(self.catch_stuff,'valPass')
wiz = wiz_test.WizObj(self,a)
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
wiz_test.py
import wx
import wx.wizard as wiz
from wx.lib.pubsub import pub
class WizPage(wiz.WizardPageSimple):
def __init__(self, parent):
self.answer = 3
wiz.WizardPageSimple.__init__(self, parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
title = wx.StaticText(self, -1, "Wizard Page")
title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
sizer.Add(title, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
#----------------------------------------------------------------------
class WizObj(object):
def __init__(self,parent,a):
wizard = wx.wizard.Wizard(None, -1, "Simple Wizard")
page1 = WizPage(wizard)
wizard.FitToPage(page1)
wizard.RunWizard(page1)
pub.sendMessage('valPass',a = page1.answer)
wizard.Destroy()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
main()
app.MainLoop()
The result is that the console prints the value 3 which was retrieved from the called wizard.

wxPython. How do I center a static size panel?

I know this is a very simple question but I've spent hours trying to figure out a solution. I want my panel to be a 50x50 blue box that doesn't change size, and stays centered in the window. I've tried having an inner and outer sizer that are HORIZONTAL and VERTICAL respectively and it seems to only recognize the outer sizer. I'd greatly appreciate it if anyone could help me out.
import wx
class Main(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Main',size=(700,500))
self.SetBackgroundColour('white')
panel = wx.Panel(self,wx.ID_ANY,size=(50,50))
panel.SetBackgroundColour('blue')
sizer=wx.BoxSizer()
sizer.Add(panel,0,wx.ALIGN_CENTER|wx.ALL)
self.SetSizer(sizer)
if __name__=='__main__':
app=wx.App()
frame=Main(parent=None, id=-1)
frame.Show()
app.MainLoop()
Adding a stretchspacer on either side of the panel will do it
import wx
class Main(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Main', size=(700, 500))
self.SetBackgroundColour('white')
panel = wx.Panel(self, wx.ID_ANY, size=(50, 50))
panel.SetBackgroundColour('blue')
sizer = wx.BoxSizer()
sizer.AddStretchSpacer(1)
sizer.Add(panel, 0, wx.ALIGN_CENTER)
sizer.AddStretchSpacer(1)
self.SetSizer(sizer)
if __name__ == '__main__':
app = wx.App()
frame = Main(parent=None, id=-1)
frame.Show()
app.MainLoop()

How to make a canvas (rectangle) in wxpython?

I am trying to make a windows canvas type rectangle, here is an image if you are having issues understanding what i mean,
Is there any way todo this in wxpython? ( also, is there a way to set it to automatically adjust to the window width -20px?, so a radius around the window, and will adjust to the users window size.
EDIT: I asked on the wxPython IRC channel and a fellow named "r4z" came up with the following edit to my code which worked for me on Windows 7.
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
#----------------------------------------------------------------------
def OnPaint(self, event):
""""""
pdc = wx.PaintDC(self)
try:
dc = wx.GCDC(pdc)
except:
dc = pdc
w, h = self.GetSizeTuple()
w = w - 10
h = h - 10
dc.Clear()
dc.DrawRectangle(x=5, y=5, width=w, height=h)
#----------------------------------------------------------------------
def OnSize(event):
event.EventObject.Refresh()
event.Skip()
if __name__ == "__main__":
app = wx.App(False)
frame = wx.Frame(None, title="Test")
panel = MyPanel(frame)
frame.Bind(wx.EVT_SIZE, OnSize)
frame.Show()
app.MainLoop()
Alternately, you might look at the wx.StaticBox widget.
EDIT #2: You could also just set the frame's style like this and skip the whole OnSize business:
frame = wx.Frame(None, title="Test", style=wx.DEFAULT_FRAME_STYLE|wx.FULL_REPAINT_ON_RESIZE)

Categories