[PyQt] Qstring and encode

Detlev Offenbach detlev at die-offenbachs.de
Mon Sep 25 17:08:57 BST 2017


Am Montag, 25. September 2017, 07:06:58 CEST schrieb Phil:
> 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-byte
> > s-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 only possibility I see is, that the Python interpreter configured in eric 
is different from the one used by IDLE. 

Nice play with words; together it's "eric idle" :-)

Detlev

> 
> 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.

-- 
Detlev Offenbach
detlev at die-offenbachs.de


More information about the PyQt mailing list