Pin-Jiun / Python

Python Document
0 stars 0 forks source link

15- File Explorer in Python using Tkinter #15

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

The following steps are involved in creating a tkinter application:

Importing the Tkinter module. Creation of the main window (container). Addition of widgets to the main window Applying the event Trigger on widgets like buttons, etc.


File dialogs

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.


# Python program to create
# a file explorer in Tkinter

# import all components
# from the tkinter library
from tkinter import *

# import filedialog module
from tkinter import filedialog

# Function for opening the
# file explorer window
def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/",
                                        title = "Select a File",
                                        filetypes = (("Text files",
                                                        "*.txt*"),
                                                    ("all files",
                                                        "*.*")))

    # Change label contents
    label_file_explorer.configure(text="File Opened: "+filename)

# Create the root window
window = Tk()

# Set window title
window.title('File Explorer')

# Set window size
window.geometry("500x500")

#Set window background color
window.config(background = "white")

# Create a File Explorer label
label_file_explorer = Label(window,
                            text = "File Explorer using Tkinter",
                            width = 100, height = 4,
                            fg = "blue")

button_explore = Button(window,
                        text = "Browse Files",
                        command = browseFiles)

button_exit = Button(window,
                    text = "Exit",
                    command = exit)

# Grid method is chosen for placing
# the widgets at respective positions
# in a table like structure by
# specifying rows and columns
label_file_explorer.grid(column = 1, row = 1)

button_explore.grid(column = 1, row = 2)

button_exit.grid(column = 1,row = 3)

# Let the window wait for any events
window.mainloop()

https://www.geeksforgeeks.org/file-explorer-in-python-using-tkinter/

Pin-Jiun commented 1 year ago

tkinter filedialog 開啟檔案對話框

最簡單的使用方法像這樣


import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)

filedialog.askopenfilename() 會回傳的類型為 str,若取消的話會回傳一個空的 tuple ()


tkinter filedialog 設定開啟檔案對話框的標題 filedialog 想要設定一些提示訊息在開啟檔案對話框的標題,來提示使用者到底要開什麼檔案的話, 可以在 filedialog.askopenfilename() 參數裡指定 title,實際上就會寫成這樣

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(parent=root, 
                                    title='Select file')

tkinter filedialog 指定一個初始的目錄來開啟檔案 通常會有個初始的目錄讓使用者去選,但預設的目錄可能離最終目標的目錄差很多層,這樣使用者要點很多次,很不方便,所以會給一個初始目錄

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(parent=root,
                                        initialdir='~/')

tkinter filedialog 設定開啟的檔案類型 假設使用者只想開啟圖片類型的檔案,又不想看到一堆非圖片類型的檔案例如像 .txt 或其他類型, 否則使用者在選擇檔案時會找很慢,所以有些情況下會去設定開啟的檔案類型,這有助於加速使用者開啟檔案, 可以在 filedialog.askopenfilename() 參數裡指定 filetypes,像這樣

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(parent=root,
                filetypes = (("jpeg files","*.jpg"),("all files","*.*")))

如果要改成開啟資料夾, 可以使用

from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
folder_selected = filedialog.askdirectory()

https://shengyu7697.github.io/python-tkinter-filedialog/