I tried to put values in the grids from a string. But the values are not showing or updating. How do you put serial values(from arduino) to a specific grid in Tkinter? When I run the code, no values were showing only the colored bgs. Thanks!


import time
import serial
from Tkinter import *

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)

class Application(Frame):

    def measure(self):
    data = ser.readline()

    if (data != ""):
        processed_data = data.split(",")
        self.pm1_data.set("PM1: " + str(processed_data[0])) 
        self.pm25_data.set("PM2.5: " + str(processed_data[1])) 
        self.pm10_data.set("PM10: " + str(processed_data[2])) 
        self.temp_data.set("Temperature: " + str(processed_data[3])) 
        self.hum_data.set("Humidity: " + str(processed_data[4])) 
        self.co_data.set("CO: " + str(processed_data[5])) 
        self.co2_data.set("CO2: " + str(processed_data[6])) 
        self.no2_data.set("NO2: " + str(processed_data[7])) 
        self.so2_data.set("SO2: " + str(processed_data[8]))
    # Wait 1 second between each measurement 

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title("Grid Manager")
        self.pm1_data = StringVar()
        self.pm25_data = StringVar()
        self.pm10_data = StringVar()    

        for r in range(6):
            self.master.rowconfigure(r, weight=1)    
        for c in range(5):
            self.master.columnconfigure(c, weight=1)
            Button(master, text="Button {0}".format(c)).grid(row=6,column=c,sticky=E+W)

        Frame1 = Frame(master, bg="red")
        Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S) 
        label = Label(Frame1, textvariable=self.pm1_data).pack()
        label2 = Label(Frame1, textvariabl=self.pm25_data).pack()
        label3 = Label(Frame1, textvariable=self.pm10_data).pack()

        Frame2 = Frame(master, bg="blue")
        Frame2.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
        Frame3 = Frame(master, bg="green")
        Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)

root = Tk()
app = Application(master=root)

If this all copied correctly

def measure(self):
data = ser.readline()

There is nothing in the function measure(self): because its not indented

should be

def measure(self):
    data = ser.readline()
    .....rest of function

Also I am assuming when you write "m" to the arduino that is telling it to send the measurements? You go directly from the write to the read with no delay, this may also cause some issues as it does not give the arduino time to process your request. It may only mean that your readings will be delayed by one cycle, or could cause other problems

time.sleep(.1)#or another value
data = ser.readline()
print(data)#might be good to be able to see the output on the console
| improve this answer | |
  • My apologies. I just copied it wrong. I think I just made a bad formatting in the Frame part of the code. – Pau Feb 13 '18 at 3:13
  • Kinda assumed that. It would not have run the other way. How about the delay or printing the line. Try that? – Chad G Feb 13 '18 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.