import sys
from PyQt5.QtWidgets import *
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100,200,300,400)
self.setWindowTitle("PyQt")
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
anaconda navigater > show original > bin 파일 > Designer
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("/Users/jangsujeong/Desktop/mywindow.ui")[0]
class MyWindow(QMainWindow, form_class):#다중상속
def __init__(self):
super().__init__()
self.setupUi(self)
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
loadUiType()
: QT Designer의 결과물인 mywindow.ui 파일을 읽어 파이썬 클래스 코드를 만듦setupUi()
: form_class에 정의된 메서드. Qt Designer에서 만든 클래스 초기화.btn = QPushButton('클릭', self)
btn.clicked.connect(self.method)
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("/Users/jangsujeong/Desktop/mywindow.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
print('button click')
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import pykorbit
form_class = uic.loadUiType("/Users/jangsujeong/Desktop/mywindow.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.inquery)
def inquery(self):
price = pykorbit.get_current_price("BTC")
self.lineEdit.setText(str(price))
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
self.lineEdit.setText(str(price))
: price변수가 바인딩하는 값을 lineEdit의 setText()로 넘겨줌self.timer = QTimer(self) #윈도우가 생성될 때 QTimer 객체 생성해야함 self.timer.start(1000) #생성한 객체에 interval(1초) 추가 설정 self.timer.timeout.connect(self.timeout) #
- QTimer.timeout() : interval마다 발생하는 이벤트
- 1초에 한번씩 timeout 이벤트 발생
- 이벤트 루프 : 1초에 한 번 self.timeout()이라는 메서드 호출
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import pykorbit
from PyQt5.QtCore import *
form_class = uic.loadUiType("/Users/jangsujeong/Desktop/mywindow.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.timer = QTimer(self) # 윈도우가 생성될 때 QTimer 객체 생성해야함
self.timer.start(1000) # 생성한 객체에 interval(1초) 추가 설정
self.timer.timeout.connect(self.timeout)
def timeout(self):
cur_time = QTime.currentTime() #현재 시각을 얻어옴
str_time = cur_time.toString("hh:mm:ss")
self.statusBar().showMessage(str_time) #윈도우 상태바에 현재시간 출력
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import pykorbit
from PyQt5.QtCore import *
form_class = uic.loadUiType("/Users/jangsujeong/Desktop/mywindow.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.timer = QTimer(self) # 윈도우가 생성될 때 QTimer 객체 생성해야함
self.timer.start(1000) # 생성한 객체에 interval(1초) 추가 설정
self.timer.timeout.connect(self.inquery)
def inquery(self):
cur_time = QTime.currentTime()
str_time = cur_time.toString("hh:mm:ss")
self.statusBar().showMessage(str_time)
price = pykorbit.get_current_price("BTC")
self.lineEdit.setText(str(price))
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
- 시그널(signal) : 위젯에 정의된 이벤트
- 슬롯(slot) : 이벤트가 발생할 때 호출되는 함수나 메서드
btn.clicked.connect(self.btn_clicked)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MySignal(QObject):
signal1 = pyqtSignal()
def run(self):
self.signal1.emit()
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
mysignal = MySignal()
mysignal.signal1.connect(self.signal1_emitted)
mysignal.run()
def signal1_emitted(self):
print('signal1_emitted')
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
mysignal.signal1.connect(self.signal1_emitted)
: MySignal이라는 객체로부터 'signal1' 이벤트 발생 시 이벤트루프는 signal1_emitted 메서드 호출함mysignal.run()
: MySignal객체가 emit()호출하여 시그널 발생시키면 Mywindow에 정의된 메서드(슬롯) 호출됨import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MySignal(QObject):
signal1 = pyqtSignal()
signal2 = pyqtSignal(int, int)
def run(self):
self.signal1.emit()
self.signal2.emit(1, 2)
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
mysignal = MySignal()
mysignal.signal1.connect(self.signal1_emitted)
mysignal.signal2.connect(self.signal2_emitted)
mysignal.run()
@pyqtSlot()
def signal1_emitted(self):
print('signal1_emitted')
@pyqtSlot(int, int)
def signal2_emitted(self, arg1, arg2):
print('signal2_emitted', arg1, arg2)
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()