Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening File (Tkinter)

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

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

like image 430
Serial Avatar asked May 07 '13 22:05

Serial


People also ask

How do I open a tkinter file in Python?

Tkinter ProgrammingImport the Tkinter module. Create the GUI application main window. Add one or more of the above-mentioned widgets to the GUI application. Enter the main event loop to take action against each event triggered by the user.

How do I browse files in tkinter?

In order to do so, we have to import the filedialog module from Tkinter. The File dialog module will help you open, save files or directories. In order to open a file explorer, we have to use the method, askopenfilename(). This function creates a file dialog object.

What is file dialog in Python?

File dialogs help you open, save files or directories. This is the type of dialog you get when you click file,open. This dialog comes out of the module, there's no need to write all the code manually. Tkinter does not have a native looking file dialog, instead it has the customer tk style.


1 Answers

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

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:

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/

like image 99
Bharat Avatar answered Sep 29 '22 02:09

Bharat