카테고리 없음

PYQT 홀짝 맞추기

9400 2023. 1. 2. 19:45

<?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>748</width>
    <height>602</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>60</x>
      <y>10</y>
      <width>121</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>나</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>200</y>
      <width>181</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>게임하기</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl2">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>50</y>
      <width>121</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>컴</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl3">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>120</y>
      <width>121</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>결과</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lemine">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>30</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="lecom">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>70</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="leresult">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>130</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>748</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

 

 

 

코드

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import random

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


class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        
        self.pb.clicked.connect(self.myclick)
        
    def myclick(self):
        #홀짝
        mine = self.lemine.text()
        
        rnd = random.random()
        print(rnd)
        
        if rnd>0.5:
            com="홀"
        else:
            com="짝"
        
        
        if mine==com:
            result="유저가 이김"
        else:
            result="유저가 짐"
            
        self.lecom.setText(com)
        self.leresult.setText(result)
 
if __name__ == "__main__" :
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) 
    #WindowClass의 인스턴스 생성
    myWindow = WindowClass() 
    #프로그램 화면을 보여주는 코드
    myWindow.show()
    #프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
    app.exec_()

출력 성공