본문 바로가기

파이썬/수업내용

[Python/PyQt] PyQt 로또 출력하기

문제

로또 번호 출력하기


풀이

pyqt05.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>526</width>
    <height>212</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLCDNumber" name="ln1">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>71</width>
      <height>61</height>
     </rect>
    </property>
    <property name="digitCount">
     <number>2</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="ln3">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>10</y>
      <width>71</width>
      <height>61</height>
     </rect>
    </property>
    <property name="digitCount">
     <number>2</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="ln5">
    <property name="geometry">
     <rect>
      <x>340</x>
      <y>10</y>
      <width>71</width>
      <height>61</height>
     </rect>
    </property>
    <property name="digitCount">
     <number>2</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="ln4">
    <property name="geometry">
     <rect>
      <x>260</x>
      <y>10</y>
      <width>71</width>
      <height>61</height>
     </rect>
    </property>
    <property name="digitCount">
     <number>2</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="ln6">
    <property name="geometry">
     <rect>
      <x>420</x>
      <y>10</y>
      <width>71</width>
      <height>61</height>
     </rect>
    </property>
    <property name="digitCount">
     <number>2</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="ln2">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>10</y>
      <width>71</width>
      <height>61</height>
     </rect>
    </property>
    <property name="digitCount">
     <number>2</number>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>80</y>
      <width>471</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>로또 생성하기</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

 

pyqt05.py

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from random import shuffle, random

form_class = uic.loadUiType("./pyqt05.ui")[0] #로드할 UI파일 


class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__() # 상위 클래스인 QtWidgets.QMainWindow의 초기화
        self.setupUi(self)  # UI 파일을 현재 위도우에 설정
        self.show()# 윈도우를 표시
        self.pb.clicked.connect(self.lotto) #클릭되는 이벤트

    def lotto(self):
        arr = [1,2,3,4,5,   6,7,8,9,10,
               11,12,13,14,15,  16,17,18,19,20,
               21,22,23,24,25,  26,27,28,29,30,
               31,32,33,34,35,  36,37,38,39,40,
               41,42,43,44,45]
        
        for i in range(1000):
            rnd = int(random()*45)   
            a= arr[0]
            arr[0] = arr[rnd]
            arr[rnd] = a

        self.ln1.display(arr[0])
        self.ln2.display(arr[1])
        self.ln3.display(arr[2])
        self.ln4.display(arr[3])
        self.ln5.display(arr[4])
        self.ln6.display(arr[5])
 
if __name__ == "__main__": # java의 main 메소드와 같음
    app = QApplication(sys.argv) # 애플리케이션 초기화
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()
    # app.exec_() => 이벤트 루프 실행, 애플리케이션 실행

 


결과


 

 

Point

 

 

이전과 다르게 출력이 되어야 하는 부분이 다르기 때문에 display로 작성해야한다.