I have a need for a tool to spit out commands to a telnet device that requires none printable chars, such as carriage-returns and line-feeds. In python itself this is achieved with something like the following....
myDevice.send(b'command\n\r')
This would result in 9 bytes being sent to the device.
I would like to enter such a string into a Tkinter entry box. However it seems the Tk entry box is too clever for its own good and escapes the \ so I always get \\ whenever I introduce \r\n or \x0a etc. In other words it is always printable.
I have tried endlessly to render the resultant string into what I need but without joy. I have included a small example below that should illustrate the problem.
from tkinter import *
def show_entry_fields():
print(e1.get().encode())
def show_string():
myString = b'hello\n'
print(myString)
master = Tk()
Label(master, text="Enter string with \\n").grid(row=0)
e1 = Entry(master)
e1.insert(10,"enter with escape char")
e1.grid(row=0, column=2)
Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Show Entry', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Show myString', command=show_string).grid(row=3, column=2, sticky=W, pady=4)
mainloop( )
The following was entered into the tk entry box:
'hello\n'
This is what comes out from a print:
b'hello\\n'
What I actually want to see is this:
b'hello\n'
And once sent to the device becomes just 6 bytes.