Python 如何使用tkfiledialog打开文件并使用记事本读取内容

Python 如何使用tkfiledialog打开文件并使用记事本读取内容,python,tkinter,Python,Tkinter,我想通过使用tk filedialog打开来读取所选文件的内容。选择文件并单击“打开”按钮时,文件不会打开,而是关闭对话框。如何使用记事本打开所选文件,以便能够读取文件中的内容 from tkinter import * from tkinter import filedialog def my_file(): filename = filedialog.askopenfile(mode="r", initialdir="/", title="select file",

我想通过使用tk filedialog打开来读取所选文件的内容。选择文件并单击“打开”按钮时,文件不会打开,而是关闭对话框。如何使用记事本打开所选文件,以便能够读取文件中的内容

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的提示下,我将filedialog.askopenfile更改为filedialog.askopenfilename,以便在选择文件时将其返回到notepad.exe打开。 代码如下:

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'未被识别为内部或外部命令, 可操作的程序或批处理文件。 大宗报价

但是,当我将返回更改为“打印”时,请将目录打印到终端。我尝试使用子进程打开

 subprocess.Popen([r'C:\Program Files (x86)\Notepad.exe' + f])

它也不会用这个打开。

下面的代码将显示一个按钮和一个文本小部件。该按钮将加载到您的文件中,文本小部件将显示它。我想这就是你的想法

from tkinter import *
from tkinter import filedialog

def my_file():
    file = filedialog.askopenfile(mode="r", initialdir="/", title="select file",
                                   filetypes=(("text files", "*.txt"), ("all files", "*.*")))
    t.insert(END, file.read())
    file.close()

root = Tk()
root.geometry("300x300")
                          #open the selected txt file with notepad to read the content
t = Text(root)
t.grid(row=0, column=0)
b = Button(root, text="open text file", command = my_file)
b.grid(row=1, column=0)

root.mainloop()

下面的代码将显示一个按钮和一个文本小部件。该按钮将加载到您的文件中,文本小部件将显示它。我想这就是你的想法

from tkinter import *
from tkinter import filedialog

def my_file():
    file = filedialog.askopenfile(mode="r", initialdir="/", title="select file",
                                   filetypes=(("text files", "*.txt"), ("all files", "*.*")))
    t.insert(END, file.read())
    file.close()

root = Tk()
root.geometry("300x300")
                          #open the selected txt file with notepad to read the content
t = Text(root)
t.grid(row=0, column=0)
b = Button(root, text="open text file", command = my_file)
b.grid(row=1, column=0)

root.mainloop()

正如pm2ring所提到的,我将使用
os.system
功能。正如其描述中提到的,“os.system(command)”让您执行命令,就像您在命令提示符中编写命令一样,因此
os.system(“Notepad c:/users/yourName/junk.txt)
将在该位置打开一个名为junk.txt的文件

换句话说,从
filedialog.askopenfilename
调用中获得文件名后,执行以下操作:

import os
os.system("Notepad " + filename) # where filename is the string that filedialog.askopenfilename returns

代码中的实现应该不会太糟糕,但是如果您需要更完整的示例,请让我知道。

正如PM 2Ring所提到的,我将使用
os.system
函数。正如其描述中提到的,“os.system(command)”让您执行命令,就像您在命令提示符中编写命令一样,因此
os.system(“记事本c:/users/yourName/junk.txt)
将打开一个名为junk.txt的文件,如果它位于该位置

换句话说,从
filedialog.askopenfilename
调用中获得文件名后,执行以下操作:

import os
os.system("Notepad " + filename) # where filename is the string that filedialog.askopenfilename returns

代码中的实现应该不会太糟糕,但是如果您需要更完整的示例,请告诉我。

这里有一些东西需要修改

首先,
C:/notepad.exe
不是记事本的位置(至少不是在任何默认设置的Windows计算机上),您可以简单地使用
notepad.exe
,这将使它与将记事本移动到另一个位置的系统更兼容(需要引用)

第二,执行

for f in filename:
    return f
os.system(r"C:/notepad.exe" + f)
实际上,您的程序正在将字符串加载到循环中,计算第一个字符(可能是“C”)然后将字符串返回到未接收任何返回值的
按钮
小部件。这会中断您的函数,因此它永远不会真正到达您的
os.system(r“C:/notepad.exe”+f)
声明

您还需要在用于打开记事本的语句
Notepad.exe
和实际的文件声明
f
之间包含一个空格,否则您将运行类似于
Notepad.exeC:/text.txt
的操作,这将向您抛出一个错误

您应该做的事情如下所示:

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", "*.*")))
    os.system(r"notepad.exe " + filename)

root = Tk()
root.geometry("300x300")
b = Button(root, text="open text file", command = my_file).pack()

root.mainloop()

我想补充一点,我不知道你为什么要这样做,为什么不简单地在一个
text
小部件中显示文本?你在这里要做的是为人们在记事本中打开文件添加一个额外的步骤,而不是打开文件资源管理器,找到文件,然后打开它,他们必须打开你的程序,然后打开文件资源管理器,然后找到t他打开文件,然后打开它。它至少增加了一次额外的点击,更不用说程序的加载时间了。

这里有几点需要修改

首先,
C:/notepad.exe
不是记事本的位置(至少不是在任何默认设置的Windows计算机上),您可以简单地使用
notepad.exe
,这将使它与将记事本移动到另一个位置的系统更兼容(需要引用)

第二,执行

for f in filename:
    return f
os.system(r"C:/notepad.exe" + f)
实际上,您的程序正在将字符串加载到循环中,计算第一个字符(可能是“C”)然后将字符串返回到未接收任何返回值的
按钮
小部件。这会中断您的函数,因此它永远不会真正到达您的
os.system(r“C:/notepad.exe”+f)
声明

您还需要在用于打开记事本的语句
Notepad.exe
和实际的文件声明
f
之间包含一个空格,否则您将运行类似于
Notepad.exeC:/text.txt
的操作,这将向您抛出一个错误

您应该做的事情如下所示:

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", "*.*")))
    os.system(r"notepad.exe " + filename)

root = Tk()
root.geometry("300x300")
b = Button(root, text="open text file", command = my_file).pack()

root.mainloop()

我想补充一点,我不知道你为什么要这样做,为什么不简单地在一个
text
小部件中显示文本?你在这里要做的是为人们在记事本中打开文件添加一个额外的步骤,而不是打开文件资源管理器,找到文件,然后打开它,他们必须打开你的程序,然后打开文件资源管理器,然后找到t他打开文件,然后打开它。它至少增加了一次额外的点击,更不用说程序的加载时间了。

如果你想在记事本中读取文件,为什么不在记事本中打开它?
文件对话框。askopenfile
用于打开文件,以便你可以在Tkinter程序中使用它们,例如在文本小部件中显示文本。我想是的您可以使用相关的
filedialog.askopenfilename
子流程
模块函数或
o