QRcode Generator by Python

ผู้เขียนบทความ : นายเขมินท์ อนุวรรค Coe#12

1. ความเป็นมา

เนื่องจากผู้จัดทำมีความสนใจในเรื่อง QRcode เลยอยากลองหาความรู้และ พัฒนาต่อยอดจากบทความที่ผู้จัดทำเรียนรู้มา จึงได้มาเป็นโปรแกรม QRcode Generator ผู้จัดทำหวังว่า ทุกๆท่านจะสามารถนำไปต่อยอดได้อีกและหวังว่าจะมีประโยชน์ต่อ ทุกๆท่านไม่มากก็น้อย

2. วัตถุประสงค์

2.1 เพื่อศึกษาหาความรู้ในการใช้ภาษา Python
2.2 เพื่อพัฒนาโปรแกรม Qrcode Generator ไปประยุกต์ใช้ได้จริง
2.3 เพื่อนำความรู้ที่ได้ไปใช้ต่อยอดในการศึกษาและการทำงาน

3. ขอบเขต

3.1 โปรแกรมสามารถรองรับการทำงานบน Visaul Studio Code ได้
3.2 โปรแกรมสามารถแสดงผลและบันทึก Qrcode generator ได้

4. ประโยชน์ที่คาดว่าจะได้รับ

4.1 ได้ความรู้ในการนำภาษา python มาประยุกต์ใช้งาน
4.2 ได้ความรู้เกี่ยวกับโมดูลและไลบรารีต่างๆที่เกี่ยวกับการสร้าง QRcode

5. ความรู้ที่เกี่ยวข้อง

5.1 พื้นฐานในการใช้งานภาษา python ทั่วไป


5.2 ความรู้เกี่ยวกับ Tkinter เพื่อใช้ในการสร้าง GUI

Tkinter คือ utility ที่นิยมใช้มากที่สุดเป็นหนึ่งในวิธีที่เร็วและง่ายที่สุดในการสร้างแอปพลิเคชัน GUI ยิ่งไปกว่านั้น Tkinter ยังข้ามแพลตฟอร์มดังนั้นรหัสเดียวกันจึงทำงานบน macOS, Windows และ Linux


5.3 ความรู้เกี่ยวกับโมดูลต่างๆที่ใช้ในการสร้าง QRcode ออกมา

5.3.1 PyPNG คือ packages เสริมใน Python ซึ่ง PyPNG อนุญาตให้อ่านและเขียนไฟล์ภาพ PNG โดยใช้ Python ล้วนๆ
5.3.2 Pillow ซึ่งเป็น image processing and graphics capabilities หรือโมดูลจัดการและประมวลผลรูปภาพบน Python
5.3.3 PyQRcode เป็นโมดูลที่เอาไว้สร้างตัว QRcode ขึ้นมา
โดยคำสั่งที่ใช้บน Python ได้แก่

pip install Pillow
pip install pyqrcode
pip install pypng
pip install tkinter

6. ผลการดำเนินงาน

ระยะที่ 1 ศึกษาหาความรู้เกี่ยวกับ QRcode
ระยะที่ 2 เป็นการเขียนโปรแกรมสร้าง QRcode Generator มีดังนี้


ขั้นตอนที่ 1 นำเข้าโมดูลที่จำเป็น
เราต้องนำเข้าโมดูล Tkinter pyqrcode pypng และ pillow
code :

pip install Pillow
pip install pyqrcode
pip install pypng
pip install tkinter

ขั้นตอนที่ 2 สร้างหน้าต่าง GUI
ขั้นตอนนี้เราจะสร้างหน้าต่างสำหรับ QRcode Generator
code:

from tkinter import *
gui = Tk()
gui.title("QR code generator")
gui.option_add("*Font", "consolas 20")
Label(gui, text="Write anything").grid(row=0, column=0)
txt = Text(gui, height=4, width=30, fg="green")
txt.insert(END, "")
txt.grid(row=1, column=0)
btn = Button(gui, text="create QR Code", bg="gold")
btn.grid(row=2, column=0)
btn.bind("", on_click)
qr = Label(gui)
qr.grid(row=0, column=1, rowspan=3)
gui.mainloop()

ขั้นตอนที่ 3 การนำโมดูลมาใช้ ใน GUI ที่สร้างไว้
code :

from tkinter import *
import qrcode
from PIL import ImageTk
def create_qrcode(text):
qr = qrcode.QRCode()
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color="white", back_color="black")
return img
def demo():
def on_click(e):
input_text = txt.get("0.0", "end-1c")
print(input_text)
img = create_qrcode(input_text).resize((250, 250))
imgTk = ImageTk.PhotoImage(img)
qr.configure(image=imgTk)
qr.image = imgTk
gui = Tk()
gui.title("QR code generator")
gui.option_add("*Font", "consolas 20")
Label(gui, text="Write anything").grid(row=0, column=0)
txt = Text(gui, height=4, width=30, fg="green")
txt.insert(END, "")
txt.grid(row=1, column=0)
btn = Button(gui, text="create QR Code", bg="gold")
btn.grid(row=2, column=0)
btn.bind("", on_click)
qr = Label(gui)
qr.grid(row=0, column=1, rowspan=3)
gui.mainloop()
if name == 'main':
demo()

ระยะที่ 3 ผลการทดลอง
เมื่อรันโปรแกรม QRcode Generator ก็จะได้หน้าต่างนี้ขึ้นมา

7. สรุปผลและข้อเสนอแนะ

ตัวโปรแกรมเป็นไปตามที่ผู้ศึกษาต้องการ สามารถใช้งานได้ปกติ แสดงผลได้อย่างถูกต้อง และตัวโปรแกรมสามารถนำไปพัฒนาต่อไปได้อีก สามารถนำไปเพิ่มฟังก์ชั่นต่างๆ ได้อีก

8. ข้อมูลอ้างอิง

-website ที่ใช้ในการศึกษาอ้างอิง
https://github.com/ShivamPandya/python-qr-generator/blob/master/qr.py
https://www.youtube.com/watch?v=ErzISUgrELk
https://codingshiksha.com/python/python-tkinter-gui-script-to-make-a-qr-code-generator-desktop-app-using-pyqrcode-library-full-project-for-beginners/

บทความที่เกี่ยวข้องกัน

Share

You may also like...

Leave a Reply