본문 바로가기

파이썬/과제

[Python]24.06.28

문제 

GUI를 이용하여 홀짝 게임 만들기 

풀이 

<?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>261</width>
    <height>216</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>30</x>
      <y>40</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>나 :</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_com">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>80</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>컴 :</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_result">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>120</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>결과 :</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_mine">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>40</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_com">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>80</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_result">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>120</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>160</y>
      <width>181</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>결과보기</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from random import random
from tkinter.constants import SEL

form_class = uic.loadUiType("./pyqt06.ui")[0] #로드할 UI파일 


class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__() # 상위 클래스인 QtWidgets.QMainWindow의 초기화
        self.setupUi(self)  # UI 파일을 현재 위도우에 설정
        self.show()# 윈도우를 표시
        self.pb.clicked.connect(self.myclick) #클릭되는 이벤트
        self.le_mine.returnPressed.connect(self.myclick) #엔터 이벤트
        

    def myclick(self):
        mine = self.le_mine.text()
        com = ""
        result = ""
        
        rnd = random()
        
        if rnd > 0.5 :
            com = "홀"
        else : 
            com = "짝"
        if com == mine :
            result = "이김"
        else :
            result = "짐"
        print("나:{}".format(mine))
        print("컴:{}".format(com))
        print("결과:{}".format(result))
        
        self.le_mine.setText(mine)
        self.le_com.setText(com)
        self.le_result.setText(result)
                

if __name__ == "__main__": # java의 main 메소드와 같음
    app = QApplication(sys.argv) # 애플리케이션 초기화
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()
    # app.exec_() => 이벤트 루프 실행, 애플리케이션 실행

결과 


문제 

GUI를 이용하여 전화번호 누르고  alert창 보여주기

풀이 

<?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>298</width>
    <height>388</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLineEdit" name="le">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>30</y>
      <width>231</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb1">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>70</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>1</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb2">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>70</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>2</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb3">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>70</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>3</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb4">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>130</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>4</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb5">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>130</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>5</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb6">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>130</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>6</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb7">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>190</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>7</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb8">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>190</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>8</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb9">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>190</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>9</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb0">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>250</y>
      <width>71</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>0</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb_call">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>250</y>
      <width>151</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>☏</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

 

방법1

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.Qt import QMessageBox

form_class = uic.loadUiType("./pyqt07.ui")[0] #로드할 UI파일 


class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__() # 상위 클래스인 QtWidgets.QMainWindow의 초기화
        self.setupUi(self)  # UI 파일을 현재 위도우에 설정
        self.show()# 윈도우를 표시
        self.pb1.clicked.connect(self.myclcik) 
        self.pb2.clicked.connect(self.myclcik) 
        self.pb3.clicked.connect(self.myclcik) 
        self.pb4.clicked.connect(self.myclcik) 
        self.pb5.clicked.connect(self.myclcik) 
        self.pb6.clicked.connect(self.myclcik) 
        self.pb7.clicked.connect(self.myclcik) 
        self.pb8.clicked.connect(self.myclcik) 
        self.pb9.clicked.connect(self.myclcik) 
        self.pb0.clicked.connect(self.myclcik) 

        self.pb_call.clicked.connect(self.mycall) 

    def mycall(self):
        print("mycall")
        str_num = self.le.text()
        QMessageBox.about(self,'Calling',str_num)
        
    def myclcik(self):
        str_new = self.sender().text()
        str_old = self.le.text()
        self.le.setText(str_old+str_new)
        
        print(self.sender().text())

if __name__ == "__main__": # java의 main 메소드와 같음
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) # 애플리케이션 초기화
    #WindowClass의 인스턴스 생성
    myWindow = WindowClass()
    #프로그램 화면을 보여주는 코드
    myWindow.show()
    # app.exec_() => 이벤트 루프 실행, 애플리케이션 실행
    app.exec_()

 

방법2

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.Qt import QMessageBox
from tkinter.constants import SEL

form_class = uic.loadUiType("./pyqt07.ui")[0] #로드할 UI파일 


class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__() # 상위 클래스인 QtWidgets.QMainWindow의 초기화
        self.setupUi(self)  # UI 파일을 현재 위도우에 설정
        self.show()# 윈도우를 표시
        
        #self는 스크립트에 this와 같다.
        self.pb1.clicked.connect(self.myclcik) 
        self.pb2.clicked.connect(self.myclcik) 
        self.pb3.clicked.connect(self.myclcik) 
        self.pb4.clicked.connect(self.myclcik) 
        self.pb5.clicked.connect(self.myclcik) 
        self.pb6.clicked.connect(self.myclcik) 
        self.pb7.clicked.connect(self.myclcik) 
        self.pb8.clicked.connect(self.myclcik) 
        self.pb9.clicked.connect(self.myclcik) 
        self.pb0.clicked.connect(self.myclcik) 
        self.pb_call.clicked.connect(self.mycall) 
    def mycall(self):
        str_tel = self.le.text()
        QMessageBox.about(self,'Calling',str_tel)
    
    def myclcik(self):
        str_new = self.sender().text()# sender() : this와 같다. 
        str_old = self.le.text()
        self.le.setText(str_old+str_new)

if __name__ == "__main__": # java의 main 메소드와 같음
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) # 애플리케이션 초기화
    #WindowClass의 인스턴스 생성
    myWindow = WindowClass()
    #프로그램 화면을 보여주는 코드
    myWindow.show()
    # app.exec_() => 이벤트 루프 실행, 애플리케이션 실행
    app.exec_()

결과

 

'파이썬 > 과제' 카테고리의 다른 글

[Python] 24.07.01 야구게임  (0) 2024.07.01
[Python] 24.06.27 과제  (0) 2024.06.27
[Python] 24.06.26 과제  (0) 2024.06.27
[python] 24.06.25 과제  (0) 2024.06.26
[24.06.26 과제]  (0) 2024.06.26