PYTHON

PYQT 가위바위보 게임 + QLineEdit 엔터이벤트주기

9400 2023. 1. 2. 19:53

 

<?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="le_mine">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>30</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_com">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>70</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_result">
    <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("main06.ui")[0]


class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        
        self.pb.clicked.connect(self.myclick)
        self.le_mine.returnPressed.connect(self.myclick)
        
    def myclick(self):
        #가위바위보
        mine = self.le_mine.text()
        
        rnd = random.random()
        print(rnd)
        
        if rnd>0.66:
            com="가위"
        elif rnd>0.33:
            com="바위"
        else:
            com="보"
        
        #결과 출력
        print(com)
        
        if mine=="가위" and com=="가위":
            result="비겼음"
        if mine=="가위" and com=="바위":
            result="졌음"
        if mine=="가위" and com=="보":
            result="이겼음"
        
        if mine=="바위" and com=="바위":
            result="비겼음"
        if mine=="바위" and com=="보":
            result="졌음"
        if mine=="바위" and com=="가위":
            result="이겼음"
       
        if mine=="보" and com=="보":
            result="비겼음"
        if mine=="보" and com=="가위":
            result="졌음"
        if mine=="보" and com=="바위":
            result="이겼음" 
       
            
        self.le_com.setText(com)
        self.le_result.setText(result)
        

if __name__ == "__main__" :
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) 
    #WindowClass의 인스턴스 생성
    myWindow = WindowClass() 
    #프로그램 화면을 보여주는 코드
    myWindow.show()
    #프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
    app.exec_()

출력성공

 

 

class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        
        self.pb.clicked.connect(self.myclick)
        self.le_mine.returnPressed.connect(self.myclick)

단 QLineEdit 에서

가위/바위/보 입력 후 엔터시 

가위바위보가 실행되고 싶으면(myclick메서드 실행)

self.le_mine.returnPressed.connect(self.myclick)

코드를 넣어줘야함