[PyQt] Qstring and encode

Phil phil_lor at bigpond.com
Mon Sep 25 06:06:58 BST 2017


On 25/09/17 11:59, Kyle Altendorf wrote:
> Python 3 does have bytes.  Note that in Python 2 str is a byte sequence.
> 
> https://docs.python.org/3/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview
> 
> But what version of Python and PyQt are you working with? And what did you try and what was the full traceback?

Thank you again for your reply Kyle.

I'm using python version 3.5.3 and a qt4 Eric project.

The following code fails under python's IDLE, yet works from the Eric 
IDE. Under Eric "mytext" comes from a LineEdit box as explained in my 
previous message.

mytext = "Fred"
byte = bytes(mytext)
byte = byte + '\n'
#ser.write(b mytext)
ser.write(byte)


Traceback (most recent call last):
   File "/home/phil/Python/serial_test.py", line 11, in <module>
     byte = bytes(mytext)
TypeError: string argument without an encoding


This block of code also works under Eric but fails from the command line 
because QString does not have an encode attribute.

mytext = mytext + "\n"
mybytes = mytext.encode('ascii')#'utf-8')
ser.write(mybytes)

I suppose I could have two versions but the same code for the GUI and 
the command line would be better.

-- 
Regards,
Phil


More information about the PyQt mailing list