enzofxxjia
Posts: 4
Joined: Sun Nov 01, 2015 5:05 pm

wxpython StaticText Realtime update

Sun Nov 01, 2015 7:31 pm

Hi Guys
I am total beginner in programming. I already start a small project by using raspberry PI connect to the car OBD port and reading the data to the wxpython GUI. The OBD library I was using from http://brendan-w.com/work/python-obd.
By using the instruction above, I could successful print living rpm data in the python shell line by line.
Code is here:

Code: Select all

import obd
import time 

connection = obd.Async("/dev/rfcomm0") # same constructor as 'obd.OBD()'
cmd = obd.commands.RPM 

connection.watch(cmd) # keep track of the RPM

connection.start() # start the async update loop

while(True):

           response_rpm = connection.query(cmd).value

           print(response_rpm) # non-blocking, returns immediately

           time.sleep(0.01)

#obd.debug.console = True
After this, I created the GUI by using wxformbuilder and change to wxpython code. I test the GUI wxpython code in raspberry PI which was no problem. But after add my OBD library code into it and, the whole frame is not working The thing that I want to do is to using the staticText.SetLabel() to display the living data in a while loop. The Code after add OBD library is here

Code: Select all

# -*- coding: utf-8 -*-
 
###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
 
import wx
import obd
import time
 
 
###########################################################################
## Class MyFrame1
###########################################################################
 
class MyFrame1 ( wx.Frame ):
       
        def __init__( self, parent,id,title ):
                wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 480,320 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
               
                self.SetSizeHintsSz( wx.DefaultSize, wx.Size( 480,320 ) )
                self.SetBackgroundColour( wx.Colour( 0, 0, 0 ) )
               
                gSizer3 = wx.GridSizer( 3, 3, 0, 0 )
               
                self.time_adv_staticText = wx.StaticText( self, wx.ID_ANY, u"time_adv", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
                self.time_adv_staticText.Wrap( -1 )
                self.time_adv_staticText.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), 70, 90, 90, False, "@Arial Unicode MS" ) )
                self.time_adv_staticText.SetForegroundColour( wx.Colour( 0, 255, 0 ) )
               
                gSizer3.Add( self.time_adv_staticText, 0, wx.ALIGN_CENTER, 5 )
               
               
                gSizer3.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
               
                self.MAF_staticText = wx.StaticText( self, wx.ID_ANY, u"MAF", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
                self.MAF_staticText.Wrap( -1 )
                self.MAF_staticText.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), 70, 90, 90, False, "@Arial Unicode MS" ) )
                self.MAF_staticText.SetForegroundColour( wx.Colour( 0, 255, 0 ) )
               
                gSizer3.Add( self.MAF_staticText, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
               
               
                gSizer3.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
               
                self.SPEED_staticText = wx.StaticText( self, wx.ID_ANY, u"Km/h", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
                self.SPEED_staticText.Wrap( -1 )
                self.SPEED_staticText.SetFont( wx.Font( 20, 70, 90, 90, False, "@Arial Unicode MS" ) )
                self.SPEED_staticText.SetForegroundColour( wx.Colour( 0, 255, 0 ) )
                self.SPEED_staticText.SetBackgroundColour( wx.Colour( 0, 0, 0 ) )
               
                gSizer3.Add( self.SPEED_staticText, 0, wx.ALIGN_CENTER|wx.ALL|wx.EXPAND, 5 )
               
               
                gSizer3.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
               
                self.Throttle_staticText = wx.StaticText( self, wx.ID_ANY, u"Throttle", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
                self.Throttle_staticText.Wrap( -1 )
                self.Throttle_staticText.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), 70, 90, 90, False, "@Arial Unicode MS" ) )
                self.Throttle_staticText.SetForegroundColour( wx.Colour( 0, 255, 64 ) )
               
                gSizer3.Add( self.Throttle_staticText, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
               
                self.RPM_staticText = wx.StaticText( self, wx.ID_ANY, u"RPM", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
                self.RPM_staticText.Wrap( -1 )
                self.RPM_staticText.SetFont( wx.Font( 15, 70, 90, 90, False, "@Arial Unicode MS" ) )
                self.RPM_staticText.SetForegroundColour( wx.Colour( 0, 255, 0 ) )
               
                gSizer3.Add( self.RPM_staticText, 0, wx.ALIGN_CENTER|wx.ALL|wx.EXPAND, 5 )
               
                self.Intake_staticText = wx.StaticText( self, wx.ID_ANY, u"Intake", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
                self.Intake_staticText.Wrap( -1 )
                self.Intake_staticText.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), 70, 90, 90, False, "@Arial Unicode MS" ) )
                self.Intake_staticText.SetForegroundColour( wx.Colour( 0, 255, 0 ) )
               
                gSizer3.Add( self.Intake_staticText, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
               
               
                self.SetSizer( gSizer3 )
                self.Layout()
               
                self.Centre( wx.BOTH )
               
   
# Every wxWidgets application must have a class derived from wx.App
class MyApp(wx.App):
 
    # wxWindows calls this method to initialize the application
    def OnInit(self):
 
        # Create an instance of our customized Frame class
        frame = MyFrame1(None, -1, "")
        frame.Show(True)
 
        # Tell wxWindows that this is our main window
        self.SetTopWindow(frame)
        #step up OBD connection and get update information
        connection = obd.Async("/dev/rfcomm0") # same constructor as 'obd.OBD()'
        connection.watch(obd.commands.RPM) # keep track of the RPM
        connection.start() # start the async update loop
       
        while(True):
                   r_rpm=connection.query(obd.commands.RPM).value
                   frame.RPM_staticText.SetLabel(str(r_rpm)+'RPM')#frame is who de shenm shen
                   frame.Show(True)
                   time.sleep(0.001)
       
       
        # Return a success flag
        return True
 
 
 
app = MyApp(0)     # Create an instance of the application class
app.MainLoop()     # Tell it to start processing events





after running this in the raspberry Pi I only got a gray frame and nothing work
Please help me, I don't know how to pursue further from here

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: wxpython StaticText Realtime update

Fri Nov 13, 2015 3:19 pm

I would assume the OnInit method would need to return for the wx framework to continue with the application - now your code enters the infinite while(True) loop and never returns control to the framework.

Graphical user interfaces and tight loops don't go well together. You would need to look at creating a separate thread for the OBD reading code or maybe the wx framework has somekind of timer object that could be used to periodically read...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”