문제
구구단 출력하기
풀이
pyqt04.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>354</width>
<height>559</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>70</x>
<y>50</y>
<width>91</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>출력단수</string>
</property>
</widget>
<widget class="QSpinBox" name="sb">
<property name="geometry">
<rect>
<x>180</x>
<y>50</y>
<width>101</width>
<height>22</height>
</rect>
</property>
<property name="minimum">
<number>2</number>
</property>
<property name="maximum">
<number>9</number>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>60</x>
<y>80</y>
<width>221</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>출력하기</string>
</property>
</widget>
<widget class="QTextEdit" name="te">
<property name="geometry">
<rect>
<x>60</x>
<y>120</y>
<width>221</width>
<height>351</height>
</rect>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
pyqt04.py 방법 1
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
form_class = uic.loadUiType("./pyqt04.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.btnclick) #클릭되는 이벤트
def btnclick(self):
dan = self.sb.text()
b = ""
for i in range(1,9+1):
b += ("{}*{}={}\n".format(dan,i,str(int(dan)*i)))
self.te.setText(b)
#self.le3.setText(str(c)) #num은 숫자이기 때문에 문자로 바꿔줘야 한다.
if __name__ == "__main__": # java의 main 메소드와 같음
app = QApplication(sys.argv) # 애플리케이션 초기화
myWindow = WindowClass()
myWindow.show()
app.exec_()
# app.exec_() => 이벤트 루프 실행, 애플리케이션 실행
pyqt04.py 방법 2
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
form_class = uic.loadUiType("./pyqt04.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.btnclick) #클릭되는 이벤트
def btnclick(self):
idan = self.sb.value()
txt = ""
for i in range(1,9+1):
txt += ("{} * {} = {}\n".format(idan,i,i*idan))
self.te.setText(txt)
if __name__ == "__main__": # java의 main 메소드와 같음
app = QApplication(sys.argv) # 애플리케이션 초기화
myWindow = WindowClass()
myWindow.show()
app.exec_()
# app.exec_() => 이벤트 루프 실행, 애플리케이션 실행
결과
![]() |
![]() |
'파이썬 > 수업내용' 카테고리의 다른 글
[Python] 숫자 맞추기 / 별 그리기 (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 |
[Python] GUI (0) | 2024.06.27 |