본문 바로가기
Python/Python 기초

파이썬(QT) 로또 출력 예제

by 미눅스[멘토] 2023. 6. 28.
728x90
import sys
from PyQt5 import(uic)
from PyQt5.QtWidgets import QApplication, QMainWindow
from random import random

form_class = uic.loadUiType("myqt03.ui")[0]

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick)
    
    def myclick(self):
        arr = list(range(1,45+1))
        
        for i in range(100) :
            ran = int(random()*45)
            a = arr[0]
            arr[0] = arr[ran]
            arr[ran]=a
            
    
        print(arr)
        arr2 = arr[0:6]
        arr2.sort()
        # arr2 = sorted(arr[0:6])
        print(arr2)
 
 
        self.lbl1.setText(str(arr2[0]))
        self.lbl2.setText(str(arr2[1]))
        self.lbl3.setText(str(arr2[2]))
        self.lbl4.setText(str(arr2[3]))
        self.lbl5.setText(str(arr2[4]))
        self.lbl6.setText(str(arr2[5]))
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

결과

 


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>800</width>
    <height>600</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>81</x>
      <y>151</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl2">
    <property name="geometry">
     <rect>
      <x>137</x>
      <y>151</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl3">
    <property name="geometry">
     <rect>
      <x>193</x>
      <y>151</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl4">
    <property name="geometry">
     <rect>
      <x>248</x>
      <y>151</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl5">
    <property name="geometry">
     <rect>
      <x>304</x>
      <y>151</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl6">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>151</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>__</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>210</y>
      <width>341</width>
      <height>51</height>
     </rect>
    </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>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>