본문 바로가기

Python

[QT] 가위바위보 게임

태그객체에 따라 text(), setText() 일 수도 있고 toPlainText()일수도있다.

실행해보고 오류 나면 검색해서 알맞게 수정해 사용하면 된다.

 

myqt06.py

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.btnClick)
        
    def btnClick(self):
        mine = self.pte_mine.toPlainText()
        c = ["가위","바위","보"]
        rnd = int(random()*3)
        com = c[rnd]
        res = ""
        if mine == com:
            res = "비김"
        elif (mine,com) in [("가위","바위"),("바위","보"),("보","가위")]:
            res = "패배"
        elif (mine,com) in [("가위","보"),("바위","가위"),("보","바위")]:
            res = "승리"
        else:
            res = "잘못된 입력값"
        self.pte_com.setPlainText(com)
        self.pte_result.setPlainText(res)

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

myqt.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>30</y>
      <width>61</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>80</y>
      <width>61</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>컴:</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_result">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>130</y>
      <width>61</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>결과:</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte_mine">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>30</y>
      <width>151</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte_com">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>80</y>
      <width>151</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte_result">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>130</y>
      <width>151</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>180</y>
      <width>201</width>
      <height>41</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>

'Python' 카테고리의 다른 글

[QT] 야구 게임  (0) 2023.06.29
[QT] 별찍기  (0) 2023.06.29
[QT] 전화기 만들기  (0) 2023.06.28
[QT] 구구단 출력하기  (0) 2023.06.28
[QT] 로또 만들기  (0) 2023.06.28