파이썬 IDE 중 하나인 pycharm을 이용하여 제작하였습니다. PyQt5 설치이후 코드를 입려하여 실행하면 실행 될 것입니다. import sys, random from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QSizePolicy, QMessageBox from PyQt5.QtGui import QFont from PyQt5.QtTest import QTest class window(QWidget): def __init__(self): super().__init__() self.버튼 = [] self.앞면 = ["!","@","#","$","%","^","/","*","!","@","#","$","%","^","/","*"] self.cnt = 0 self.a = 0 self.b = 0 btn = QPushButton('시작', self) btn.resize(btn.sizeHint()) btn.move(50, 50) btn.clicked.connect(self.start) btn.clicked.connect(self.hidePzl)
def start(self): for x in range(300): rnd = random.randint(0,15) self.앞면[rnd], self.앞면[0] = self.앞면[0], self.앞면[rnd]
self.grid = QGridLayout() self.grid.setSpacing(15)
fnt = QFont() fnt.setBold(True) fnt.setPixelSize(50)
for r in range(4): self.버튼.append([]) for c in range(4): self.버튼[r].append(QPushButton(self.앞면[r * 4 + c])) self.버튼[r][c].setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.버튼[r][c].setFont(fnt) self.버튼[r][c].setToolTip(str(r * 4 + c)) self.버튼[r][c].setToolTipDuration(1) self.버튼[r][c].clicked.connect(self.test) self.grid.addWidget(self.버튼[r][c], r, c) self.setLayout(self.grid) self.setGeometry(100, 100, 500, 500)
def hidePzl(self): QTest.qWait(2000) for r in range(0,4): for c in range(0,4): self.버튼[r][c].setText("?")
def test(self): e = self.sender() if e.text() != "?": return if self.cnt == 0: self.cnt = 1 e.setText(self.앞면[int(e.toolTip())]) self.a = int(e.toolTip()) else: self.cnt = 0 e.setText(self.앞면[int(e.toolTip())]) self.b = int(e.toolTip()) self.chk(self.a, self.b)
def chk(self, a, b): if self.앞면[a] != self.앞면[b]: QTest.qWait(600) self.버튼[a // 4][a % 4].setText("?") self.버튼[b // 4][b % 4].setText("?")
def closeEvent(self, QCloseEvent): ans = QMessageBox.question(self, "종료 확인", "종료하시겠습니까?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if ans == QMessageBox.Yes: QCloseEvent.accept() else: QCloseEvent.ignore()
app = QApplication(sys.argv) win = window() win.show() sys.exit(app.exec_())
|