我的代碼目前的功能如下:
創建目錄 --> 將作業目錄更改為創建的目錄 --> 將 .txt 檔案生成到目錄中的選項,可在串列框中查看 --> 從串列框中選擇 .txt 檔案并在文本小部件中查看內容。
然后我正在努力尋找一種方法來輸入文本并保存/附加到文本小部件中當前打開/查看的文本檔案。
代碼:
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()
作為實作此功能的主要方法,無法調整任何一個以使其運行。
uj5u.com熱心網友回復:
在第二個函式中,tkinter 不希望文本小部件獲取函式以整數為索引。在這里,我重新創建了第二個保存功能,但將行編輯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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411586.html
標籤:
