How can I remove newline character (\n) at the end of Text widget?

依然范特西╮ 提交于 2021-01-28 08:05:57

问题


I am working on a Tkinter GUI.

I am wondering how can I remove newline character (\n) at the end of Text widget? It's making my App looking ugly I don't want this.

Example code is:

from tkinter import *
from tkinter import ttk

window = Tk()
window.title('Advanced Calculator v0.9')
window.geometry('500x400')
window.resizable(False, False)

window.configure(background='black')

hist_t = Text(window, background='black', height='7', foreground='red', font='Verdana', highlightcolor='grey', highlightthickness=1, relief='solid')

Label(window, text='History:', background='black', foreground='red').pack(pady=3, anchor='w')

hist_t.pack(fill='x', padx=3)

hist_t.insert('end', '4 + 4 = 8\n2 + 2 = 4\n5 + 3 = 8\n10 + 13 = 23\n30 + 10 = 40\n12 + 8 = 20')

window.mainloop()

Thanks.


回答1:


Assert:

hist_t = tk.Text(...)

You can always call hist_t.delete('end-1c', 'end') whenever you want to remove the last char, which is newline in this case. You can also remove the last char only if it is newline:

while hist_t.get('end-1c', 'end') == '\n':
    hist_t.delete('end-1c', 'end')



回答2:


Your problem doesn't appear to be any extra newlines, it's simply that you configured it to show 7 lines but you're only inserting 6. If you set the height to 6 you won't see this blank line.



来源:https://stackoverflow.com/questions/48220788/how-can-i-remove-newline-character-n-at-the-end-of-text-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!