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


Entry Widget issue - PA3040 - Feb-17-2020

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


RE: Entry Widget issue - Larz60+ - Feb-18-2020

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.


RE: Entry Widget issue - PA3040 - Feb-19-2020

Hi

I tried using set but it does not work


RE: Entry Widget issue - Larz60+ - Feb-19-2020

Please show modified code
Thank You


RE: Entry Widget issue - PA3040 - Feb-20-2020

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)



RE: Entry Widget issue - Larz60+ - Feb-21-2020

Not sure why that didn't work, perhaps add e.update()


RE: Entry Widget issue - PA3040 - Feb-21-2020

I will update


RE: Entry Widget issue - Larz60+ - Feb-21-2020

you need to add the update statement after the set


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

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")



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

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()