i have written a gui using tkinter and i need to send the values of the 2 scales in realtime to an arduino. i have verified that the arduino is working using another sketch which sends values to the arduino and these are received, i have added in the following code to my python code
while True:
#command = raw_input("Enter level ")
#if command == '1' :
ser.write("c"+str (c1v.get()))
ser.write(":d"+str (c2v.get()))
i have moved this inside and outside of the tkinter mainloop and get varied results from the gui not loading to the data only sending once the gui is closed. can someone tell me how to get the gui to run and as i move a scale the value is sent to the arduino in real time over serial.
heres the code :
from Tkinter import *
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
master= Tk()
master.geometry('500x500+0+0')
def print_value(val):
print ("c1="+str (c1v.get()))
print ("c2="+str(c2v.get()))
c1v=DoubleVar()
c2v=DoubleVar()
c1 = Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value, variable =c1v)
c1.grid(row=1,column=1)
c2 = Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value, variable =c2v)
c2.grid(row=1,column=2)
def load_p1():
pass
lp1 = open("/home/pi/Desktop/IEP/test/preset_test.txt")
val1, val2 = (x.split("=")[1] for x in lp1)
c1.set(val1)
c2.set(val2)
lp1.close()
#
def record():
save_path = '/home/pi/Desktop/IEP/test'
name_of_file = ("preset_test")
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName , "w")
toFile = ("c1="+str (c1.get())+ "
""c2="+str(c2.get()))
file1.write(toFile)
file1.close()
rec=Button(master, text="Record",width=20, height=10, bg='Red', command=record)
rec.grid(row=2, column=1)
load=Button(master, text="Load",width=20, height=10, bg='gold',command=load_p1)
load.grid(row=2, column=2)
master.mainloop()
while True:
#command = raw_input("Enter level ")
#if command == '1' :
ser.write("c"+str (c1v.get()))
ser.write(":d"+str (c2v.get()))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…