요즘 python3 와 PyQt5 를 활용한 GUI 프로그래밍 할 일이 많이 있었습니다. GUI 프로그래밍은 레퍼런스를 잘 보고 복붙해서 하면 좋은데 PyQt5 는 적당한 예제를 찾기 힘든게 사실입니다.
PyQt4 예제만 잔뜩 나오죠. 레퍼런스는 C++로만 제공되서 더욱 어렵게 느껴집니다.
이에 제가 자주 쓰는 예제코드들을 하나씩 올려두려 합니다. 누군가에겐 도움이 됐으면 좋겠습니다.
QMessageBox 는 윈도우의 MessageBox 와 그 기능이 완전히 동일합니다. 사용법도 두 줄이면 되니 정말 간편하고요. 어느 버튼을 눌렀는지 가져올 수도 있지만 그건 예제가 너무 길어지니 다음 기회에…
from PyQt5 import QtWidgets
import sys
class MyDialog(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle('QMessageBox Example')
self.setGeometry(200, 200, 320, 200) # left top width height
btn = QtWidgets.QPushButton(self)
btn.setGeometry(50, 50, 100, 50)
btn.setText('Show')
btn.clicked.connect(self.showMessageBox)
def showMessageBox(self):
msgbox = QtWidgets.QMessageBox(self)
msgbox.question(self, 'MessageBox title', 'Here comes message', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = MyDialog()
ui.show()
sys.exit(app.exec_())