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ápHộ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íchfrom 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[]
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ưuChú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 tinTrong 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[]
4from 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
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[]
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[]
5Phươ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àyfrom 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[]
0Sử 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
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 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[]
2Bạ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[]
01Chú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