L
L
leon -----2021-06-08 20:22:23
Python
leon -----, 2021-06-08 20:22:23

Why is \n added on exit?

I wrote a code that sorts words or numbers.
And when exiting, it is added to any of the words n\:

carousel, movie\n, car

Here is the code:
from tkinter import *  
from tkinter.ttk import Radiobutton 
from tkinter import scrolledtext


def sortint():
    zxcint = entint.get(1.0, END).split(",")
    zxcint = [int(i) for i in zxcint]
    zxcint.sort()
    entint.delete(1.0, END)
    entint.insert(INSERT,str(zxcint)[1:-1])

def delint():
    entint.delete(1.0, END)

def per123int():
    global entint
    entint = scrolledtext.ScrolledText(root,width=40,height=4)
    entint.grid(column=1,row=0)
    entint.insert(INSERT,'Вводите числа через зяпятую(123,32,54 и т.п). Сотрите меня')
    butsortint = Button(root,text="Сортировка",command=sortint)
    butsortint.grid(column=2,row=0)
    butdelint = Button(root,text="Очистка",command=delint)
    butdelint.grid(column=2,row=1)

def sortword():
    translation = {39: None}
    zxcword = entword.get(1.0, END).split(",")
    zxcword.sort()
    entword.delete(1.0, END)
    entword.insert(INSERT,str(zxcword).translate(translation)[1:-1])

def delword():
    entword.delete(1.0, END)

def per123word():
    global entword
    entword = scrolledtext.ScrolledText(root,width=40,height=4)
    entword.grid(column=1,row=0)
    entword.insert(INSERT,'Вводите слова через зяпятую(машина,карусель,кино и т.п). Сотрите меня')
    butsortword = Button(root,text="Сортировка",command=sortword)
    butsortword.grid(column=2,row=0)
    butdelword = Button(root,text="Очистка",command=delword)
    butdelword.grid(column=2,row=1)

root = Tk()
root.title("GUGU")
root.geometry('600x555')


radbutint = Radiobutton(root,text='Цифры',value=1,command=per123int)
radbutword = Radiobutton(root,text='Слова',value=2,command=per123word)

radbutint.grid(column=0,row=0)
radbutword.grid(column=0,row=1)

root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-06-08
@leon1000

I suspect that not to any, but to the latter is added. Because in the field enter with a newline at the end.
Further this translation haunts you.
At the stage str(zxcword), it is already fixed in the form of an escape sequence.

>>> s = "мама,мыла,ламу\n"
>>> print(s)
мама,мыла,ламу

>>> a = s.split(",")
>>> a.sort()
>>> a
['ламу\n', 'мама', 'мыла']

Do strip(): and convert the list to a string in a human way (this is for the future):
zxcword = entword.get(1.0, END).strip().split(",")
",".join(zxcword)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question