PyQt5
pyqt5 -- QObject 사용하는 singleton 만들기
자유프로그램
2018. 10. 26. 14:15
반응형
pyqt5 -- QObject 사용하는 singleton 만들기
참고 : https://wikidocs.net/3693
https://github.com/jazzycamel/PyQt5Singleton
QObject 를 상속받는 Singleton class 만들기
-- 프로젝트 만들때 package 로 만들어 사용함.
<< 실행화면 >>
<< 소스코드 >>
파일명 ; mypatten.py
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 QObject | |
class QtSingleton(QObject): | |
__instance = None | |
def __new__(cls, *args, **kwargs): | |
if not isinstance(cls.__instance, cls): | |
cls.__instance = QObject.__new__(cls, *args, **kwargs) | |
return cls.__instance | |
if __name__ == "__main__": | |
class AA(QtSingleton): | |
def __init__(self): | |
super().__init__() | |
self.num = 123 | |
class KK: | |
def __init__(self): | |
self.num = 55555 | |
print("----------- singletone 사용한 경우---------") | |
aa = AA() | |
bb = AA() | |
print(aa) | |
print(bb) | |
print(aa.num) | |
print(bb.num) | |
aa.num = 333 | |
print(aa.num) | |
print(bb.num) | |
print("----------- singleton 아닌 경우---------") | |
kk = KK() | |
mm = KK() | |
print(kk) | |
print(mm) |
반응형