Filedialog, Tkinter and Opening Files

filedialog, tkinter and opening files

The exception you get is telling you filedialog is not in your namespace.
filedialog (and btw messagebox) is a tkinter module, so it is not imported just with from tkinter import *

>>> from tkinter import *
>>> filedialog
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'filedialog' is not defined
>>>

you should use for example:

>>> from tkinter import filedialog
>>> filedialog
<module 'tkinter.filedialog' from 'C:\Python32\lib\tkinter\filedialog.py'>
>>>

or

>>> import tkinter.filedialog as fdialog

or

>>> from tkinter.filedialog import askopenfilename

So this would do for your browse button:

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror

class MyFrame(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Example")
self.master.rowconfigure(5, weight=1)
self.master.columnconfigure(5, weight=1)
self.grid(sticky=W+E+N+S)

self.button = Button(self, text="Browse", command=self.load_file, width=10)
self.button.grid(row=1, column=0, sticky=W)

def load_file(self):
fname = askopenfilename(filetypes=(("Template files", "*.tplate"),
("HTML files", "*.html;*.htm"),
("All files", "*.*") ))
if fname:
try:
print("""here it comes: self.settings["template"].set(fname)""")
except: # <- naked except is a bad idea
showerror("Open Source File", "Failed to read file\n'%s'" % fname)
return

if __name__ == "__main__":
MyFrame().mainloop()

Sample Image

Set tkinter filedialog to open only executable files

There's nothing you can do by using the standard dialogs. Tkinter doesn't support listing files by attributes other than their extensions. You'll have to create your own file selection dialog if you want this feature.

How to open file with tkfiledialog and read the content with notepad

There's a few things which need to be amended here.

First of all, C:/notepad.exe is not the location of Notepad (At least not on any Windows machine with a default setup), you can simply use notepad.exe which should make it more compatible with systems that have moved Notepad to another location (citation needed).

Secondly, executing . . .

for f in filename:
return f
os.system(r"C:/notepad.exe" + f)

Doesn't do what you seem to think it does. What's actually happening here is that your program is loading the string into the loop, evaluating the first character (Probably "C") and then returning the string to the Button widget which doesn't receive any returned values. This then breaks your function, so it never actually reaches your declaration of os.system(r"C:/notepad.exe" + f).

You also need to include a space between the statement used to open Notepad notepad.exe and the actual file declaration f, otherwise you're running something like notepad.exeC:/text.txt which is going to throw an error at you.

What you should be doing is something like the below:

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()

I'd like to add that I have no clue why you're doing this, why not simply display the text in a Text widget? What you're doing here is adding one extra step for people to open files in Notepad, instead of opening file explorer, finding the file and then opening it they have to open your program, then open file explorer, then find the file and then open it. It's adding at least one extra click not to mention the load time of your program.

filedialog to open files within a folder (tkinter)

Your code is running well, i assume what you want to understand is how the filetype is used in the example.

With the list of types provided (it's a tuple actually) the browse dialog is looking in priority for files with a .tplate extension.
Then, you can change this option in the dropdown listbox to select html, python or any type of file.

fname = askopenfilename(filetypes=(("Template files", "*.tplate"),
("HTML files", "*.html;*.htm"),
("Python file", "*.py"),
("All files", "*.*") ))

If you change the order of the tuple provided, you can select another type first,

fname = askopenfilename(filetypes=(("Python file", "*.py"),
("HTML files", "*.html;*.htm"),
("All files", "*.*") ))

Check this doc for more details on the options.

Root window jump to front when opening file dialog in child window tkinter

Below code worked for me. GUI is my toplevel window, and root is my main window.

    GUI.attributes('-topmost', 0)
filename = filedialog.askopenfilename()
GUI.attributes('-topmost', 1)

Once the filedialog opens file, I will bring back my GUI window to front.

Quick and easy file dialog in Python?

Tkinter is the easiest way if you don't want to have any other dependencies.
To show only the dialog without any other GUI elements, you have to hide the root window using the withdraw method:

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

Python 2 variant:

import Tkinter, tkFileDialog

root = Tkinter.Tk()
root.withdraw()

file_path = tkFileDialog.askopenfilename()


Related Topics



Leave a reply



Submit