The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Update Label
Discuss Update Label in the Python Programming forum on Dev Shed. Update Label Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 13th, 2012, 03:25 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 7
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?
|

December 13th, 2012, 03:41 PM
|
 |
Contributing User
|
|
Join Date: May 2012
Location: 39N 104.28W
Posts: 90
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.
|

December 13th, 2012, 04:02 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 7
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?
|

December 13th, 2012, 04:12 PM
|
 |
Contributing User
|
|
Join Date: May 2012
Location: 39N 104.28W
Posts: 90
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.
|

December 13th, 2012, 07:32 PM
|
|
Contributing User
|
|
Join Date: May 2009
Posts: 294
  
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.
|

December 14th, 2012, 09:16 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 7
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.
|

December 14th, 2012, 10:57 AM
|
 |
Contributing User
|
|
|
|
|
Thanks for the nice information xsilvergs
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|