ในบางครั้งภายใน
Folder ของเรานั้นอาจจะมีรูปภาพเป็น ร้อยๆรูป แต่เราอาจจะอยากจะให้มันโชวน์ แค่รูปบางประเภทเท่านั้น เราสามารถกำหนดได้ใน filetypes โดยเราสามารถกำหนดให้โชวน์ได้ ทีละนามสกุล หรือทีละ 2-3 นามสกุลพร้อมกันก็ได้ โดยวิธีการเขียนจะมีดังนี้
1.
Import library
Code: Select all
from tkinter import *
from tkinter import filedialog
2.สร้างฟังก์ชั่น ที่ใช้สำหรับการเปิดไฟล์ โดยเราจะกำหนดนามสกุลที่ต้องการ ใน filetypes
Code: Select all
def OpenFile():
name = filedialog.askopenfilenames(initialdir="",
filetypes =(("PNG Files", "*.png")("All Files","*.*")),
title = "Choose a file."
)
print (name)
3.สร้าง GUI
Code: Select all
root = Tk()
root.geometry('200x200')
root.title("Test")
btn = Button ( root, text='Upload', command=lambda: OpenFile() , font= 10 )
btn.place(x = 50 , y= 100)
root.mainloop()
ผลลัพธ์ของโปรแกรม

- Python Knowledge-2.png (4.84 KiB) Viewed 397 times

- Python Knowledge-1.png (18.55 KiB) Viewed 397 times
เราสามารถกำหนดตัวนามสกุลให้แสดงได้ทีละ 2 นามสกุลด้วยการ เพิ่มนามสกุลต่อตามหลังนามสกุลเก่า
Code: Select all
name = filedialog.askopenfilenames(initialdir="",
filetypes =(("PNG and JPG Files", "*.png *jpg"),("All Files","*.*")),
title = "Choose a file."
)

- Python Knowledge-1.png (20.79 KiB) Viewed 397 times
เราสามารถพลิกแพลงเรียกไฟล์นามสกุลที่เราต้องการและทำให้โปรแกรมของเรามีลูกเล่นที่น่าสนใจมากขึ้น เป็นอย่างไรบ้างครับ หวังว่าคงจะมีประโยชน์กับหลายๆคนนะครับ
อ้างอิง
https://www.programcreek.com/python/example/95887/tkinter.filedialog.askopenfilenames
https://stackoverflow.com/questions/44403566/add-multiple-extensions-in-one-filetypes-mac-tkinter-filedialog-askopenfilenam
https://books.google.co.th/books?id=45lUDwAAQBAJ&pg=PA86&lpg=PA86&dq=tkinter+filetype&source=bl&ots=eXh-uKEeSf&sig=ACfU3U3yVcxldoFPKOc8YZ-dwpfGltBN0A&hl=th&sa=X&ved=2ahUKEwj9gNfGlavnAhX0heYKHSudCoAQ6AEwB3oECAkQAQ#v=onepage&q=tkinter%20filetype&f=false
ศึกษาเพิ่มเติม
วีดีโอสอน Python
บทความที่น่าสนใจเกี่ยวกับ python
แชร์ความรู้เกี่ยวกับ python
การใช้งาน Python GUI (Tkinter) : variations on Hello world
การใช้งาน Python GUI (Tkinter) : การ Browse file (การค้นหาไฟล์) จากการกดปุ่ม Button
ในบางครั้งภายใน [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/google-for-work/4705-folder-gmail-allies.html]Folder[/url] ของเรานั้นอาจจะมีรูปภาพเป็น ร้อยๆรูป แต่เราอาจจะอยากจะให้มันโชวน์ แค่รูปบางประเภทเท่านั้น เราสามารถกำหนดได้ใน filetypes โดยเราสามารถกำหนดให้โชวน์ได้ ทีละนามสกุล หรือทีละ 2-3 นามสกุลพร้อมกันก็ได้ โดยวิธีการเขียนจะมีดังนี้
1.[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python/2495-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89-from-import-modules.html]Import[/url] [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/239-it-technology/4952-libraly-framwork.html]library[/url]
[code]from tkinter import *
from tkinter import filedialog[/code]
2.สร้างฟังก์ชั่น ที่ใช้สำหรับการเปิดไฟล์ โดยเราจะกำหนดนามสกุลที่ต้องการ ใน filetypes
[code]def OpenFile():
name = filedialog.askopenfilenames(initialdir="",
filetypes =(("PNG Files", "*.png")("All Files","*.*")),
title = "Choose a file."
)
print (name)[/code]
3.สร้าง GUI
[code]root = Tk()
root.geometry('200x200')
root.title("Test")
btn = Button ( root, text='Upload', command=lambda: OpenFile() , font= 10 )
btn.place(x = 50 , y= 100)
root.mainloop()[/code]
ผลลัพธ์ของโปรแกรม
[attachment=1]Python Knowledge-2.png[/attachment]
[attachment=2]Python Knowledge-1.png[/attachment]
เราสามารถกำหนดตัวนามสกุลให้แสดงได้ทีละ 2 นามสกุลด้วยการ เพิ่มนามสกุลต่อตามหลังนามสกุลเก่า
[code] name = filedialog.askopenfilenames(initialdir="",
filetypes =(("PNG and JPG Files", "*.png *jpg"),("All Files","*.*")),
title = "Choose a file."
)[/code]
[attachment=0]Python Knowledge-1.png[/attachment]
เราสามารถพลิกแพลงเรียกไฟล์นามสกุลที่เราต้องการและทำให้โปรแกรมของเรามีลูกเล่นที่น่าสนใจมากขึ้น เป็นอย่างไรบ้างครับ หวังว่าคงจะมีประโยชน์กับหลายๆคนนะครับ
อ้างอิง
https://www.programcreek.com/python/example/95887/tkinter.filedialog.askopenfilenames
https://stackoverflow.com/questions/44403566/add-multiple-extensions-in-one-filetypes-mac-tkinter-filedialog-askopenfilenam
https://books.google.co.th/books?id=45lUDwAAQBAJ&pg=PA86&lpg=PA86&dq=tkinter+filetype&source=bl&ots=eXh-uKEeSf&sig=ACfU3U3yVcxldoFPKOc8YZ-dwpfGltBN0A&hl=th&sa=X&ved=2ahUKEwj9gNfGlavnAhX0heYKHSudCoAQ6AEwB3oECAkQAQ#v=onepage&q=tkinter%20filetype&f=false
ศึกษาเพิ่มเติม
[url=https://www.mindphp.com/vdo-tutorial-python.html]วีดีโอสอน Python[/url]
[url=https://www.mindphp.com/developer/tips-python.html]บทความที่น่าสนใจเกี่ยวกับ python[/url]
[url=https://www.mindphp.com/forums/viewforum.php?f=144]แชร์ความรู้เกี่ยวกับ python[/url]
[url=https://www.mindphp.com/forums/viewtopic.php?f=144&t=45435]การใช้งาน Python GUI (Tkinter) : variations on Hello world[/url]
[url=https://www.mindphp.com/forums/viewtopic.php?f=144&t=45907]การใช้งาน Python GUI (Tkinter) : การ Browse file (การค้นหาไฟล์) จากการกดปุ่ม Button[/url]