簡體   English   中英

在 tkinter 中,如何將輸入到文本區域的文本保存到已經打開的文本文件中?

[英]In tkinter, how can I save text entered into a text area into an already open text file?

我的代碼目前的功能如下:

創建目錄 --> 將工作目錄更改為創建的目錄 --> 將.txt 文件生成到目錄中的選項,可在列表框中查看 --> select 來自列表框的 a.txt 文件並在文本小部件中查看內容。

然后我正在努力尋找一種方法來輸入文本並將 / append 保存到文本小部件中當前打開/查看的文本文件中。

代碼:

from tkinter import *
from os import mkdir, getcwd, path, listdir, chdir

def dir_gen():
    curr_dir = getcwd()
    print("curr dir", curr_dir)
    fin_dir = path.join(curr_dir, r'note_folder')
    print("fin dir", fin_dir)
    if not path.exists(fin_dir):
        makedirs(fin_dir)
        print("directory created")

dir_gen()

flist = listdir("note_folder")
print("flist", flist)

chdir("note_folder")


def main_gui():
    window = Tk()
    window. geometry("400x400")
    window.config(bg="#000000")

    def go_home():
        window.destroy()
        main_gui()
        print("refreshed window")


    lbox = Listbox(window)
    lbox.place(x=20, y=20)

    listdir()
    print("listdir", listdir())
    for l in listdir():
        lbox.insert(END, l)


    add_note_entry = Entry(window)
    add_note_entry.place(x=20, y=190)
    add_note_entry.config(width=20)

    add_line_entry = Entry(window)
    add_line_entry.place(x=200, y=300)
    add_line_entry.config(width=27)


    def add_note():
        title = add_note_entry.get()
        hdl = open(title + ".txt", "w")
        hdl.close(), go_home()

    def open_note(event):
        x = lbox.curselection()[0]
        print("x", x)
        file = lbox.get(x)
        print("file", file)
        with open(file, 'r+') as file:
            file = file.read()
        text_output.delete('1.0', END)
        text_output.insert(END, file)

    def save_note():
        pass


    add_note_button = Button(window, command=add_note)
    add_note_button.place(x=40, y=210)
    add_note_button.config(width=10, height=2, text=("ADD NOTE"))

    save_note_button = Button(window, command=save_note)
    save_note_button.place(x=240, y=345)
    save_note_button.config(width=10, height=2, text=("SAVE NOTE"))

    text_output = Text(window)
    text_output.place(x=200, y=20)
    text_output.config(height=15, width=20)

    lbox.bind("<<ListboxSelect>>", open_note)

    window.mainloop()

main_gui()

我努力了

  def save_note(event):
        line = add_line_entry.get()
        x = lbox.curselection()[0]
        file = lbox.get(x)
        file = str(filename)
        print("file2", file)
        with open(file, 'a') as file:
            file.write(line)
        open_note()

  def save_note():
        x = lbox.curselection()[0]
        file = lbox.get(x)
        hdl = open(file, 'r+')
        nt = text_output.get(1)
        for l in nt:
            hdl.write(l)
        hdl.close()

作為實現此功能的主要方法,無法調整任何一個以使其運行。

在第二個 function 中,tkinter 不期望文本小部件獲取 function 被索引為 Z1527EDB15DF530602365。 在這里,我重新創建了第二次保存 function 但將nt = text_output.get(1)行編輯為nt = text_output.get('1.0', 'end') 這將獲取 tkinter 文本小部件中從第一個字符到最后一個字符的所有內容。

    def save_note():
        x = lbox.curselection()[0]
        file = lbox.get(x)
        hdl = open(file, 'r+')
        nt = text_output.get('1.0', 'end')
        for l in nt:
            hdl.write(l)
        hdl.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM