반응형
PyQt5 -- custom data-carrying signal & slot 만들기 예제
환경 ; windows 10 64bit, anaconda 3 32bit, python 3.6, PyQt5.6.2
참고 ; http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/
http://www.comp.leeds.ac.uk/nde/modules/2542/labs/sigslot/main.html#custom-slots-in-python
http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html
https://books.martinfitzpatrick.name/create-simple-gui-applications/#_signals_slots_events
1. custom signal & custom slot
< 실행결과 >
< 소스 >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject | |
class PunchingBag(QObject): | |
""" | |
QObject 를 상속받으면, custom signal을 만들수있다. | |
""" | |
punched = pyqtSignal() # custom signal | |
def __init__(self): | |
# Initialize the PunchingBag as a QObject | |
super().__init__() | |
def punch(self): | |
self.punched.emit() | |
@pyqtSlot() | |
def say_punched(): | |
print('Bag was punched.') | |
if __name__ == "__main__": | |
bag = PunchingBag() | |
# Connect the bag's punched signal to the say_punched slot | |
bag.punched.connect(say_punched) | |
# Punch the bag 10 times | |
for i in range(10): | |
bag.punch() |
--
2. custom data-carrying signal & custom slot
<실행결과 >
< 소스 >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Data-Carrying PySide/PyQt Signals | |
""" | |
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject | |
class Circle(QObject): | |
# Signal emitted when the circle is resized, | |
# carrying its integer radius | |
resized = pyqtSignal(int) # 반지름 크기 변할때, 반지름 보내는 signal | |
# Signal emitted when the circle is moved, carrying | |
# the x and y coordinates of its center. | |
moved = pyqtSignal(int, int) # 원 이동시, 중심자표 보내는 signal | |
def __init__(self, x, y, r): | |
# Initialize the Circle as a QObject so it can emit signals | |
super().__init__() | |
# "Hide" the values and expose them via properties | |
self.__x = x | |
self.__y = y | |
self.__r = r | |
@property | |
def x(self): | |
return self.__x | |
@x.setter | |
def x(self, new_x): | |
self.__x = new_x | |
# After the center is moved, emit the | |
# moved signal with the new coordinates | |
self.moved.emit(new_x, self.y) | |
@property | |
def y(self): | |
return self.__y | |
@y.setter | |
def y(self, new_y): | |
self.__y = new_y | |
# After the center is moved, emit the moved | |
# signal with the new coordinates | |
self.moved.emit(self.x, new_y) | |
@property | |
def r(self): | |
return self.__r | |
@r.setter | |
def r(self, new_r): | |
self.__r = new_r | |
# After the radius is changed, emit the | |
# resized signal with the new radius | |
self.resized.emit(new_r) | |
# A slot for the "moved" signal, accepting the x and y coordinates | |
@pyqtSlot(int, int) | |
def on_moved(x, y): | |
print('Circle was moved to (%s, %s).' % (x, y)) | |
# A slot for the "resized" signal, accepting the radius | |
@pyqtSlot(int) | |
def on_resized(r): | |
print('Circle was resized to radius %s.' % r) | |
if __name__ == "__main__": | |
c = Circle(5, 5, 4) | |
# Connect the Circle's signals to our simple slots | |
c.moved.connect(on_moved) | |
c.resized.connect(on_resized) | |
# Move the circle one unit to the right | |
c.x += 1 | |
# Increase the circle's radius by one unit | |
c.r += 1 |
--
3. custom signal & custom slot
< 실행결과 >
< 소스 >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtCore import Qt | |
from PyQt5.QtWidgets import * | |
from PyQt5.QtCore import pyqtSlot, pyqtSignal | |
class MyMain(QWidget): | |
def __init__(self): | |
super().__init__() | |
vbox = QVBoxLayout() | |
self.txtline = MyTextLine() | |
self.txtline2 = QLineEdit() | |
vbox.addWidget(self.txtline) | |
vbox.addWidget(self.txtline2) | |
self.setLayout(vbox) | |
self.txtline.A_pressed.connect(self.a_press) # custom signal - slot 연결함. | |
self.show() | |
@pyqtSlot() | |
def a_press(self): | |
txt = self.txtline2.text() | |
self.txtline2.setText(txt + "A...") | |
class MyTextLine(QLineEdit): | |
A_pressed = pyqtSignal() # custom signal 생성함 | |
def __init__(self, *args): | |
super().__init__(*args) | |
def keyPressEvent(self, e): | |
if e.key() == Qt.Key_A: | |
self.A_pressed.emit() # custom signal 발생 | |
super().keyPressEvent(e) | |
else: | |
super().keyPressEvent(e) | |
pass | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
ex = MyMain() | |
sys.exit(app.exec_()) |
--
반응형
'PyQt5' 카테고리의 다른 글
PyQt5 -- 마우스 event 사용 예제 (1) | 2017.05.02 |
---|---|
PyQt5 -- 마우스 event & 마우스 현재 위치 출력하기 (0) | 2017.05.01 |
PyQt5 -- signal & slot 예제 (0) | 2017.05.01 |
PyQt5 -- layout 관리 주의사항 (6) | 2017.04.28 |
PyQt5 기초 -- QLabel, QLineEdit, QTextEdit (0) | 2017.04.28 |