Python GUI (Tkinter) : การ Browse file แบบ multiple file จากการกดปุ่ม Button

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: Python GUI (Tkinter) : การ Browse file แบบ multiple file จากการกดปุ่ม Button

Python GUI (Tkinter) : การ Browse file แบบ multiple file จากการกดปุ่ม Button

โดย benzas00123 » 30/01/2020 5:46 pm

หลังจากที่เราได้ทำการศึกษา การใช้งาน Python GUI (Tkinter) : การ Browse file (การค้นหาไฟล์) จากการกดปุ่ม Butto Browse file แบบ ทีละไฟล์มาแล้ว จาก การใช้งาน Python GUI (Tkinter) : การ Browse file (การค้นหาไฟล์) จากการกดปุ่ม Button[/url] วันนี้เราจะมาลองเขียนให้สามารถ เลือกได้ทีละหลาย File กันครับ โดยใช่ Tkinter เขียนด้วยภาษา python โดยวิธีเขียนจะมีดังนี้

1.ทำการ Import library

โค้ด: เลือกทั้งหมด

from tkinter import *
from tkinter import filedialog 
2.สร้างฟังก์ชั่นไว้สำหรับเรียกไฟล์

โค้ด: เลือกทั้งหมด

def OpenFile():
    name = filedialog.askopenfilenames(initialdir="",
                           filetypes =(("Python", "*.py"),("All Files","*.*")),
                           title = "Choose a file."
                           )
    print (name)
3.สร้าง หน้า Gui และปุ่ม

โค้ด: เลือกทั้งหมด

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-1.png
Python Knowledge-1.png (5.88 KiB) Viewed 1882 times
ผลลัพธ์
Python Knowledge-2.png
Python Knowledge-2.png (21.37 KiB) Viewed 1882 times
Python Knowledge-3.png
Python Knowledge-3.png (6.83 KiB) Viewed 1882 times
ค่าที่แสดงออกมาจะเป็น Tuple และ และจะมีค่าเป็นตำแหน่งของไฟล์ + ชื่อไฟล์

เป็นอย่างไรบ้างครับกับ ฟังก์ชั่นในวันนี้หวังว่าหลายๆคนที่กำลังศึกษาหรือกำลังทำโปรเจคที่เกี่ยวกับการอัพโหลดพวก ไฟล์ข้อมูลหรือ ไฟล์รูป หวังว่าคงจะมีประโยชน์บ้างไม่มากก็น้อยนะครับ

อ้างอิง
https://stackoverflow.com/questions/16790328/open-multiple-filenames-in-tkinter-and-add-the-filesnames-to-a-list
https://gist.github.com/vperezma/85202b860ee359c23d37ab5331121fc6
viewtopic.php?f=144&t=45907

ศึกษาเพิ่มเติม

วีดีโอสอน python
python แชร์ความรู้
บทความที่มีประโยชน์เกี่ยวกับ python
การใช้งาน Python GUI (Tkinter) : การ Browse file (การค้นหาไฟล์) จากการกดปุ่ม Button

ข้างบน