Tkinter:如何处理文本区域中的新行并将它们保存为单个条目?

huangapple go评论68阅读模式
英文:

Tkinter: How to handle new lines in a textarea and save them as a single entry?

问题

我有一个在tkinter上的笔记保存应用程序当我在文本区域中写入一个段落时它会作为一个单独的条目保存在已保存的笔记中但是当我使用回车键比如如果我换行或者说我制作一个列表那么每一行都会被保存为一个单独的条目如何解决这个问题这是我的保存功能

    def save_note():
    note = text.get("1.0", "end-1c")
    if note:
        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")
英文:

I have a notes saving app on tkinter. When i write a para in my textarea then it is saving as a single entry in saved notes but when i use enter key like if i go to next line or we can say if i make a list, then each line is saving as a separate entry. How to resolve this. here is my save function

def save_note():
note = text.get("1.0", "end-1c")
if note:
    with open("notes.txt", "a") as file:
        file.write(note + "\n")
    text.delete("1.0", tk.END)
    text.see(tk.END)
    load_notes()
else:
    messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")

答案1

得分: 0

你可以简单地使用替换函数更改你的文本,如下所示:file.write(note.replace("\n", "") + "\n")

英文:

You can simply change your text with replace function file.write(note.replace("\n", "") + "\n")

答案2

得分: 0

尝试这种方式。它会起作用。

def save_note():
    note = text.get("1.0", tk.END).strip()
    note = note.replace('\n', ' ')
    note = note.replace('\r', '')
    if note:
        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")
英文:

Try this way. It will work.

def save_note():
    note = text.get("1.0", tk.END).strip()
    note = note.replace('\n', ' ')
    note = note.replace('\r', '')
    if note:
        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")

答案3

得分: 0

要将段落保存为笔记中的单个条目,您可以修改保存函数以处理换行符,并将它们转换为空格或任何其他所需的分隔符。以下是更新后的保存函数版本:

def save_note():
    note = text.get("1.0", "end-1c")
    if note:
        # 用空格或所需的分隔符替换换行符
        note = note.replace('\n', ' ')

        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")
英文:

To save the paragraphs as a single entry in your notes, you can modify your save function to handle the line breaks (newlines) and convert them to a space or any other desired delimiter. Here's an updated version of your save function:

def save_note():
    note = text.get("1.0", "end-1c")
    if note:
        # Replace line breaks with a space or desired delimiter
        note = note.replace('\n', ' ')

        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")

答案4

得分: 0

更简单的方法来做这个。使用Walrus和f-string格式。

注意,你不需要为Entry小部件添加参数。

  • 使用Walrus,所以我们不需要重复的 note
  • 删除Entry小部件的参数。
  • 使用f-string格式 f'{note}' + "\n"
  • Entry.delete中的1.0替换为0。

这是我做这个的实际方法。

与你的脚本一样修改的代码片段:

def save_note() -> str:
    if note := text.get():
        with open("notes.txt", "a") as file:
            file.write(f'{note}' + "\n")
        text.delete(0, tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")
英文:

Easier way to do this. Using Walrus and f-string format.

Noticed, you don't need parameters for Entry widget.

  • Use Walrus, so we don't need duplicate note
  • Remove parameter for Entry widget.
  • Use f-string format f'{note}' + "\n"
  • Replace 1.0 to 0 for Entry.delete.

This is practical way I do this.

Snippet modified same as your script:

def save_note() -> str:
    if note := text.get():
    #if note:
        with open("notes.txt", "a") as file:
            file.write(f'{note}' + "\n")
        text.delete(0, tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")

答案5

得分: 0

我在这里做了一些改进。它可能会有用。

def save_note():
    note = text.get("1.0", tk.END).strip()
    note = note.replace('\r\n', '\n')
    if note:
        lines = note.split('\n')
        if len(lines) > 1:
            note = '\n'.join(['- ' + line for line in lines]) 
        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")
英文:

I have improved something here. It can be useful

def save_note():
    note = text.get("1.0", tk.END).strip()
    note = note.replace('\r\n', '\n')
    if note:
        lines = note.split('\n')
        if len(lines) > 1:
            note = '\n'.join(['- ' + line for line in lines]) 
        with open("notes.txt", "a") as file:
            file.write(note + "\n")
        text.delete("1.0", tk.END)
        text.see(tk.END)
        load_notes()
    else:
        messagebox.showwarning("Leere Notiz", "Bitte geben Sie eine Notiz ein.")

huangapple
  • 本文由 发表于 2023年7月3日 16:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定