본문 바로가기
Python/Python 기초

파이썬(QT) 가위바위보 프로그램 만들기 예제

by 미눅스[멘토] 2023. 6. 29.
728x90

 

 

 

import sys
from PyQt5 import(uic)
from PyQt5.QtWidgets import QApplication, QMainWindow
from random import random

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

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick)
    
    def myclick(self):
        me = self.pte_mine.toPlainText()
        arr = ["가위","바위","보"]
        ran = int(random()*3)
        com = arr[ran]
        print(com)
        self.pte_com.setPlainText(com);
        
        if me==com:
            self.pte_result.setPlainText("비겼습니다.");
        elif (me=="가위" and com=="보")or (me=="바위" and com=="가위")or (me=="보" and com=="바위") :
            self.pte_result.setPlainText("이겼습니다!.")
        else:
            self.pte_result.setPlainText("졌습니다.!.")
        
        
        

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="lbl_mine">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>100</y>
      <width>121</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>나</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_com">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>160</y>
      <width>121</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>com</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_result">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>230</y>
      <width>121</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>결과</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte_mine">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>100</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte_com">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>160</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte_result">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>230</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>290</y>
      <width>231</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>