Thread: Update Label

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Update Label


    Hi, New to Python so please be patient.
    I've written my first program after reading a few online tutorials but have now run into a problem. Having spent the last 2 evening trying to sort it out I'm now asking for help. Here is my code:
    Code:
    from Tkinter import*
    import serial
    
    root=Tk()
    
    var = StringVar()
    var.set("---")
    
    def getdata():  
        rate_str = ''
        ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0.5)
        
        while 1:
            datain = ser.readline()
            if datain:
                if datain == ',':
                    var.set("666")#used for test
                    #var.set = datain
                    print "past var.set" #it passed this way
                else:
                    rate_str += datain
    
    root.after(1000, getdata)
    root.title("Polar HRM")
    hrmLabel=Label(root,textvariable=var,font=("Helvetica",300),width=("3"),background=("White"),fg=("Red")).pack()
    
    root.mainloop()
    The problem:
    When the code runs it creates the Label and fills it with "---".
    When I send a comma I wish it to take the three numbers held in the "var" and update the Label but it doesn't work it just remains with the 3 dashes.
    Can anyone explain and educate me please?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    2
    I think the problem is scope. To do it the way you have it now, you need to make "var" global ("global var") in the function.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by rrashkin
    I think the problem is scope. To do it the way you have it now, you need to make "var" global ("global var") in the function.
    Thanks for reply.
    If you mean like this:
    Code:
    def getdata():
         global var
         rate_str = '' 
         ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0.5)
    It still doesn't work. Have you another suggestion?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    2
    Well, yes I do. I use Tk a lot in Tcl but I have found the textvariable option in Python's Tkinter never works quite right for me (I've never tried it in labels but I gave up on it in entries). I prefer to use the <widget>.config(text="blah") method.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    Sometimes you have to call update_idletasks(). I can never remember the particular circumstances, but try it first when things don't happen as they should. Also, you should sleep during each cycle otherwise this program could hog the computer.
    Code:
    from Tkinter import*
    ##import serial
    import time
    
    root=Tk()
    
    var = StringVar()
    var.set("---")
    
    def getdata():  
        rate_str = ''
    ##    ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0.5)
        datain = ","
        Label(root,textvariable=var,font=("Helvetica",300),width=("3"),background=("White"),fg=("Red")).pack()
        ctr = 0
        while ctr < 50:
            ctr += 1
            time.sleep(0.2)
    ##        datain = ser.readline()
            if datain 
                if datain == ',':
                    var.set(str(ctr))#used for test
                    #var.set = datain
                    root.update_idletasks()
                    print "past var.set" #it passed this way
                else:
                    rate_str += datain
    
    root.after(1000, getdata)
    root.title("Polar HRM")
    
    root.mainloop()
    Last edited by dwblas; December 13th, 2012 at 07:40 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    dwblas,

    Thanks for help. Here is the code so far which updates the Label.
    Code:
    from Tkinter import*
    import serial
    import time
    
    root=Tk()
    var = StringVar()
    
    def getdata():  
        rate_str = ''
        ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0.5)
        Label(root,textvariable=var,font=("Helvetica",300),width=("3"),background=("White"),fg=("Red")).pack()
    
        while TRUE:
            time.sleep(0.2)
            datain = ser.read()             #read 1 byte
            if datain:                      #is there data in the buffer
                if datain == ',':           #if it's a comma print var to screen
                    #print "comma"
                    var.set(str(rate_str))  #value to send to Label
                    root.update_idletasks()
                    rate_str = ''           #clear var
                else:
                    rate_str += datain      #concat values
                    print rate_str
    
    root.after(1000, getdata)
    root.title("Polar HRM")
    
    root.mainloop()
    I want to develop it more so will be back for more help in the future.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Thanks for the nice information xsilvergs
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo