,可以通过以下步骤实现:
import tkinter as tk
window = tk.Tk()
text_box = tk.Text(window)
button = tk.Button(window, text="导入文本", command=import_text)
def import_text():
file_path = "path/to/your/file.txt" # 替换为实际的文件路径
with open(file_path, "r") as file:
text = file.read()
text_box.insert(tk.END, text)
text_box.pack()
button.pack()
window.mainloop()
完整的代码示例:
import tkinter as tk
def import_text():
file_path = "path/to/your/file.txt" # 替换为实际的文件路径
with open(file_path, "r") as file:
text = file.read()
text_box.insert(tk.END, text)
window = tk.Tk()
text_box = tk.Text(window)
button = tk.Button(window, text="导入文本", command=import_text)
text_box.pack()
button.pack()
window.mainloop()
这样,当点击按钮时,程序将从指定文件中读取文本内容,并将其显示在Tkinter文本框中。你可以根据实际需求修改文件路径和按钮的文本内容。
领取专属 10元无门槛券
手把手带您无忧上云