tk filedialog로 열어 선택한 파일의 내용을 읽고 싶습니다. 내가 파일을 선택하고 열기 버튼을 클릭하면 파일이 열리지 않고 대화 상자가 닫힙니다. 파일의 내용을 읽을 수 있도록 메모장에서 선택한 파일을 어떻게 열 수 있습니까? @PM 2RING 및 @Erik에서 힌트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()
EDIT 가 나는 파일을 선택할 때 NOTEPAD.EXE 열리고 그것을 반환 할 filedialog.askopenfilename filedialog.askopenfile 변경.
from tkinter import *
from tkinter import filedialog
import os
def my_file():
filename = filedialog.askopenfilename(initialdir="C:/", title="select
file", filetypes=(("text files", "*.txt"), ("all files", "*.*")))
for f in filename:
return f
os.system(r"C:/notepad.exe" + f)
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()
그 출력이 오류 : 이 코드입니다
Blockquote'C:/notepad.exet' is not recognized as an internal or external command, operable program or batch file. Blockquote
하지만 난 그것을 terminal.I에 대한 디렉토리를 인쇄 인쇄 반환을 변경할 때 서브 프로세스로 열려고
subprocess.Popen([r'C:\Program Files (x86)\Notepad.exe' + f])
이 항목도 열지 않습니다.
메모장에서 파일을 읽으려면 메모장에서 열어 볼까요? 'filedialog.askopenfile'은 텍스트 위젯 내부에 텍스트를 표시하는 것과 같이 Tkinter 프로그램 내에서 사용할 수 있도록 파일을 여는 것입니다. 난 당신이 메모장에서 파일을 열려면 관련된'filedialog.askopenfilename'과'subprocess' 모듈 함수 또는'os.system'을 사용할 수있을 것이라고 생각합니다. 그러나 그것은 좀 이상합니다. IMHO. –