Henkel
Posts: 3
Joined: Fri Jul 05, 2019 2:29 am

label with textvariable doesn't refresh (tkinter)

Fri Jul 05, 2019 11:33 am

Hi guys and girls,

I got an Arduino Uno connected to a Raspberry Pi 3 B+.
The Arduino is writing all the time a random number in form of "0.00".
I wonna read that number on my Raspberry to print it on a label.

The code bellow is working so far but it doesn't refresh it's text.

I am very new to Python, Raspberry and Arduino so I am sorry for basic mistakes.

Code: Select all

import serial
import time

from tkinter import *

HEIGHT = 500
WIDTH = 600

root = Tk()
 
s = serial.Serial('/dev/ttyUSB0', 9600) # Namen ggf. anpassen
s.isOpen()
time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung, daher muss kurz gewartet werden
    
def Main():

    var = StringVar()

    battery_frame = Frame(root, bg = '#80c1ff', bd = 5)
    battery_frame.place(relwidth= 1, relheight=1)
    
    getValue(var)
    
    battery_voltage= Label(battery_frame, textvariable = var)
    battery_voltage.place(relwidth=1, relheight=1)

    root.mainloop()

def getValue(var):
    
    InputValue = s.readline()
    var.set(InputValue)
    return var
    
Main()
I hope u can help me, a big thanks for ur effort. :D

User avatar
MrYsLab
Posts: 353
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: label with textvariable doesn't refresh (tkinter)

Fri Jul 05, 2019 1:06 pm

I modified your code to just run the tkinter elements and it seems to work without issue.
If you print out the value of InputValue in your getValue function after the readline, what do you get?

Henkel
Posts: 3
Joined: Fri Jul 05, 2019 2:29 am

Re: label with textvariable doesn't refresh (tkinter)

Fri Jul 05, 2019 1:23 pm

MrYsLab wrote:
Fri Jul 05, 2019 1:06 pm
I modified your code to just run the tkinter elements and it seems to work without issue.
If you print out the value of InputValue in your getValue function after the readline, what do you get?
Hi MrYsLab,

thanks for ur answer.

When I run my programm the first time I get one number.
But the programm isn't refreshing so it stays all the time on the same number.

Henkel
Posts: 3
Joined: Fri Jul 05, 2019 2:29 am

Re: label with textvariable doesn't refresh (tkinter)

Fri Jul 05, 2019 2:04 pm

I got a solution for my problem.
The Text(box) is now refreshing.

I wrote

Code: Select all

root.mainloop()
to the last row of the code. (From row 27)

I added

Code: Select all

root.after(1000, Main)
to the last row of def Main():

ghp
Posts: 1393
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: label with textvariable doesn't refresh (tkinter)

Fri Jul 05, 2019 4:54 pm

Hello as always there are alternate solutions.
Let me propose some code using a thread to read values from arduino serial line. In the thread, the StringVar is set and the window is updating.
A slight advantage over the root.after(1000, Main)-solution is that the thread is independent from the update frequency of the arduino, at least if not too fast.

Code: Select all

import serial
import time
import threading

from tkinter import *

use_serial = True # set to False when simulation code is used.

HEIGHT = 500
WIDTH = 600

root = Tk()
 
if  use_serial: 
    s = serial.Serial('/dev/ttyUSB0', 9600) # Namen ggf. anpassen
    s.isOpen()
    time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung, daher muss kurz gewartet werden
    

var = StringVar()

def Main():
    battery_frame = Frame(root, bg = '#80c1ff', bd = 5)
    battery_frame.place(relwidth= 1, relheight=1)
        
    battery_voltage= Label(battery_frame, textvariable = var)
    battery_voltage.place(relwidth=1, relheight=1)

    root.mainloop()

i = 0
def getValue():
    while True:
        if use_serial:
            InputValue = s.readline()
            var.set(InputValue)
        else:
            # some simulation of an input
            global i
            InputValue = "{v:6.2f}".format(v=i*3.1415)
            i+= 1
            var.set(InputValue)
            time.sleep(0.2)
  
readThread = threading.Thread(target=getValue)
readThread.start()

Main()

Return to “Python”