Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2012, 03:25 PM
xsilvergs xsilvergs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 xsilvergs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 48 sec
Reputation 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?

Reply With Quote
  #2  
Old December 13th, 2012, 03:41 PM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 41 m 29 sec
Reputation 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.

Reply With Quote
  #3  
Old December 13th, 2012, 04:02 PM
xsilvergs xsilvergs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 xsilvergs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 48 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old December 13th, 2012, 04:12 PM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 41 m 29 sec
Reputation 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.

Reply With Quote
  #5  
Old December 13th, 2012, 07:32 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 294 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 53 m 55 sec
Reputation Power: 7
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.

Reply With Quote
  #6  
Old December 14th, 2012, 09:16 AM
xsilvergs xsilvergs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 xsilvergs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 48 sec
Reputation 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.

Reply With Quote
  #7  
Old December 14th, 2012, 10:57 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
Thanks for the nice information xsilvergs
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Update Label

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap