Search code examples
pythonstringpython-3.xtkintertkinter-entry

Need to display \r\n in entry box Python Tkinter


I have a program where i need to display in an entry box the \r\n characters. However in this entry box in tkinter they don't show up. When I send and return over serial, the \r\n shows up on what is returned so the characters are there, they're just not being displayed.

Is there any way to get these characters to show up in the entry box without having to manually enter them each time.

Here is the code

from tkinter import *


title_str = "Entry Boxes Test"

root = Tk()
root.title(title_str)

f1_value = StringVar()
f1_value.set("=> HELP\r\n")
f2_value = StringVar()
f2_value.set("=> VERSION\r\n")


def callback_f1():
    listbox.insert(END,  f1_entry.get())
    listbox.see(END)
    return


def callback_f2():
    listbox.insert(END,  f2_entry.get())
    listbox.see(END)
    return

title_frame = Frame(root)
title_frame.pack(side=TOP)

body_frame = Frame(root)
body_frame.pack(side=TOP)

left = Frame(body_frame)
right = Frame(body_frame)
left.pack(side=LEFT)
right.pack(side=LEFT)

bottom_frame = Frame(root)
bottom_frame.pack(side=TOP)

#attach scroll bar to bottom frame for messages
scrollbar = Scrollbar(bottom_frame)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(bottom_frame, width=40)
listbox.pack()

# attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

title_var = StringVar()
title = Label(title_frame, textvariable=title_var, relief=FLAT, height=2)
title_var.set(title_str)
title.pack(side=TOP)

#F1 and F2
f1_button = Button(left, text="F1", width=15, height=2, command=callback_f1, state=NORMAL)
f1_button.pack(side=TOP)
f1_entry = Entry(left, textvariable=f1_value, width=18)
f1_entry.pack(side=TOP)
f2_button = Button(right, text="F2", width=15, height=2, command=callback_f2, state=NORMAL)
f2_button.pack(side=TOP)
f2_entry = Entry(right, textvariable=f2_value, width=18)
f2_entry.pack(side=TOP)


root.mainloop()

I've tried separating it like

f1_value.set("=> Help" + "\r\n")

and

f1_value.set("=> FUP{ret}".format(ret="\r\n"))

but nothings worked so far.

Thanks in advance!

Sending this over serial,

ser = serial.Serial()
def write_ser(st):
    ser.write(st.encode())
    time.sleep(1)
    while ser.inWaiting():
        try:
            s = ser.read(20)
        except:
            listbox.insert(END, "read failed")
            listbox.see(END)
            return
        listbox.insert(END,  "<" + str(s)[2:-1] + ">")
        listbox.see(END)
    return

with, e.g. for f1,

def callback_f1():
    listbox.insert(END,  f1_entry.get())
    listbox.see(END)
    write_ser(f1_entry.get())
    return

Solution

  • If you mean to display \ literally, escape them:

    f1_value = StringVar()
    f1_value.set("=> HELP\\r\\n")
    f2_value = StringVar()
    f2_value.set("=> VERSION\\r\\n")
    

    or use r'raw string literals':

    f1_value = StringVar()
    f1_value.set(r"=> HELP\r\n")
    f2_value = StringVar()
    f2_value.set(r"=> VERSION\r\n")