[Python] QR Code 생성기 앱

Legday_Dev·2024년 3월 2일
0

Python

목록 보기
2/3
post-thumbnail

QR Code 생성기 앱을 만들기 위해선 qrcode 모듈을 pip로 설치받아야 한다.
명령어 : pip install qrcode

UI 구현


  • Qt Designer 를 통해 UI 를 구현 하였다. XML 파일로 저장이 된다.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>320</width>
    <height>390</height>
   </rect>
  </property>
  <property name="maximumSize">
   <size>
    <width>320</width>
    <height>390</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>QR Code Generator</string>
  </property>
  <widget class="QLabel" name="lblQrCode">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>300</width>
     <height>300</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: white;
</string>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLineEdit" name="txtQrData">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>320</y>
     <width>301</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="btnGenerate">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>350</y>
     <width>111</width>
     <height>41</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>D2Coding</family>
    </font>
   </property>
   <property name="text">
    <string>QR 코드 생성</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

코드설명


  • qrcode 모듈을 import 한다.
  • qrApp 클래스에서 initUI 함수로 윈도우창을 띄우고 btnGenerateClicked() 함수로 QR 코드를 생성한다.
  • initUI() 함수에서는 Qt Designer 로 만든 UI 창을 로드하고, 제목을 QR Code Generator 라고 설정한다. 그리고 btnGenerate 이름을 가진 버튼을 눌렀을 때 QR Code 를 생성하는 함수인 btnGenerateClicked() 함수를 호출하게 한다.
  • btnGenerateClicked() : QR Code 를 생성하는 로직이 구현되어있다.
    • txtQrData 라는 위젯에 링크를 입력하면 text() 함수로 문자열로 변경 후 data 변수에 저장
    • data 의 길이가 0이면 알림창을 띄우고 함수를 종료
    • data 가 있다면 qr 코드 이미지를 저장하기 위한 경로를 설정해준다.
    • 그리고 qrcode 모듈의 make 함수에 data를 넣어줘서 이미지를 생성하고 저장한다.
    • 그리고 lblQrCode 위젯에 qr코드 이미지를 띄어준다.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import qrcode 


class qrApp(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.initUI()

    def initUI(self):
        uic.loadUi("./day07/qrApp.ui", self)
        self.setWindowTitle("QR Code Generator")
        self.btnGenerate.clicked.connect(self.btnGenerateClicked)
        self.show()

    def btnGenerateClicked(self):
        data = self.txtQrData.text()

        if len(data.strip()) == 0:
            QMessageBox.about(self, "경고", "뭐함? 데이터 안넣음?")
            return
        else:
            imgPath = "./day07/qr.png"
            qrImage = qrcode.make(data)
            qrImage.save("./day07/qr.png")
            img = QPixmap(imgPath)
            self.lblQrCode.setPixmap(QPixmap(img).scaledToWidth(300))

    def closeEvent(self, QCloseEvent) -> None:
        re = QMessageBox.question(
            self, "종료", "종료 하시겠습니까?", QMessageBox.Cancel | QMessageBox.Yes
        )
        if re == QMessageBox.Yes:
            QCloseEvent.accept()
        else:
            QCloseEvent.ignore()


app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/qrCode.png"))
instance = qrApp()
app.exec_()

실행화면


profile
백엔드개발자

0개의 댓글