숫자 맞추기
<?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>286</width>
<height>270</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>60</x>
<y>30</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>110</x>
<y>30</y>
<width>113</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>60</x>
<y>70</y>
<width>161</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>맞춰보기</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="pte">
<property name="geometry">
<rect>
<x>60</x>
<y>100</y>
<width>161</width>
<height>101</height>
</rect>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
# PyQt ui 불러오기
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from random import random
form_class = uic.loadUiType("./pyqt08.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.com=int(random()*99)
print("self.com",self.com)
self.show()
self.pb.clicked.connect(self.myclick) # 버튼 클릭 시 myclick 실행됨
def myclick(self):
mine = self.le.text()
str_new=""
if self.com<int(mine):
str_new="{} DOWN\n".format(mine)
elif self.com>int(mine):
str_new="{} UP\n".format(mine)
else:
str_new="{} 정답입니다.\n".format(mine)
QMessageBox.about(self,'Up&Down',"축하합니다.")
str_old=self.pte.toPlainText()
self.pte.setPlainText(str_old+str_new)
self.le.setText("")
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
![]() |
![]() |
별 그리기
<?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>219</width>
<height>477</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="lbl_first">
<property name="geometry">
<rect>
<x>40</x>
<y>20</y>
<width>56</width>
<height>12</height>
</rect>
</property>
<property name="text">
<string>첫 별수</string>
</property>
</widget>
<widget class="QLineEdit" name="le_first">
<property name="geometry">
<rect>
<x>110</x>
<y>10</y>
<width>61</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>40</x>
<y>80</y>
<width>131</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>별그리기</string>
</property>
</widget>
<widget class="QLabel" name="lbl_last">
<property name="geometry">
<rect>
<x>40</x>
<y>50</y>
<width>56</width>
<height>12</height>
</rect>
</property>
<property name="text">
<string>끝 별수</string>
</property>
</widget>
<widget class="QLineEdit" name="le_last">
<property name="geometry">
<rect>
<x>110</x>
<y>40</y>
<width>61</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="te">
<property name="geometry">
<rect>
<x>40</x>
<y>110</y>
<width>131</width>
<height>231</height>
</rect>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
form_class = uic.loadUiType("./pyqt09.ui")[0] #로드할 UI파일
class WindowClass(QMainWindow, form_class):
def __init__(self):
super().__init__() # 상위 클래스인 QtWidgets.QMainWindow의 초기화
self.setupUi(self) # UI 파일을 현재 위도우에 설정
self.show()# 윈도우를 표시
self.pb.clicked.connect(self.myclick) #클릭되는 이벤트
def myclick(self):
txt1 = self.le_first.text()
txt2 = self.le_last.text()
num1 = int(txt1)
num2 = int(txt2)
txt = ""
for i in range(num1,num2+1):
txt += self.drawStar(i)
self.te.setText(txt)
def drawStar(self,cnt):
ret=""
for i in range(cnt):
ret += "*"
ret += "\n"
return ret
if __name__ == "__main__": # java의 main 메소드와 같음
app = QApplication(sys.argv) # 애플리케이션 초기화
myWindow = WindowClass()
myWindow.show()
app.exec_()
# app.exec_() => 이벤트 루프 실행, 애플리케이션 실행
결과
![]() |
![]() |
'파이썬 > 수업내용' 카테고리의 다른 글
[Python] MongoDB, Studio 3T 설치 및 사용하기 (0) | 2024.07.03 |
---|---|
[Python]DB (MySQL) 설치 및 연동 하기 / SELECT, INSERT, UPDATE, DELETE (0) | 2024.07.01 |
[Python/PyQt] PyQt 로또 출력하기 (0) | 2024.06.28 |
[Python/PyQt] PyQt 구구단 출력하기 (0) | 2024.06.28 |
[Python/PyQt] PyQt 숫자 입력 후 클릭 시 계산하기 (0) | 2024.06.28 |