Python Forum

Full Version: Entry Widget issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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.
Hi

I tried using set but it does not work
Please show modified code
Thank You
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)
Not sure why that didn't work, perhaps add e.update()
I will update
you need to add the update statement after the set
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")
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()
Pages: 1 2