창 닫기

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import QCoreApplication
# QtCore 모듈의 QCoreApplication 클래스를 불러온다.

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        btn = QPushButton('Quit', self)
				# Quit 위치에는 버튼에 들어갈 문구가 들어간다.
				# self는 버튼이 위치할 부모 위젯이다.				
        btn.move(50, 50)
        btn.resize(btn.sizeHint())
        btn.clicked.connect(QCoreApplication.instance().quit)

        self.setWindowTitle('Quit Button')
				# 타이틀 입력해주는 위젯이다.
        self.setGeometry(300,300,300,200)
				# 버튼의 위치를 나타낸다.
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

결과

Untitled

화면 창 닫는 버튼을 만드는 코드다.

코드를 실행시킨 결과다.

상태바 만들기

Untitled

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

class MyApp(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.statusBar().showMessage('Ready')
				# 하단에 있는 상태 바에 나올 문구를 입력하는 위젯이다.

        self.setWindowTitle('Statusbar')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

결과

Untitled

메인창의 레이아웃이며 위젯들의 명칭을 알 수 있다.

하단의 상태바에 문구를 입력하는 위젯과 관련된 코드다.

코드를 실행시킨 결과다.

하단의 상태 바에 “Ready”라고 적혀있다.

메뉴바 만들기

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon

class MyApp(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q') 
				# 단축키 커맨드와 단축키의 매핑등록하는 위젯이다.
        exitAction.setStatusTip('Exit application')
				# 마우스 hover 했을때 상태바에 나타날 메시지를 설정하는 위젯
        exitAction.triggered.connect(qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        menubar.setNativeMenuBar(False)
        filemenu = menubar.addMenu('&File')
        filemenu.addAction(exitAction)

        self.setWindowTitle('Menubar')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

결과

Untitled

상단에 메뉴바를 만들어주는 코드다.

실행화면 중앙 시작

날짜와 시간 표현