Python Forum
Entry Widget issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Entry Widget issue (/thread-24513.html)

Pages: 1 2


RE: Entry Widget issue - PA3040 - Mar-04-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

import serial
from tkinter import *
from time import sleep
# import matplotlib.pyplot as plt
# from drawnow import *
  
values = []
root = Tk()
root.geometry('600x200')
# plt.ion()
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)
#var.set('My Var')
root.mainloop()



RE: Entry Widget issue - Larz60+ - Mar-04-2020

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


RE: Entry Widget issue - PA3040 - Mar-06-2020

Hi
Thanks for the reply

This is the working code
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)
    #var.set('My Var')
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

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]
but when i put the root.mainloop() to the inside of the while loop as showing following


while True:
    valueRead = serialArduino.readline()
    print(valueRead)
    var.set(valueRead)
    #var.set('My Var')
    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


RE: Entry Widget issue - Larz60+ - Mar-06-2020

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.


RE: Entry Widget issue - Kurta - Jan-05-2021

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

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

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?


RE: Entry Widget issue - deanhystad - Jan-05-2021

while 1:
    arduinoData = ser.readline().decode('ascii')
    print(arduinoData)

#### NO CODE BELOW THIS LINE EVER RUNS ####
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.


RE: Entry Widget issue - pitterbrayn - Jan-20-2021

Not sure why that didn't work