import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk[]
my_w.geometry["400x300"] # Size of the window
my_w.title['www.plus2net.com']
my_font1=['times', 18, 'bold']
l1 = tk.Label[my_w,text='Upload File & read',width=30,font=my_font1]
l1.grid[row=1,column=1]
b1 = tk.Button[my_w, text='Upload File',
width=20,command = lambda:upload_file[]]
b1.grid[row=2,column=1]
def upload_file[]:
file = filedialog.askopenfilename[]
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
my_w.mainloop[] # Keep the window open
2 tạo hộp thoại giao diện gốc, theo phương thức để người dùng chọn và tải tệp lên từ hệ thống cục bộ. import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import askopenfile
Hiển thị trình duyệt tệp để tải lên đường dẫn tệp đã đọc trong cửa sổ Tkinter bằng hộp thoại tệp hỏiopenfilename
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk[]
my_w.geometry["400x300"] # Size of the window
my_w.title['www.plus2net.com']
my_font1=['times', 18, 'bold']
l1 = tk.Label[my_w,text='Upload File & read',width=30,font=my_font1]
l1.grid[row=1,column=1]
b1 = tk.Button[my_w, text='Upload File',
width=20,command = lambda:upload_file[]]
b1.grid[row=2,column=1]
def upload_file[]:
file = filedialog.askopenfilename[]
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
my_w.mainloop[] # Keep the window open
Đầu ra ở đây [dựa trên tệp đã tải lên]id,name,class,mark,gender
1,John Deo,Four,75,female
2,Max Ruin,Three,85,male
3,Arnold,Three,55,male
4,Krish Star,Four,60,female
5,John Mike,Four,60,female
Khi người dùng hủy cửa sổ tập tin
Sau khi mở trình duyệt tệp, người dùng có thể sử dụng nút hủy để đóng thao tác, để xử lý việc này, chúng tôi đã thêmimport tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk[]
my_w.geometry["400x300"] # Size of the window
my_w.title['www.plus2net.com']
my_font1=['times', 18, 'bold']
l1 = tk.Label[my_w,text='Upload File & read',width=30,font=my_font1]
l1.grid[row=1,column=1]
b1 = tk.Button[my_w, text='Upload File',
width=20,command = lambda:upload_file[]]
b1.grid[row=2,column=1]
def upload_file[]:
file = filedialog.askopenfilename[]
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
my_w.mainloop[] # Keep the window open
3 vào đoạn mã trên. Phần khác của mã sẽ hiển thị thông báo. def upload_file[]:
file = filedialog.askopenfilename[]
if file: # user selected one file
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
else: # user cancel the file browser window
print["No file chosen"]
tùy chọn. ban đầu
Chúng tôi có thể đặt thư mục ban đầu để hiển thị cho người dùng.file = filedialog.askopenfilename[
initialdir='D:\\my_data\\my_html\\',
filetypes=[["CSV files", ".csv"]]]
Tiêu đề
file = filedialog.askopenfilename[
initialdir='D:\\my_data\\my_html\\',
title='Upload to plus2net',
filetypes=[["CSV files", ".csv"]]]
loại tập tin
________số 8hiển thị đường dẫn tệp
Chúng ta có thể sử dụng một Nhãn với StringVar[] để hiển thị đường dẫn với tên của tệp đã chọn.Chọn nhiều tệp
Bằng cách sử dụng tùy chọnimport tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk[]
my_w.geometry["400x300"] # Size of the window
my_w.title['www.plus2net.com']
my_font1=['times', 18, 'bold']
l1 = tk.Label[my_w,text='Upload File & read',width=30,font=my_font1]
l1.grid[row=1,column=1]
b1 = tk.Button[my_w, text='Upload File',
width=20,command = lambda:upload_file[]]
b1.grid[row=2,column=1]
def upload_file[]:
file = filedialog.askopenfilename[]
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
my_w.mainloop[] # Keep the window open
4, người dùng có thể chọn nhiều tệp và đầu ra sẽ chứa một bộ với tất cả các tên tệp [ có đường dẫn ]. import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import askopenfile
1Đầu ra ở đâyimport tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import askopenfile
2Từ bộ dữ liệu này, chúng ta có thể sử dụng các phần tử bên trong tập lệnh của mình. Đọc và hiển thị nhiều hình ảnh bằng cách sử dụng askopenfilename »
sự khác biệt giữa askopenfile[] và askopenfilename[]
Hàm askopenfilename[] trả về tên tệp đã chọn. [đường dẫn dưới dạng chuỗi].Hàm askopenfile[] trả về đối tượng tệp đã mở ở chế độ đọc.
Trong mã trên, cả hai chức năng đều có thể được sử dụng và xem phần nhận xét của mã bên dưới để so sánh cả hai chức năng. Trong cả hai trường hợp đầu ra là như nhau.
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk[]
my_w.geometry["400x300"] # Size of the window
my_w.title['www.plus2net.com']
my_font1=['times', 18, 'bold']
l1 = tk.Label[my_w,text='Upload File & read',width=30,font=my_font1]
l1.grid[row=1,column=1]
b1 = tk.Button[my_w, text='Upload File',
width=20,command = lambda:upload_file[]]
b1.grid[row=2,column=1]
def upload_file[]:
file = filedialog.askopenfilename[]
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
my_w.mainloop[] # Keep the window open
0Đang chọn thư mục
Chúng tôi có thể sử dụng askdirectory[] để chọn bất kỳ thư mục nào trong hệ thống của bạn dưới dạng chuỗi. Ở đây chúng tôi đang mở một hộp thoại để duyệt và chọn một thư mục. Khi chọn thư mục, đường dẫn sẽ được hiển thị trên Nhãn. Người dùng cũng có thể thay đổi lựa chọn
Tkinter filedialog askdirectory[] để hiển thị cửa sổ hộp thoại để chọn thư mục và trả về đường dẫn dưới dạng chuỗi
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk[]
my_w.geometry["400x300"] # Size of the window
my_w.title['www.plus2net.com']
my_font1=['times', 18, 'bold']
l1 = tk.Label[my_w,text='Upload File & read',width=30,font=my_font1]
l1.grid[row=1,column=1]
b1 = tk.Button[my_w, text='Upload File',
width=20,command = lambda:upload_file[]]
b1.grid[row=2,column=1]
def upload_file[]:
file = filedialog.askopenfilename[]
fob=open[file,'r']
print[fob.read[]]
#file = filedialog.askopenfile[]
#print[file.read[]]
my_w.mainloop[] # Keep the window open
1Hiển thị cấu trúc thư mục và tệp bằng Treeview »
Dự án. Trình soạn thảo văn bản GUI
Trình soạn thảo văn bản với tất cả các thao tác xử lý tệp như Mới, Mở. , Save, Save As và Close để thêm hoặc cập nhật dữ liệu của tệp.Tkinter filedialog được sử dụng để hiển thị các hộp thoại xử lý tệp và Menu được sử dụng để thực thi các chức năng khác nhau để quản lý tệp.
Trình soạn thảo văn bản Tkinter để quản lý thao tác tệp »← Tkinter Hiển thị cấu trúc thư mục và tệp trong Treeview » Filedialog hiển thị dữ liệu trong Treeview » asksaveasfile[]» Tải lên và hiển thị tệp hình ảnh → ← Đăng ký Kênh YouTube của chúng tôi tại đây