구구단을 출력해보자.
마찬가지로 생성자에 버튼 클릭 시 동작하는 이벤트 메소드를 선언해주고,
myclick함수를 선언해준다.
print("hello")를 찍어보고 run을 실행해보자! 잘 실행이되었다면 제대로 작성한것이다.
QLineEdit은 .getText( )가 아닌 .text( )로 텍스트 값을 가져온다.
가져온 값들은 모두 String타입이기 때문에 숫자로 연산을 해야하는 경우라면
int(데이터) 로 형변환을 해주어야 한다.
구구단을 찍을 때 자바에서는 이중 for문을 썼기때문에 익숙한대로 for문을 생각하겠지만,
아래와 같은 방법으로 모두 찍어보는것도 좋은방법이니, for만 생각하지말고 아래와 같은방법으로 코드를 작성해자.
일단 2*1= 2와 같이 문자열을 출력하여 담을 그릇을 만든다.
answer =" " => 빈 스트링으로 초기화해주었다.
format() 함수 이용하여 문자열 출력하기
answer += "{ }*{ } = { }\n".format(idan,1,idan*1)
answer 을 출력할 텍스트뷰(TextView) tvDisp에 값을 셋팅해준다. => self.tvDisp.setText( answer )
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.Qt import QLabel
from PyQt5.uic.Compiler.qtproxies import QtWidgets, QtCore
form_class = uic.loadUiType("main03.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
print("hello")
dan = self.le.text()
idan = int(dan)
#print(str(aa) + "*" +"1"+"="+ str(aa*1) +"\n")
answer=""
answer += "{}*{}={}\n".format(idan,1,idan*1)
answer += "{}*{}={}\n".format(idan,2,idan*2)
answer += "{}*{}={}\n".format(idan,3,idan*3)
answer += "{}*{}={}\n".format(idan,4,idan*4)
answer += "{}*{}={}\n".format(idan,5,idan*5)
answer += "{}*{}={}\n".format(idan,6,idan*6)
answer += "{}*{}={}\n".format(idan,7,idan*7)
answer += "{}*{}={}\n".format(idan,8,idan*8)
answer += "{}*{}={}\n".format(idan,9,idan*9)
self.tvDisp.SetText(answer)
# for i in range(1, 10):
# answer += str(idan) + "*" +str(i)+"="+ str(idan*i) +"\n"
#
# self.tvDisp.setText(answer)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
main03.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>762</width>
<height>596</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>110</x>
<y>50</y>
<width>91</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>출력단수:</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>110</x>
<y>90</y>
<width>211</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>출력하기</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>210</x>
<y>60</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="tvDisp">
<property name="geometry">
<rect>
<x>110</x>
<y>130</y>
<width>211</width>
<height>101</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>762</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
'공부 기록노트 > python' 카테고리의 다른 글
[python]day3. 파이큐티(PyQt5 ) - 로또번호 번호 출력하기 (0) | 2023.01.03 |
---|---|
[python]day3. 파이큐티(PyQt5 ) - 홀 짝 게임 만들기 (0) | 2023.01.03 |
[python]day3. 파이큐티(PyQt5 ) - 숫자 증가시키기 (0) | 2023.01.03 |
[python]day3. 파이큐티(PyQt5 ) 시작하기 - designer실행 (1) | 2023.01.03 |
[python] day2. 파이썬 개발환경 세팅(전자정부프레임워크) (0) | 2023.01.01 |