728x90
from PyQt5 import uic
from PyQt5.QtWidgets import *
from random import random
import sys
form_class = uic.loadUiType("myqt08.ui")[0]
class WindowClass(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.ranC()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
self.le.returnPressed.connect(self.myclick)
def getS(self,com,mine):
ret = 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: ret+=1
if c2 ==m2: ret+=1
if c3 ==m3: ret+=1
return ret
def getB(self,com,mine):
ret = 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: ret+=1
if c2 ==m1 or c2==m3: ret+=1
if c3 ==m1 or c3==m2: ret+=1
return ret
def ranC(self):
arr=[1,2,3,4,5,6,7,8,9]
for i in range(100):
rnd= int(random()*9)
a = arr[0]
arr[0] = arr[rnd]
arr[rnd]=a
self.com = "{}{}{}".format(arr[0],arr[1],arr[2])
print("self.com: ",self.com)
def myclick(self):
mine=self.le.text()
s= self.getS(self.com,mine)
b= self.getB(self.com,mine)
str_new = mine+"\t"+str(s)+"S"+str(b)+"B"+"\n"
str_old = self.te.toPlainText()
self.te.setText(str_new+str_old)
self.le.setText("")
if s == 3:
QMessageBox.about(self,'야구게임',mine+"맞췄습니다")
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">
<property name="geometry">
<rect>
<x>110</x>
<y>80</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>스트라이크:</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>260</x>
<y>80</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>100</x>
<y>130</y>
<width>271</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>게임하기</string>
</property>
</widget>
<widget class="QTextEdit" name="te">
<property name="geometry">
<rect>
<x>90</x>
<y>190</y>
<width>291</width>
<height>351</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>
'Python > Python 기초' 카테고리의 다른 글
파이썬(QT) 오목게임 만들기 (0) | 2023.07.01 |
---|---|
파이썬(QT) 별찍기 예제 (0) | 2023.06.29 |
파이썬(QT) 가위바위보 프로그램 만들기 예제 (0) | 2023.06.29 |
파이썬(QT) 전화번호 팝업창 띄우기 예제 (0) | 2023.06.28 |
파이썬(QT) 구구단 입력 받아 출력 예제 (0) | 2023.06.28 |