[파이썬] pyqt5 예제 5종 로또, 전화번호 전화기, 가위바위보, 별찍기, 야구게임

jychae·2022년 11월 3일
0

파이썬

목록 보기
3/8

main06.py 로또

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

#UI파일 연결
#단, UI파일은 Python 코드 파일과 같은 디렉토리에 위치해야한다.
form_class = uic.loadUiType("main06.ui")[0]

#화면을 띄우는데 사용되는 Class 선언
class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclcik) #파이썬 문법!
        
    def myclcik(self):
        print("myclcik")
        
        arr = [
            1,2,3,4,5,       6,7,8,9,10,
            11,12,13,14,15,  16,17,18,19,20,
            21,22,23,24,25,  26,27,28,29,30,
            31,32,33,34,35,  36,37,38,39,40,
            41,42,43,44,45
        ]
        
        print(arr[0])
        
        for i in range(1000):
            rnd = int(random.random()*45)
            a = arr[0]
            b = arr[rnd]
            arr[0]=b
            arr[rnd]=a    

        
        print(arr[:6])
        
        self.le1.setText(str(arr[0])) 
        self.le2.setText(str(arr[1])) 
        self.le3.setText(str(arr[2])) 
        self.le4.setText(str(arr[3])) 
        self.le5.setText(str(arr[4])) 
        self.le6.setText(str(arr[5])) 

if __name__ == "__main__" :
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) 

    #WindowClass의 인스턴스 생성
    myWindow = WindowClass() 

    #프로그램 화면을 보여주는 코드
    myWindow.show()

    #프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
    app.exec_()  

main06.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="QLineEdit" name="le1">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>70</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le2">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>70</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le3">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>70</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le4">
    <property name="geometry">
     <rect>
      <x>300</x>
      <y>70</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le5">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>70</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le6">
    <property name="geometry">
     <rect>
      <x>420</x>
      <y>70</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>130</y>
      <width>171</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>

main07.py 전화번호 전화기

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


#UI파일 연결
#단, UI파일은 Python 코드 파일과 같은 디렉토리에 위치해야한다.
form_class = uic.loadUiType("main07.ui")[0]

#화면을 띄우는데 사용되는 Class 선언
class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        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__" :
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) 

    #WindowClass의 인스턴스 생성
    myWindow = WindowClass() 

    #프로그램 화면을 보여주는 코드
    myWindow.show()

    #프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
    app.exec_()  

main07.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="QLineEdit" name="le">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>70</y>
      <width>201</width>
      <height>41</height>
     </rect>
    </property>
    <property name="layoutDirection">
     <enum>Qt::LeftToRight</enum>
    </property>
    <property name="alignment">
     <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
   </widget>
   <widget class="QPushButton" name="pb1">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>140</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>1</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb2">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>140</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>2</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb3">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>140</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>3</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb4">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>170</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>4</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb5">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>170</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>5</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb6">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>170</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>6</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb7">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>200</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>7</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb8">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>200</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>8</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb9">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>200</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>9</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb0">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>230</y>
      <width>61</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>0</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb_call">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>230</y>
      <width>131</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>CALL</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>

main08.py 가위바위보

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

#UI파일 연결
#단, UI파일은 Python 코드 파일과 같은 디렉토리에 위치해야한다.
form_class = uic.loadUiType("main08.ui")[0]

#화면을 띄우는데 사용되는 Class 선언
class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick) #파이썬 문법!
        
    def myclick(self):
        print("myclick")
        
        com =""
        mine = self.leMine.text()
        result =""
         #하나하나 단위 테스트 하면서 짜볼것!
        rnd = random.random()
        if rnd > 0.66:
            com = "가위"
        elif rnd > 0.33:
            com = "바위" 
        else:
            com = "보" 
        
        if com == "가위" and mine == "가위"  :  result = "비김";
        if com == "가위" and mine == "바위"  :  result = "이김";
        if com == "가위" and mine == "보"  :  result = "짐";
        
        if com == "바위" and mine == "가위"  :  result = "짐";
        if com == "바위" and mine == "바위"  :  result = "비김";
        if com == "바위" and mine == "보"  :  result = "이김";
        
        if com == "보" and mine == "가위"  :  result = "이김";
        if com == "보" and mine == "바위"  :  result = "짐";
        if com == "보" and mine == "보"  :  result = "비김";
        
        
        self.leCom.setText(com)    
        self.leResult.setText(result)    
        #print(com, mine, result) 

if __name__ == "__main__" :
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) 

    #WindowClass의 인스턴스 생성
    myWindow = WindowClass() 

    #프로그램 화면을 보여주는 코드
    myWindow.show()

    #프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
    app.exec_()  

main08.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="QLineEdit" name="leMine">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>90</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="leCom">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>130</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="leResult">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>180</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>90</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>나 :</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>140</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>컴 :</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>190</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>결과 :</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>230</y>
      <width>171</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>

main09.py 별찍기

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


#UI파일 연결
#단, UI파일은 Python 코드 파일과 같은 디렉토리에 위치해야한다.
form_class = uic.loadUiType("main09.ui")[0]

#화면을 띄우는데 사용되는 Class 선언
class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick)
    
    def star(self,cnt):
        ret = ""
        for i in range(cnt):
            ret += "*"
        ret += "\n"
        return ret    
                           
    def myclick(self):
        print("myclick")
        a = self.le_f.text() #getText()하면 죽음
        b = self.le_l.text()
        
        aa = int(a)
        bb = int(b)
        # print(aa,bb)
    
        #1,2라고 치면
        # *
        # **
        
        # res=""
        # for i in range(aa,bb+1) :
        #     res += "*" * i + "\n"
        #     self.pte.setText(res)
        
        txt =""
        
        #txt = "*\n"
        #txt = "**\n"
        
        #txt += self.star(1)
        
        for i in range(aa, bb+1):
            txt += self.star(i)
        
        self.pte.setPlainText(txt)

if __name__ == "__main__" :
    #QApplication : 프로그램을 실행시켜주는 클래스
    app = QApplication(sys.argv) 

    #WindowClass의 인스턴스 생성
    myWindow = WindowClass() 

    #프로그램 화면을 보여주는 코드
    myWindow.show()

    #프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
    app.exec_()  

main09.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>485</width>
    <height>350</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>40</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>첫별수 :</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>70</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>끝별수 :</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_f">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>30</y>
      <width>41</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_l">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>60</y>
      <width>41</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>90</y>
      <width>121</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>별그리기</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>120</y>
      <width>121</width>
      <height>151</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>485</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

main10.py 야구게임

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

#UI파일 연결
#단, UI파일은 Python 코드 파일과 같은 디렉토리에 위치해야한다.
form_class = uic.loadUiType("main10.ui")[0]

#화면을 띄우는데 사용되는 Class 선언
class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.com ="123"
        self.setupUi(self)
        self.pb.clicked.connect(self.myclcik) #파이썬 문법!    
        self.setCom()
        
    def setCom(self):
        arr = [1,2,3,4,5,6,7,8,9]   
        
        for i in range(1000):
            rnd = int(random.random()*9)
            a = arr[0]
            b = arr[rnd]
            arr[0] = b
            arr[rnd] = a
            
        self.com = "{}{}{}".format(arr[0],arr[1],arr[2])
        print("self.com",self.com)
                    
    def myclcik(self):
        print("myclcik")
        mine = self.le.text()
        s = self.getStrike(self.com,mine);
        b = self.getBall(self.com,mine);
        print("s", s)
        
        # str_new = mine+"\t"+s+"S"+b+"B"+"\n"
        str_new = "{} {}S{}B\n".format(mine,s,b)
        str_old = self.te.toPlainText()
        
        
        self.te.setText(str_old+str_new)
        self.le.setText("")
        
        if s == 3 :
            QMessageBox.about(self,'야구게임','당신이 이겼습니다.')
        
        
    def getBall (self,com,mine):   
        cnt = 0
        c1 = com[0:1]
        c2 = com[1:2]
        c3 = com[2:3]
        
        m1 = mine[0:1]
        m2 = mine[1:2]
        m3 = mine[2:3]
        
        if c1 == m2 or c1 == m3 : cnt+=1
        if c2 == m1 or c2 == m3 : cnt+=1
        if c3 == m1 or c3 == m2 : cnt+=1
        
        return cnt
    
    def getStrike (self,com,mine):   
        cnt = 0
        c1 = com[0:1]
        c2 = com[1:2]
        c3 = com[2:3]
        
        m1 = mine[0:1]
        m2 = mine[1:2]
        m3 = mine[2:3]
        
        if c1 == m1 : cnt+=1
        if c2 == m2 : cnt+=1
        if c3 == m3 : cnt+=1
        
        return cnt

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

main10.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">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>140</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>야구게임</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>140</y>
      <width>61</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>180</y>
      <width>141</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>맞추기</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="te">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>220</y>
      <width>141</width>
      <height>221</height>
     </rect>
    </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>
profile
안녕하세요! 초보개발자 공부 시작!

0개의 댓글