0

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!

(https://stackoverflow.com/questions/6129899/python-multiple-frames-with-grid-manager)

import time
import serial
from Tkinter import *

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

class Application(Frame):

    def measure(self):
    ser.write("m")
    data = ser.readline()

    if (data != ""):
        processed_data = data.split(",")
        self.pm1_data.set("PM1: " + str(processed_data[0])) 
        self.pm1.pack() 
        self.pm25_data.set("PM2.5: " + str(processed_data[1])) 
        self.pm25.pack() 
        self.pm10_data.set("PM10: " + str(processed_data[2])) 
        self.pm10.pack() 
        self.temp_data.set("Temperature: " + str(processed_data[3])) 
        self.temperature.pack() 
        self.hum_data.set("Humidity: " + str(processed_data[4])) 
        self.humidity.pack() 
        self.co_data.set("CO: " + str(processed_data[5])) 
        self.co.pack() 
        self.co2_data.set("CO2: " + str(processed_data[6])) 
        self.co2.pack() 
        self.no2_data.set("NO2: " + str(processed_data[7])) 
        self.no2.pack()
        self.so2_data.set("SO2: " + str(processed_data[8]))
        self.so2.pack()          
    # Wait 1 second between each measurement 
    self.after(1000,self.measure)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        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()
root.geometry("320x200")
app = Application(master=root)
app.mainloop()
0

If this all copied correctly

def measure(self):
ser.write("m")
data = ser.readline()

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

should be

def measure(self):
    ser.write("m")
    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

ser.write("m")
time.sleep(.1)#or another value
data = ser.readline()
print(data)#might be good to be able to see the output on the console
  • 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.