[Python/PyQt] PyQt 로또 생성하기

leesoyeon·2023년 7월 7일
0

PyQt

목록 보기
4/4

📌 PyQt 로또 예제

📃 문제

'로또생성하기' 버튼을 눌렀을 때 중복되지 않는 랜덤의 6개 숫자를 출력하시오.


📁 myqt03.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>350</width>
    <height>282</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="lbl1">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>70</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl2">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>70</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl3">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>70</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl4">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>70</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl5">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>70</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl6">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>70</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>140</y>
      <width>161</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>둥근모꼴</family>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="text">
     <string>로또생성하기</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>350</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

📁 myqt03.py

from PyQt5 import QtWidgets, uic
import sys
from random import random, shuffle
form_window = uic.loadUiType("myqt03.ui")[0]

class UiMainWindow(QtWidgets.QMainWindow, form_window):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()
        self.pb.clicked.connect(self.lotto)

    def lotto(self):
        arr = list(range(1, 45+1))
        shuffle(arr)
		
        # 방식 1
        self.lbl1.setText(str(arr[0]))
        self.lbl2.setText(str(arr[1]))
        self.lbl3.setText(str(arr[2]))
        self.lbl4.setText(str(arr[3]))
        self.lbl5.setText(str(arr[4]))
        self.lbl6.setText(str(arr[5]))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = UiMainWindow()
    sys.exit(app.exec_())

라벨에 값을 셋팅해주는 코드를 아래와 같이 작성하는 것도 가능하다.

# 방식 2
for i in range(0, 6):
            lblName = "lbl" + str(i+1)
            lbl = getattr(self, lblName)
            lbl.setText(str(arr[i]))

더 명확하고 직관적인 코드를 원한다면 방식 1 추천,
더 간결하고 유연한 코드를 원한다면 방식 2 추천.


💻 결과


💡 POINT

여러 개의 유사한 속성을 가진 경우에 반복문을 사용하여 동일한 작업을 수행해야 할 경우가 있는데, 이러한 경우에서 getattr() 함수를 사용하여 속성에 접근하면 중복을 줄일 수 있다.

0개의 댓글

관련 채용 정보