Python Forum
Entry Widget issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Entry Widget issue
#11
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()
Reply
#12
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
Reply
#13
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
Reply
#14
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.
Reply
#15
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?
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.
Reply
#16
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.
Reply
#17
Not sure why that didn't work
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 500 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,439 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,880 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] canvas widget scroll issue chrisdb 2 3,784 Apr-07-2021, 05:48 AM
Last Post: chrisdb
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,215 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  [Tkinter] password with Entry widget TAREKYANGUI 9 5,778 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,296 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  Date entry in box format issue PeroPuri 6 8,142 Apr-25-2020, 11:03 PM
Last Post: PeroPuri
  How to retreive the grid location of an Entry widget kenwatts275 7 4,482 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  POPUP on widget Entry taratata2020 4 3,678 Mar-10-2020, 05:04 PM
Last Post: taratata2020

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020