import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.Qt import QLabel
import random
# 내코드
form_class = uic.loadUiType("main0b.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
self.com= 1
self.randomCom()
def randomCom(self):
arr = ["1","2","3","4","5","6","7","8","9"]
self.com = ""
for i in range(100):
rnd = int(random.random()*len(arr))
print(rnd)
a = arr[rnd]
b = arr[0]
print("a", a)
print("b", b)
arr[0] = a
arr[rnd] = b
self.com = (arr[0]+arr[1]+arr[2])
print("self.com", self.com)
return self.com
def myclick(self):
mine = self.le.text()
print(mine)
s = self.getStrike(self.com,mine)
b = self.getBall(self.com,mine)
print("s", s)
print("b", b)
str_old = self.te.toPlainText()
str_new = mine + " " + str(s) + "S" + str(b)+ "B" + "\n"
self.te.setText(str_old + str_new)
self.le.setText("")
ss = int(s)
if(ss == 3):
QMessageBox.about(self,'3Strike','정답입니다~~')
def getStrike(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 getBall(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
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
방법2.
더보기
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.Qt import QLabel, QTextEdit
import random
# 쌤코드
form_class = uic.loadUiType("main0b.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
#전역변수는 생성자에 만들어준다
self.com = "123"
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
self.randomCom()
def randomCom(self):
arr = ["1","2","3","4","5","6","7","8","9"]
for i in range(100):
rnd = int(random.random()*len(arr))
a = arr[rnd]
b = arr[0]
arr[0] = a
arr[rnd] = b
self.com = arr[0]+arr[1]+arr[2]
print("self.com", self.com)
def myclick(self):
mine = self.le.text()
#메소드를부를때는 self. 붙이기
s = self.getStrike(self.com, mine)
b = self.getBall(self.com, mine)
#QTextEdit 글씨 가져올때 .toPlainText()
str_old = self.te.toPlainText()
str_new = mine + " " +str(s) + "S" + str(b) + "B" + "\n"
self.te.setText(str_old + str_new)
self.le.setText("")
#qte = QTextEdit()
#qte.setText()
if(s == 3):
QMessageBox.about(self, '스트라이크', '맞췄습니다.{}'.format(self.com))
#파라미터를 넘길때 self도 찍어주기
def getStrike(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
#파라미터를 넘길때 self도 찍어주기
def getBall(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
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
main0b.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>636</width>
<height>670</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>80</x>
<y>60</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>스트라이크</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>170</x>
<y>60</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string></string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>80</x>
<y>90</y>
<width>201</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>맞추기</string>
</property>
</widget>
<widget class="QTextEdit" name="te">
<property name="geometry">
<rect>
<x>80</x>
<y>130</y>
<width>201</width>
<height>241</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
'공부 기록노트 > python' 카테고리의 다른 글
파이썬(Python) 기초 print ( ) - end옵션 (0) | 2023.01.05 |
---|---|
[python]day6. MSSQL - SELECT/INSERT/UPDATE/DELETE (0) | 2023.01.04 |
[python]day4. 파이큐티(pyQt5) - 배수의 합 구하기 (0) | 2023.01.03 |
[python]day4. 파이큐티(pyQt5) - 곱하기 연산 (0) | 2023.01.03 |
[python]day4. 파이큐티(pyQt5) - 전화번호 찍기 (0) | 2023.01.03 |