Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New python learner
#1
Hello, I jst started learnPython. Have some questions. I have a code like this to create a UI

from tkinter import Tk, BOTH
from tkinter.ttk import Frame, Label, Button, Style

class Example(Frame):

def __init__(self):
super().__init__()

self.initUI()


def initUI(self):

self.style = Style()
self.style.theme_use("default")

self.master.title("Quit button")
self.pack(fill=BOTH, expand=1)

quitButton = Button(self, text="LED",
command=self.quit)
quitButton.place(x=380, y=350)

dataLabel = Label(text="Hello, world!")
dataLabel.place(x=380, y=300)

#read data function
def readData():
ser = serial.Serial('/dev/ttyUSB0', 9600)
while True :
data = ser.readline()
print (str(data))
time.sleep(1)

ser.close()


def main():

root = Tk()
root.geometry("800x400+0+0")
app = Example()
root.mainloop()


if __name__ == '__main__':
main()
This works fine.
I have another code, reads data from serial every second:

import serial, time

ser = serial.Serial('/dev/ttyUSB0', 9600)

while True :
data = ser.readline()
print str(data)
time.sleep(1)
ser.close()
Now I need to update and display data in the label as it receiving new data. How do  that in Python? Thanks.
Reply
#2
You need to fix your indentation.

in the ReadData __init__ method of the Example class, define:
self.data = None
then in the readData method, change:
data=ser.readline()
# to
self.data = ser.readline()
 Now self.data is visible for the entire class
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with kivy for a learner Talbot9 2 1,254 Mar-21-2022, 07:47 AM
Last Post: Talbot9
  new learner Rezvaneh 1 1,847 Sep-17-2020, 01:55 PM
Last Post: buran
  New Python learner looking for key word guidance naga 2 1,722 Nov-20-2019, 01:09 PM
Last Post: ChislaineWijdeven

Forum Jump:

User Panel Messages

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