Python 打开文件 (Tkinter)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16429716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:35:57  来源:igfitidea点击:

Opening File (Tkinter)

pythontkinteropenfiledialog

提问by Serial

I'm trying to make a Tkinter program that can open a file. So far it opens a tk window that has an option that says File then a drop-down menu and it says open when you click it it opens a file window but i cant figure out how to actually open that file

我正在尝试制作一个可以打开文件的 Tkinter 程序。到目前为止,它打开了一个 tk 窗口,上面有一个选项,上面写着文件,然后是一个下拉菜单,当你点击它时,它说打开它会打开一个文件窗口,但我不知道如何实际打开那个文件

Here is the code I'm trying:

这是我正在尝试的代码:

from Tkinter import *
from tkFileDialog import askopenfilename
def openfile():

   filename = askopenfilename(parent=root)
   f = open(filename)
   f.read()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=openfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

root.config(menu=menubar)
root.mainloop()

Here is what the window looks like

这是窗口的样子

采纳答案by Bharat

You have already opened the file when you did f = open(filename). To print the contents of the file to the console, you could do print f.read(). Or go through the file line by line & print the contents like

当您打开文件时,您已经打开了f = open(filename)。要将文件的内容打印到控制台,您可以执行print f.read(). 或者逐行浏览文件并打印内容,例如

for line in f:
    print line

Here is an example of how to open a file and print it's contents on the UI. I found this example to be helpful and it shows exactly what you want:

以下是如何打开文件并在 UI 上打印其内容的示例。我发现这个例子很有帮助,它准确地显示了你想要的:

from Tkinter import Frame, Tk, BOTH, Text, Menu, END
import tkFileDialog 

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent        
        self.initUI()

    def initUI(self):

        self.parent.title("File dialog")
        self.pack(fill=BOTH, expand=1)

        menubar = Menu(self.parent)
        self.parent.config(menu=menubar)

        fileMenu = Menu(menubar)
        fileMenu.add_command(label="Open", command=self.onOpen)
        menubar.add_cascade(label="File", menu=fileMenu)        

        self.txt = Text(self)
        self.txt.pack(fill=BOTH, expand=1)


    def onOpen(self):

        ftypes = [('Python files', '*.py'), ('All files', '*')]
        dlg = tkFileDialog.Open(self, filetypes = ftypes)
        fl = dlg.show()

        if fl != '':
            text = self.readFile(fl)
            self.txt.insert(END, text)

    def readFile(self, filename):

        f = open(filename, "r")
        text = f.read()
        return text


def main():

    root = Tk()
    ex = Example(root)
    root.geometry("300x250+300+300")
    root.mainloop()  


if __name__ == '__main__':
    main()  

Source: http://zetcode.com/tkinter/dialogs/

来源:http: //zetcode.com/tkinter/dialogs/