【问题标题】:Updating Static label in wxpython on event trigger在事件触发时更新 wxpython 中的静态标签
【发布时间】:2021-06-03 16:01:01
【问题描述】:

我正在构建一个应用程序,该应用程序将显示有关摩托车的基本信息,例如 RPM 和它所在的档位。假设我们通过“键盘按钮按下”来改变档位,我怎样才能让它更新标签。假设等效键是向上的 UP 键,到目前为止这是我想出的,但是当事件被触发时标签不会更新。我可能做错了什么?

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

【问题讨论】:

    标签: python event-handling wxpython


    【解决方案1】:

    我不相信 Frame 接受字符。
    使用 wx.Window 和样式 wx.WANTS_CHARSwx.Panel

    您想更新self.Gearind 并没有帮助,但您将其定义为local,即Gearind

    下面,我添加了一个面板和一些其他调整(注意仪表值)。

    您可能想调查 wxpython SpeedMeter,它可能会在您的程序中添加一些 Vroooom!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多