Filedialog trong python là gì?

Hướng dẫn này sẽ trình bày cách tạo hộp thoại tệp bằng cách sử dụng lớp Tkinter

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
5 và khám phá một số phương pháp

Hộp thoại tệp trong Thư viện Tkinter của Python

Thư viện Python Tkinter cung cấp nhiều hộp thoại mà bạn có thể sử dụng để làm việc với tệp. Bằng cách phát triển trong Tkinter, bạn không cần phải tự tạo các hộp thoại tiêu chuẩn

Hộp thoại tệp bao gồm hộp thoại mở tệp, hộp lưu tệp, v.v.

Hộp thoại tệp giúp bạn mở tệp hoặc thư mục lưu. Bạn nhận được loại hộp thoại này khi nhấp vào tệp để mở tệp

Lớp

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
5 giúp tạo hộp thoại;

Sử dụng Phương pháp
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
7 để duyệt tệp bằng hộp thoại Tkinter trong Python

Tùy chọnGiải thích
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
8Có thể đặt tên trên hộp thoại bằng tùy chọn này.
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
9Lấy đường dẫn để mở hộp thoại trong một thư mục cụ thể.
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
0Tùy chọn này cần một bộ. Bạn có thể chuyển một tên tệp và phần mở rộng cụ thể.
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
0Chúng tôi không chỉ định phần mở rộng tệp. Nếu chúng ta truyền tùy chọn này trong phương thức, hộp thoại sẽ tự động chọn phần mở rộng được truyền cho giá trị. Tùy chọn này chỉ hoạt động với hộp thoại lưu

Chúng tôi có thể sử dụng phương pháp

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
7 để tải tệp lên. Phương thức này trả về một vị trí tập tin

Trong ví dụ này, chúng tôi đã tạo một hàm

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
2. Hàm
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
2 giúp xuất hiện hộp thoại sử dụng phương thức
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
4

from tkinter import Tk,filedialog,Button,Label
from PIL import ImageTk,Image

root=Tk[]
root.geometry['400x400']
def open[]:
    # Create a dialog box
    root.file_name=filedialog.askopenfilename[initialdir="your directory path", title="file uploader",
                               filetypes=[["jpg files", "*.jpg"], ["all files", "*.*"]]]
    # takes path that is selected by dialog box
    selected_image =Image.open[root.file_name]
    # resize image
    selected_image = selected_image.resize[[300, 205], Image.ANTIALIAS]
    # displays an image
    root.image = ImageTk.PhotoImage[selected_image]
    selected_image_label=Label[root,image=root.image].pack[]

btn=Button[root,text='upload image',command=open].pack[]
root.mainloop[]

Sử dụng phương pháp
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
5 để lưu tệp bằng hộp thoại Tkinter trong Python

Nếu bạn đọc đến đây, bạn đã quen thuộc với hộp thoại lưu dưới dạng. Trong trường hợp này, chúng tôi đang sử dụng phương pháp

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
5

Phương pháp này cho phép bạn lưu tệp vào vị trí mong muốn

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]

Phương thức

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
7 giúp lấy văn bản từ tiện ích
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
8 và cho phép bạn chọn vị trí bạn muốn lưu tệp này

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
0

Sử dụng Phương thức
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
9 của Tkinter để chọn thư mục trong Python

Phương pháp này chỉ chọn một thư mục. Phương pháp này không cho phép chọn bất kỳ tệp nào

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
2

Bạn có thể đặt

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
00 trước đường dẫn nếu gặp lỗi Unicode. Ví dụ,
from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
01

Chúng ta đã thảo luận về các phương thức chủ yếu có thể sử dụng được, nhưng vẫn còn nhiều phương thức khác tồn tại trong lớp

from tkinter import *
from tkinter import filedialog

def saveFile[]:
    file = filedialog.asksaveasfile[initialdir="your directory path",
                                    defaultextension='.txt',
                                    filetypes=[
                                        ["Text file",".txt"],
                                        ["HTML file", ".html"],
                                        ["All files", ".*"],
                                    ]]
    if file is None:
        return
    # get the text
    filetext = str[text.get[1.0,END]]
    file.write[filetext]
    file.close[]

win = Tk[]
button = Button[text='save',command=saveFile]
button.pack[]
# create text box
text = Text[win]
text.pack[]
win.mainloop[]
5. Nhấn vào đây để đọc về các phương pháp khác

Làm cách nào để lấy tên tệp từ filedialog trong Python?

Giả sử chúng ta muốn lấy một chuỗi hoặc tên tệp mà chúng ta mở bằng hàm filedialog. Chúng ta có thể sử dụng tiện ích Nhãn để hiển thị tên tệp mà chúng ta sẽ mở bằng chức năng .

Vai trò của tkinter trong Python là gì?

Tkinter là cách thực tế trong Python để tạo giao diện người dùng đồ họa [GUIs] và được bao gồm trong tất cả các bản phân phối Python tiêu chuẩn. Trên thực tế, đó là khung duy nhất được tích hợp trong thư viện chuẩn Python.

Làm cách nào để cài đặt TTK trong Python?

Tuy nhiên, nếu lỡ cài đặt Tkinter trong khi cài đặt Python, chúng ta có thể thực hiện sau bằng cách sử dụng lệnh pip. .
Bước 1 - Đảm bảo Python và pip được cài đặt sẵn trên hệ thống của bạn. Nhập các lệnh sau vào dấu nhắc lệnh để kiểm tra xem python và pip đã được cài đặt trên hệ thống của bạn chưa. .
Bước 2 - Cài đặt Tkinter

Làm cách nào để mở tệp bằng Python bằng Tkinter?

Tóm tắt. trong hướng dẫn này, bạn sẽ học cách hiển thị hộp thoại tệp đang mở trong ứng dụng Tkinter. .
Sử dụng hàm askopenfilename[] để hiển thị hộp thoại mở tệp cho phép người dùng chọn một tệp
Sử dụng hàm askopenfilenames[] để hiển thị hộp thoại mở tệp cho phép người dùng chọn nhiều tệp

Chủ Đề