Posts: 16
Threads: 6
Joined: Feb 2020
Thanks for continue support
Yes it is working with text but the data coming from serial port does not update the widgets
please see the modified program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import serial
from tkinter import *
from time import sleep
values = []
root = Tk()
root.geometry( '600x200' )
cnt = 0
var = StringVar()
serialArduino = serial.Serial( 'COM3' ,baudrate = 9600 , timeout = 1 )
root.title( "D Cube Serial Read" )
e = Entry(root,width = 35 , textvariable = var ,borderwidth = 5 )
e.grid(row = 0 , column = 0 , columnspan = 3 ,padx = 10 ,pady = 10 )
while True :
valueRead = serialArduino.readline()
print (valueRead);
var. set (valueRead)
root.mainloop()
|
Posts: 12,028
Threads: 485
Joined: Sep 2016
Since I don't have a serial device to hook up to (without a bit or work), please show the output running your program.
also, there is a semicolon after print statement (C habit?)
[inline]print(f"valueRead: {valueRead}")[/inline is better
Posts: 16
Threads: 6
Joined: Feb 2020
Mar-06-2020, 05:15 AM
(This post was last modified: Mar-06-2020, 05:15 AM by PA3040.)
Hi
Thanks for the reply
This is the working code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import serial
from tkinter import *
from time import sleep
values = []
root = Tk()
root.geometry( '600x200' )
cnt = 0
var = StringVar()
serialArduino = serial.Serial( 'COM3' ,baudrate = 9600 , timeout = 1 )
root.title( "D Cube Serial Read" )
e = Entry(root,width = 35 , textvariable = var ,borderwidth = 5 )
e.grid(row = 0 , column = 0 , columnspan = 3 ,padx = 10 ,pady = 10 )
while True :
valueRead = serialArduino.readline()
print (valueRead)
var. set (valueRead)
root.mainloop()
|
You can see the root.mainloop() does not inside the while loop so then the result is showing in the python shell and it does not load the widget and GUI .only reading showing the python shell
1 2 3 4 5 6 7 8 9 10 |
b''
b '11\r\n'
b '12\r\n'
b '13\r\n'
b '14\r\n'
b '15\r\n'
b '16\r\n'
b '17\r\n'
b '18\r\n'
b '19\r\n'
|
Out Put
![[Image: s!AvrJlo2hoIofhhrLrGRj7VtNL32k]](https://1drv.ms/u/s!AvrJlo2hoIofhhrLrGRj7VtNL32k)
but when i put the root.mainloop() to the inside of the while loop as showing following
1 2 3 4 5 6 |
while True :
valueRead = serialArduino.readline()
print (valueRead)
var. set (valueRead)
root.mainloop()
|
Then it will load the GUI but the values read from serial port does not display on widget and in this case the value does not show python shell too
See the image
https://1drv.ms/u/s!AvrJlo2hoIofhhuaViWgHjtNjOVe
Please advice
Thanks in advanced
Posts: 12,028
Threads: 485
Joined: Sep 2016
root.mainloop should only be executed once, after all has been defined.
think of it as a wrapper around the GUI application.
So I don't know what is happening under the covers when you loop a loop
Doesn't sound like a good idea.
Posts: 9
Threads: 3
Joined: Dec 2020
Jan-05-2021, 03:02 AM
(This post was last modified: Jan-05-2021, 03:02 AM by Kurta.)
I didnt mean to hi jack but i have the same issue and the other guy never came back to say if it worked or not.
i tried the code in my example and it didn't work.
this is my code
a simplified version of reading the arduino temp humidity probe. That works
1 2 3 4 5 6 |
import serial
ser = serial.Serial( '/dev/ttyACM0' , baudrate = 9600 , timeout = 1 )
while 1 :
arduinoData = ser.readline().decode( 'ascii' )
print (arduinoData)
|
Output: Temperature: 74.30 *F
Humidity: 58.10 %
this will repeat forever
when i put tkinter in no window pop up
here is the code with tkinter added
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import serial
ser = serial.Serial( '/dev/ttyACM0' , baudrate = 9600 , timeout = 1 )
from tkinter import *
values = []
root = Tk()
root.geometry( '600x200' )
root.title( "Read temp/humidity from arduino" )
while 1 :
arduinoData = ser.readline().decode( 'ascii' )
print (arduinoData)
e = Entry(root,width = 100 , textvariable = arduinoData ,borderwidth = 5 )
e.grid(row = 0 , column = 0 , columnspan = 3 ,padx = 10 ,pady = 10 )
root.mainloop()
|
python editor still shows the temp and humidity but no window pops up any suggestions?
Larz60+ write Jan-04-2021, 01:52 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use code tags in future posts.
Posts: 6,783
Threads: 20
Joined: Feb 2020
1 2 3 4 5 6 7 8 9 10 |
while 1 :
arduinoData = ser.readline().decode( 'ascii' )
print (arduinoData)
e = Entry(root,width = 100 , textvariable = arduinoData ,borderwidth = 5 )
e.grid(row = 0 , column = 0 , columnspan = 3 ,padx = 10 ,pady = 10 )
root.mainloop()
|
You cannot block reading a serial port and have a GUI application. The GUI must be allowed to run. Either you need to do no-blocking serial reads and pool for data, or you need to launch a separate process to read the serial port. Neither is easy. Both types of solutions should be well documented because this is a fairly common problem.
Posts: 4
Threads: 0
Joined: Jan 2021
Not sure why that didn't work
|