1

Among other things, I am currently trying to create a basic text editor which can open text files, edit them, and then save them. I have used this Tkinter dialogue for the GUI 'file manager,' but I was wondering if anyone knew the way to access the one that comes default on Windows?

Thanks!

Technical Things:

OS: Windows 7

Language: Python 2.7.3

EDIT 1

By the DEFAULT file dialogue, I mean the windows explorer dialogue:

file selector

I also use mac. Assuming that my application is cross-platform, would there be any way for me to have the program check what the os was, and then open either Finder or Windows Explorer.

I need the program to be able to save and open items in different commands. How would I do this?

4
  • From what I understand, tkinter is using the default Windows (and OS X and Gtk+) file dialogs, but the problem is that Windows has about 35 different styles of file dialogs, and the default is the one that looks like Win95, not the fancy new one that you want. Am I right here?
    – abarnert
    Feb 15, 2013 at 1:15
  • @abarnert you are completely correct! I want to be able to open the file - similarly to the default one, but I don't want the ugly look. Feb 15, 2013 at 1:18
  • Not related with your question, but I thought it was worth to be noted: you should activate the display of your file's extension. That's a serious security leak into Windows' default config. (You can be easily duped otherwise. for instance thinking you are opening an image but it's an executable. Only the extension can't fool you)
    – jeromej
    Feb 15, 2013 at 8:39
  • @JeromeJ Thanks for the advice! I didn't know that that was dangerous. Luckily, I already have it set up like that on my computer - this was a photo I found on the Internet. Feb 15, 2013 at 12:22

2 Answers 2

1

It's not exactly clear what you're asking, since the one that tkinter comes with is default in Windows. Here's another link for that, just in case you got mixed up somewhere along the line. Remember that you can set it so it only finds a certain type of file, starts in a specific place, returns the filename or directory, or even open the file (I think)

If you mean the Windows Explorer you can open it and close it with pywin32, but not much else. Taken from this answer

import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
2
  • this was really helpful, but my files open up in Notepad, not my application. Is there any way to have it select the file, and then have python read it and display it? Feb 14, 2013 at 23:27
  • You'd be able edit the registry so that your python program is the default program to open that type of file temporarily, but I wouldn't suggest it. Feb 14, 2013 at 23:57
1

try importing tkFileDialog:

import tkFileDialog as tkfd
def save():
    savenm = tkfd.asksaveasfile()
    f = open(savenm.name,"w")
    # then put what to do with the opened file
def open():
    opennm = tkfd.askopenfile()
    f = open(savenm.name,"r")
    # then put what to do with the opened file

then make a button that uses the functions:

import Tkinter as tk
root=tk.Tk()
SAVELOADFRAME = tk.Frame(root)
SAVELOADFRAME.pack()
savebtn = Button(SAVELOADFRAME,text="Save",command=save)
savebtn.pack(side=LEFT)
root.mainloop()
loadbtn = Button(SAVELOADFRAME,text="Open",command=open)
loadbtn.pack(side=RIGHT)

maybe if you have a notepad box you might want to insert the text from the file into the tk.Text widget. The above code only works for text based files really (e.g. *.js, *.txt, *.py) not *.exe, *.dll, etcetera.

hope that solves your problem :^)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.