숫자 맞추기 게임
기존 숫자 맞추기 게임을 활용해봅시다.
# -*- coding: UTF-8 -*-
import sys
import create_num
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
from PyQt5.QtGui import QPalette
class App(QWidget):
def __init__(self):
super(App, self).__init__()
self.setWindowTitle('Widget 배치!!!')
self.resize(400, 600)
self.addWidgets()
self.reset()
def addWidgets(self):
self.label_title = QLabel('위젯들을 붙여봅시다!!', self)
self.label_title.move(0,20)
self.label_title.resize(400, 40)
self.label_title.setAlignment(Qt.AlignCenter)
self.titleFont()
self.titleFontColor()
self.label_info = QLabel('입력 : ',self)
self.label_info.move(40,100)
self.infoFont()
self.le_input = QLineEdit('', self)
self.le_input.move(100, 100)
self.te_result = QTextEdit('', self)
self.te_result.move(40, 160)
self.te_result.resize(300,300)
self.te_result.setReadOnly(True)
self.button = QPushButton('확인', self)
self.button.resize(160, 50)
self.button.move(30, 550)
self.button.clicked.connect(self.confirm)
self.button2 = QPushButton('초기화', self)
self.button2.resize(160, 50)
self.button2.move(200, 550)
self.button2.clicked.connect(self.reset)
def infoFont(self):
font = QtGui.QFont()
font.setPointSize(27)
self.label_title.setFont(font)
def titleFont(self):
font = QtGui.QFont()
font.setPointSize(30)
self.label_title.setFont(font)
def titleFontColor(self):
pal = QtGui.QPalette()
pal.setColor(QPalette.WindowText, QtGui.QColor(38,38,38))
self.label_title.setPalette(pal)
def confirm(self):
if(not self.le_input.text().isnumeric()):
QMessageBox.about(self, "!!!!", "숫자를 입력하세요!")
return
inputNum = int(self.le_input.text())
print('랜덤 값 : {} 입력값 : {}'.format(create_num.getNumber(), inputNum))
inputNum
if(create_num.checkNumber(inputNum) == 0) :
QMessageBox.about(self, "!!!!", "정답입니다!!!")
elif(create_num.checkNumber(inputNum) == -1):
self.te_result.append('{} {}'.format(self.le_input.text(), '보다 작습니다.'))
else :
self.te_result.append('{} {}'.format(self.le_input.text(), '보다 큽니다.'))
self.le_input.clear()
self.te_result.repaint()
def reset(self):
self.count = 0
create_num.makeNumber()
self.te_result.clear()
self.te_result.repaint()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_())
create_num.py
import random
quizNumber = 0
def makeNumber() :
global quizNumber
quizNumber = random.randrange(100, 1000)
print(quizNumber)
def getNumber():
return quizNumber
def checkNumber(number) :
if(quizNumber == number) :
return 0
elif(quizNumber > number) :
return -1
else :
return 1
Last updated