I am building an application that will show basic info about a motorcycle such as RPM and the gear it is in. Say we change gear with a "keyboard button press", how could I get it to update the label. Suppose the equivalent key would be the UP key to go up, so far this is what I came up with but the label wont update when the event is being triggered. What may I be doing wrong?
import wx
import sys
var =int(sys.argv[1])
gr = "N"
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((400, 300))
self.gauge_1 = wx.Gauge(self, wx.ID_ANY, 10000, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("Test")
self.gauge_1.SetBackgroundColour(wx.Colour(216, 216, 191))
self.gauge_1.SetForegroundColour(wx.Colour(128, 0, 206))
self.gauge_1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "Ubuntu"))
self.gauge_1.SetValue(var)
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.gauge_1, 0, wx.EXPAND, 0)
label_1 = wx.StaticText(self, wx.ID_ANY, "GEAR")
label_1.SetMinSize((100, 50))
label_1.SetForegroundColour(wx.Colour(0, 137, 215))
label_1.SetFont(wx.Font(25, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_ITALIC, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(label_1, 0, wx.ALL, 3)
Gearind = wx.StaticText(self, wx.ID_ANY, gr, style=wx.ALIGN_CENTER)
Gearind.SetMinSize((50, 43))
Gearind.SetForegroundColour(wx.Colour(122, 0, 7))
Gearind.SetFont(wx.Font(32, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(Gearind, 0, 0, 0)
self.SetSizer(sizer_1)
self.Layout()
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
if code == wx.WXK_UP:
gr = "1"
self.Gearind.SetLabel(gr)
elif code == wx.WXK_DOWN:
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
self.Test = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.Test)
self.Test.Show()
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
I don't believe a Frame
accepts characters.
Use a wx.Window
with style wx.WANTS_CHARS
or a wx.Panel
It won't help that you want to update self.Gearind
but you defined it as a local
ie Gearind
.
Below, I've added a panel and a few other tweaks (note the gauge value).
You might want to investigate the wxpython SpeedMeter
which may add some Vroooom!
to your program.
import wx
import sys
try:
var =int(sys.argv[1])
except Exception:
var = 0
gr = "N"
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.ID_ANY)
self.SetSize((400, 300))
self.panel = wx.Panel(self, wx.ID_ANY)
self.gauge_1 = wx.Gauge(self.panel, wx.ID_ANY, 6, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)
self.panel.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("Test")
#self.gauge_1.SetBackgroundColour(wx.Colour(216, 216, 191))
#self.gauge_1.SetForegroundColour(wx.Colour(128, 0, 206))
#self.gauge_1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "Ubuntu"))
self.gauge_1.SetValue(var)
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.gauge_1, 0, wx.EXPAND, 0)
label_1 = wx.StaticText(self.panel, wx.ID_ANY, "GEAR")
label_1.SetMinSize((100, 50))
label_1.SetForegroundColour(wx.Colour(0, 137, 215))
label_1.SetFont(wx.Font(25, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_ITALIC, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(label_1, 0, wx.ALL, 3)
self.Gearind = wx.StaticText(self.panel, wx.ID_ANY, gr, style=wx.ALIGN_CENTER)
self.Gearind.SetMinSize((50, 43))
self.Gearind.SetForegroundColour(wx.Colour(122, 0, 7))
self.Gearind.SetFont(wx.Font(32, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(self.Gearind, 0, 0, 0)
self.panel.SetSizer(sizer_1)
self.Layout()
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
gr = self.Gearind.GetLabel()
if gr.isnumeric():
gr_up = int(gr) + 1
gr_down = int(gr) - 1
else:
gr_up = 1
gr_down = 0
if gr_up > 6:
gr_up = 6
if gr_down < 1:
gr_down = "N"
if code == wx.WXK_UP:
self.Gearind.SetLabel(str(gr_up))
elif code == wx.WXK_DOWN:
self.Gearind.SetLabel(str(gr_down))
gr = self.Gearind.GetLabel()
if gr == "N":
var = 0
else:
var = int(gr)
self.gauge_1.SetValue(var)
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
self.Test = MyFrame(None)
self.SetTopWindow(self.Test)
self.Test.Show()
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.