Python Forum
[PyGUI] Byte to String Conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Byte to String Conversion
#1
I want to show COM PORT received count number into a text widget.
com port is receiving bytes from micro controller and those bytes are not acceptable by a text widget because the text widget accepts string only.

So please help me to show the count values in a text widget or at any compatible widget.

Issue inside the While*************************



import serial
from guizero import App, Text , TextBox, PushButton, Slider, Picture, Combo,info #imported the app class
ComPort = serial.Serial('COM3') # open COM3
ComPort.baudrate = 9600 # set Baud rate
ComPort.bytesize = 8 # Number of data bits = 8
ComPort.parity = 'N' # No parity
ComPort.stopbits = 1 # Number of Stop bits = 1

app = App(title="IDS",width=700, height =600, layout="grid") #name of the app is "IDS"
welcome_message = Text(app, text="WELCOME TO ITEM DISPATCHER", size=20, font="Times New Roman", color="blue",grid=[5,2],align="left") #or you can use hex code of colour (#ff0000)



item_choice= Combo(app, options=["ata","dal", "suzi", "chini","surf"], grid=[4,4], align="left")
count_item=Combo(app, options=[1,2,3,4,5,6,7,8,9],grid=[15,4], align="left")
item_description= Text(app, text="Chose Item", grid=[2,4], align="left")
count_description=Text(app, text="Count",grid=[8,4], align="left")
#p=48

#print(p)
#print(type(p))
def S_Com():
if item_choice.value == "ata":
j=0x01
ComPort.write(bytes((j,)))
if item_choice.value == "dal":
j=0x02
ComPort.write(bytes((j,)))
if item_choice.value == "suzi":
j=0x03
ComPort.write(bytes((j,)))
if item_choice.value == "chini":
j=0x04
ComPort.write(bytes((j,)))
if item_choice.value == "surf":
j=0x05
ComPort.write(bytes((j,)))

#print(item_choice.value)
count=int(count_item.value)
count=bytes([count])
Read_Com=ComPort.read(size=1)
while Read_Com!=count:
Read_Com=ComPort.read(size=1)
#print(type(Read_Com))
#print(Read_Com)
r=Read_Com.decode('utf-8')
print®
print(type®)
Dropped_Items_Count = Text(app, text=r, size=20, font="Times New Roman", color="blue",grid=[3,7],align="left")

ComPort.write(bytes((0x00,)))
info("Ordered","Your ITEM has Dispatched")



Run_Comport=PushButton(app, command=S_Com, text="Place Order",grid=[5,5], align="top")
Run_Comport.bg="blue"
#repeat(1000,check)
app.display() #Display the app
Reply
#2
Quote:So please help me to show the count values in a text widget or at any compatible widget
Use insert to put something in a text widget, i.e text.insert(END, contents) But this will not work in the code you posted because all of the returns from creating a Text widget are None because that is what is returned by grid(). So next you will get a 'NoneType' object has no attribute... https://code.i-harness.com/en/q/10cfb6
Reply


Forum Jump:

User Panel Messages

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