Open directory window python

Tkinter Dialogs¶

tkinter.simpledialog — Standard Tkinter input dialogs¶

The tkinter.simpledialog module contains convenience classes and functions for creating simple modal dialogs to get a value from the user.

tkinter.simpledialog. askfloat ( title , prompt , ** kw ) ¶ tkinter.simpledialog. askinteger ( title , prompt , ** kw ) ¶ tkinter.simpledialog. askstring ( title , prompt , ** kw ) ¶

The above three functions provide dialogs that prompt the user to enter a value of the desired type.

class tkinter.simpledialog. Dialog ( parent , title = None ) ¶

The base class for custom dialogs.

body ( master ) ¶

Override to construct the dialog’s interface and return the widget that should have initial focus.

buttonbox ( ) ¶

Default behaviour adds OK and Cancel buttons. Override for custom button layouts.

tkinter.filedialog — File selection dialogs¶

The tkinter.filedialog module provides classes and factory functions for creating file/directory selection windows.

Native Load/Save Dialogs¶

The following classes and functions provide file dialog windows that combine a native look-and-feel with configuration options to customize behaviour. The following keyword arguments are applicable to the classes and functions listed below:

Static factory functions

The below functions when called create a modal, native look-and-feel dialog, wait for the user’s selection, then return the selected value(s) or None to the caller.

tkinter.filedialog. askopenfile ( mode = ‘r’ , ** options ) ¶ tkinter.filedialog. askopenfiles ( mode = ‘r’ , ** options ) ¶

The above two functions create an Open dialog and return the opened file object(s) in read-only mode.

tkinter.filedialog. asksaveasfile ( mode = ‘w’ , ** options ) ¶

Create a SaveAs dialog and return a file object opened in write-only mode.

tkinter.filedialog. askopenfilename ( ** options ) ¶ tkinter.filedialog. askopenfilenames ( ** options ) ¶

The above two functions create an Open dialog and return the selected filename(s) that correspond to existing file(s).

tkinter.filedialog. asksaveasfilename ( ** options ) ¶

Create a SaveAs dialog and return the selected filename.

tkinter.filedialog. askdirectory ( ** options ) ¶

class tkinter.filedialog. Open ( master = None , ** options ) ¶ class tkinter.filedialog. SaveAs ( master = None , ** options ) ¶

The above two classes provide native dialog windows for saving and loading files.

Convenience classes

The below classes are used for creating file/directory windows from scratch. These do not emulate the native look-and-feel of the platform.

class tkinter.filedialog. Directory ( master = None , ** options ) ¶

Create a dialog prompting the user to select a directory.

The FileDialog class should be subclassed for custom event handling and behaviour.

Create a basic file selection dialog.

cancel_command ( event = None ) ¶

Trigger the termination of the dialog window.

Event handler for double-click event on directory.

Event handler for click event on directory.

Event handler for double-click event on file.

Event handler for single-click event on file.

filter_command ( event = None ) ¶

Filter the files by directory.

Retrieve the file filter currently in use.

Retrieve the currently selected item.

go ( dir_or_file = os.curdir , pattern = ‘*’ , default = » , key = None ) ¶

Render dialog and start event loop.

Exit dialog returning current selection.

Exit dialog returning filename, if any.

Update the current file selection to file.

class tkinter.filedialog. LoadFileDialog ( master , title = None ) ¶

A subclass of FileDialog that creates a dialog window for selecting an existing file.

Test that a file is provided and that the selection indicates an already existing file.

class tkinter.filedialog. SaveFileDialog ( master , title = None ) ¶

A subclass of FileDialog that creates a dialog window for selecting a destination file.

Test whether or not the selection points to a valid file that is not a directory. Confirmation is required if an already existing file is selected.

tkinter.commondialog — Dialog window templates¶

The tkinter.commondialog module provides the Dialog class that is the base class for dialogs defined in other supporting modules.

class tkinter.commondialog. Dialog ( master = None , ** options ) ¶ show ( color = None , ** options ) ¶

Источник

Python how to open directory windows in python

That is why renaming open to self.open works Solution 1: can be used for files/urls also Solution 2: this would probably have to be done manually, or have as a config item since there are many file managers that users may want to use. Exemple in Windows: Others interesting terminal commands: Using Commands Solution 3: self has to be used when you are calling a function defined under the same class name of which label1 or button1 is an object.

Make OS open directory in Python

There is os.startfile, but it’s only available under windows:

import os os.startfile('C:/') # opens explorer at C:\ drive 

Here someone (credits to Eric_Dexter@msn.com apparently) posted an alternative for use on unix-like systems, and someone mentions the desktop package available at pypi (but i’ve never used it). The suggested method:

import os import subprocess def startfile(filename): try: os.startfile(filename) except: subprocess.Popen(['xdg-open', filename]) 

So to complete the answer, use:

Python: How to open a folder on Windows, However, this is neither supported nor portable. This method is a simplified version of the approved answer. import os path = «C:\\Users» def listdir (dir): filenames = os.listdir (dir) for files in filenames: print (files) listdir (path) ok here is another piece of cake it list all your files in a directory. Code sampleimport ospath = «C:/Users»path = os.path.realpath(path)os.startfile(path)Feedback

Tkinter — way to open a directory window in Windows Explorer

Thanks everyone for your help it was a combo of your answers that helped with this one!

Still not 100% on why what I did worked but I added self as the argument to open() so open(self) and added as the command self.open. So the edited code from my question looks like this:

def open(self): os.system("start C:/folder dir/") button1= Button(self, text="Pre TC", fg="red", font=("Ariel", 9, "bold"), command=self.open) 

(Also changed the name of the button)

If anyone knows why the self argument has to be there or can point me in the direction of more info that would be greatly appreciated.

You can use a terminal command to do this and make a button to call this function. Exemple in Windows:

from tkinter import * from tkinter.ttk import * import os # opening any folder def openFolder(): path='C:' command = 'explorer.exe ' + path os.system(command) root = Tk() root.geometry('100x100') btn = Button(root, text = 'Click me !',command = openFolder) btn.pack(side = 'top') root.mainloop() 

Others interesting terminal commands:

import os # opening files file = 'test.md' command = 'start ' + file os.system(command) # opening current folder command = 'explorer.exe .' os.system(command) # opening any folder path='C:' command = 'explorer.exe ' + path os.system(command) 

self has to be used when you are calling a function defined under the same class name of which label1 or button1 is an object. Otherwise you get the Tkinter callback exception as the function is not found.

That is why renaming open to self.open works

Windows path in Python, from pathlib import Path data_folder = Path («source_data/text_files/») file_to_open = data_folder / «raw_data.txt» print (file_to_open.read_text ()) Path takes a path-like string and adjusts everything for the …

Standard way to open a folder window in linux?

os.system('xdg-open "%s"' % foldername) 

xdg-open can be used for files/urls also

this would probably have to be done manually, or have as a config item since there are many file managers that users may want to use. Providing a way for command options as well.

There might be an function that launches the defaults for kde or gnome in their respective toolkits but I haven’t had reason to look for them.

You’re going to have to do this based on the running window manager. OSX and Windows have a (defacto) standard way because there is only one choice.

You shouldn’t need to specify the exact filemanager application, though, this should be possible to do through the wm. I know Gnome does, and it’s important to do this in KDE since there are two possible file managers (Konqueror/Dolphin) that may be in use.

I agree that this would be a good thing for freedesktop.org to standardize, although I doubt it will happen unless someone steps up and volunteers to do it.

EDIT: I wasn’t aware of xdg-open. Good to know!

Open an existing folder/directory using Python, I’m trying to create a python program which has many buttons for various operations like opening camera , flash on and off , in rasberrypi.Now i’m trying to open a directory which contains all the captured images,using the default file explorer in pi,i’ve tried os.path open but it only opens up specific files and not …

Источник

Читайте также:  Python in the water
Оцените статью