Python Forum
Entry Widget issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Entry Widget issue
#1
Hi Team
My requirement is read data from arduino through serial port
and put it to the Entry widget

Every this is successful but only issue is The Entry Widget does not open but I can oen it with combine plot widget

Please advice to open Entry widget without plot widget

import serial
from tkinter import *
import matplotlib.pyplot as plt
from drawnow import *

values = []
root = Tk()

plt.ion()
cnt=0

#serialArduino = serial.Serial('/dev/ttyACM0', 19200)
serialArduino = serial.Serial('COM3',baudrate = 9600, timeout = 1)
root.title("D Cube Serial Read")
e = Entry(root,width = 35, borderwidth = 5)
e.grid(row = 0, column = 0, columnspan = 3,padx = 10,pady = 10)
def plotValues():
    plt.title('Serial value from Arduino')
    plt.grid(True)
    plt.ylabel('Values')
    plt.plot(values, 'rx-', label='values')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,26):
    values.append(0)
    
while True:
    while (serialArduino.inWaiting()==0):
        pass
    valueRead = serialArduino.readline()
    print(valueRead)

    #check if valid value can be casted
    try:
        valueInInt = int(valueRead)
        print(valueInInt)
        
        if valueInInt <= 1024:
            if valueInInt >= 0:
                e.delete(0,END)
                e.insert(1,valueInInt)
                values.append(valueInInt)
                values.pop(0)
                drawnow(plotValues)
                
            else:
                print ("Invalid! negative number")
        else:
            print ("Invalid! too large")
    except ValueError:
        print ("Invalid! cannot cast")
Thanks in advanced
Reply
#2
when creating your widget, add a textvariable
var = StringVar()
e = Entry(root,width = 35, textvariable = var, borderwidth = 5
then you can set the value of the text with
var.set('mytext')

I'm pretty sure that will update the widget as soon as set.
Reply
#3
Hi

I tried using set but it does not work
Reply
#4
Please show modified code
Thank You
Reply
#5
Sorry I could not update the modified code

import serial
from tkinter import *
import matplotlib.pyplot as plt
from drawnow import *

values = []
root = Tk()
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)

def plotValues():
     plt.legend(loc='upper right')
    
while True:
    while (serialArduino.inWaiting()==0):
        pass
    valueRead = serialArduino.readline()
    print(valueRead)

    #check if valid value can be casted
    try:
        valueInInt = int(valueRead)
        print(valueInInt)
        e.delete(0,END)
        #e.insert(1,valueInInt)
        var.set(valueInInt)
        #drawnow(plotValues)
                
           
    except ValueError:
        print ("Invalid! cannot cast")
as you can see the I commented the following line then widgets does not open but on the python shell values are displayed

#drawnow(plotValues)
Reply
#6
Not sure why that didn't work, perhaps add e.update()
Reply
#7
I will update
Reply
#8
you need to add the update statement after the set
Reply
#9
I did it sorry, It is still same
import serial
from tkinter import *
import matplotlib.pyplot as plt
from drawnow import *

values = []
root = Tk()
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)

def plotValues():
     plt.legend(loc='upper right')
    
while True:
    while (serialArduino.inWaiting()==0):
        pass
    valueRead = serialArduino.readline()
    print(valueRead)

    #check if valid value can be casted
    try:
        valueInInt = int(valueRead)
        print(valueInInt)
        e.delete(0,END)
        #e.insert(1,valueInInt)
        var.set(valueInInt)
        e.update()
        #drawnow(plotValues)
                
           
    except ValueError:
        print ("Invalid! cannot cast")
Reply
#10
I striped your program down, and tried the StringVar, which works
Note that I added a root.mainloop as I didn't see one in your
I also added a geometry statement to give window some size
# 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)
 
var.set('My Var')
root.mainloop()
Reply


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