【发布时间】:2020-10-15 14:36:31
【问题描述】:
所以我正在用 Python Tkinter 制作一个简单的文本编辑器。在顶部有 2 个按钮:“保存”和“打开”。(它们显示打开/保存为对话窗口)。保存按钮没问题,但是当我想在我的编辑器中打开一个文件时,它会显示为reversed order。
这是我的代码:
from tkinter import *
from tkinter import filedialog
window = Tk()
window.geometry("1600x900")
window.title("Text Editor")
def save():
editor_content = editor.get("1.0", END)
saving = filedialog.asksaveasfile(mode = "w", defaultextension = ".py")
saving.write(editor_content)
saving.close()
def open():
open_file = filedialog.askopenfile(initialdir="/", title="Open File", filetypes=(("Python files", ".py"), ("Text Files", ".txt"), ("All Files", "*.*")))
for file_opened in open_file:
editor.insert(0.0, f'{file_opened}')
editor = Text(bg = "#1f1f1f", fg = "#b5b5b5", width = 105, height = 25,wrap = WORD, padx = 10, pady = 10, font = "consolas, 20")
editor.place(x = 0, y = 40)
save_btn = Button(width = 10, height = 2, bg = "#5e5e5e", relief = "flat", text = "Save", fg = "white", activebackground = "#4e4e4e", activeforeground = "white", command = save)
save_btn.place(x = 0, y = 0)
open_btn = Button(width = 10, height = 2, bg = "#5e5e5e", relief = "flat", text = "Open", fg = "white", activebackground = "#4e4e4e", activeforeground = "white", command = open)
open_btn.place(x = 80, y = 0)
window.mainloop()
【问题讨论】:
标签: python python-3.x tkinter editor filedialog