我想用tkfiledialog打开所选文件的内容。当我选择文件并单击“打开”按钮时,文件不会打开,而是关闭对话框。如何使用记事本打开所选文件,以便能够读取文件中的内容。在from tkinter import *
from tkinter import filedialog
def my_file():
filename = filedialog.askopenfile(mode="r", initialdir="/", title="select file",
filetypes=(("text files", "*.txt"), ("all files", "*.*")))
root = Tk()
root.geometry("300x300")
#open the selected txt file with notepad to read the content
b = Button(root, text="open text file", command = my_file).pack()
root.mainloop()
编辑
在@PM 2RING和@Erik的提示下,我更改了文件对话框.askopenfile到文件对话框.askopenfilename把它放回去打开记事本.exe当我选择文件时。
代码如下:
^{pr2}$
它输出以下错误:Blockquote'C:/notepad.exet' is not recognized as an internal or external command,
operable program or batch file.
Blockquote
但是当我把返回改为打印时,它把目录打印到终端subprocess.Popen([r'C:\Program Files (x86)\Notepad.exe' + f])
它也不能用这个来打开。在