¾È±Ô °øºÎ¹æ

Python ÇÁ·Î±×·¡¹Ö > PyQt5 - QtWidgets - QMainWindow

µî·ÏÀÏ : 2018-07-11 15:38 Á¶È¸¼ö : 53,574

¿À´ÃÀº QtWidgets¿¡¼­ QMainWindow ¸¦ ¾Ë¾Æ º¼°Ì´Ï´Ù
QMainWindow´Â °¡Àå ¿Ü°ûÀÇ À©µµ¿ì ÇÁ·¹ÀÓÀ» ¸¸µå´Â ¸ðµâÀÔ´Ï´Ù.

1. ¸ÞÀÎ À©µµ¿ì »ý¼º
 
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # main window setting
        self.setGeometry(30, 70, 400, 200)
        self.setWindowTitle('¸ÞÀÎ À©µµ¿ì')
        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MyWindow()
    sys.exit(app.exec_())



¸ÕÀú ¸ÞÀÎ À©µµ¿ì¸¦ »ý¼ºÇϱâ À§ÇØ ÇÊ¿äÇÑ ÇÔ¼öµéÀº QMainWindow ·ÎºÎÅÍ »ó¼ÓÀ» ¹Þ½À´Ï´Ù.
¿©±â¿¡ QApplication À¸·Î ºÎÅÍ ¾ÛÀ¸·Î¼­ ÀÛµ¿Çϵµ·Ï ½Ã½ºÅÛÀ¸·ÎºÎÅÍ ÀÎÀÚ¸¦ ¹Þ¾Æ ½ÇÇàÇϵµ·ÏÇÕ´Ï´Ù.

self.setGeometry(30, 70, 500, 350)   // ¸ð´ÏÅͻ󿡼­ »ý¼ºµÇ´Â À§Ä¡ + ³»ºÎÇÁ·¹ÀÓ Å©±â ¼³Á¤
self.setWindowTitle('customUI')      // À©µµ¿ì³»ÀÇ Å¸ÀÌƲ ¼³Á¤
self.show()                                   // ÆÄÀÏ ½ÇÇà½Ã gui º¸¿©ÁÖµµ·Ï ¸í·É À̸¦ ½ÇÇàÇÏ¸é ¾Æ·¡¿Í °°Àº À©µµ¿ì°¡ »ý¼ºµË´Ï´Ù.

2. menubar »ý¼º

À̹ø¿£ À©µµ¿ì ³»¿¡ ¸Þ´º¹Ù¸¦ ¸¸µé¾î º¸°Ú½À´Ï´Ù.

from PyQt5.QtWidgets import qApp, QAction
from PyQt5.QtGui import QIcon


Ãß°¡µÇ´Â ¸ðµâÀº QAction°ú qApp ±×¸®°í QIcon ÀÔ´Ï´Ù.
QAction Àº ¸Þ´º¹Ù¿¡ ¸Þ´º¿Í ¸µÅ©ÇÒ ±â´ÉÀ» °¡Áö´Â °´Ã¼ÀÔ´Ï´Ù.
¿©±â¿¡ ¾ÆÀÌÄÜ °´Ã¼¸¦ ³Ö±âÀ§Çؼ­ QIcon ÀÌ ÇÊ¿äÇÕ´Ï´Ù.
qAppÀº »ç½Ç Áö±ÝÀ¸·Î¼­´Â À־ ÁÁ°í ¾ø¾îµµ ÁÁ½À´Ï´Ù. ÇÏÁö¸¸ ±â´ÉÀ» °£´ÜÈ÷ ºÒ·¯¿Ã¼ö ÀÖ´Ù´Â ÀåÁ¡ÀÌ À־ ¿©±â¼­ º¸¿©ÁÝ´Ï´Ù.


menu_bar = self.menuBar()        // À©µµ¿ì ³»¿¡¼­ ¸Þ´º¹ÙÀÇ °ø°£À» ¸¶·ÃÇÕ´Ï´Ù.
menu_bar.setNativeMenuBar(False) // mac native menu bar¸¦ »ç¿ëX
 


óÀ½ ÄÚµå´Â ¹Ù·Î ÀÌÇØ°¡ °¡´ÉÇѵ¥ µÎ¹ø° Äڵ尡 ¾î»öÇϳ׿ä.. ^^ ¸Æ¿¡¼­´Â »ç½Ç ¸Þ´º¹ÙÀÇ À§Ä¡°¡ ½ÇÇà À©µµ¿ì ¾È¿¡ À§Ä¡ÇÏÁö ¾Ê½À´Ï´Ù.
±×·¡¼­ ±×³É ¸Þ´º¹Ù¸¸ »ý¼ºÀ» ÇÏ¸é »ç½Ç ÀÛµ¿ÇÏÁö ¾Ê½À´Ï´Ù.
Çؼ­ ¸ÆÀÇ ³×ÀÌƼºê¸Þ´º¹Ù¸¦ »ç¿ëÇÏÁö ¾Ê°Ú´Ù´Â ÄÚµùÀ» ³Ö¾îÁÖ¸é ÀÏ¹Ý À©µµ¿ìÀÇ ¸Þ´º¹Ùó·³ ÀÛµ¿ÇÏ°Ô ¸¸µé¼ö ÀÖ½À´Ï´Ù.
ÀÌÁ¦ ¸Þ´º¹Ù¸¦ ¸¸µé¾úÀ¸´Ï ¸Þ´º¸¦ ³Ö¾îº¸°Ú½À´Ï´Ù.

# manu Ãß°¡
menu1   = menu_bar.addMenu('&ÆÄÀÏ')
menu2   = menu_bar.addMenu('&º¸±â')

ÇÏÁö¸¸ ÀÌ·± ¸Þ´º´Â ±×³É ¿©·¯ ±â´ÉÁß ºñ½ÁÇÑ ±â´ÉµéÀÇ ¸ðÀ½ÁýÀÔ´Ï´Ù.
¸ðÀ½ÁýÀÌ´Ï ÀÌÁ¦ ±â´É(action)À» ³Ö¾î ÁÖ¾î¾ß ÇÕ´Ï´Ù.


# manu1 ±â´É »ý¼º
action1 = QAction(QIcon('home_icon.png'),'&Exit', self)
action1.setShortcut('Ctrl+Q')           #´ÜÃàÅ°·Î 'Ctrl+Q'·Î ¼³Á¤
action1.setStatusTip('Exit App')        #statusBar¿¡ Ç¥½ÃµÉ tip ¼³Á¤
action1.triggered.connect(self.menu1_clicked)  #¼±Åýÿ¡ menu1_clicked ÇÔ¼ö¸¦ È£ÃâÇÕ´Ï´Ù.
menu1.addAction(action1)   'ÆÄÀÏ' Ä­¿¡ Ãß°¡ÇØÁÝ´Ï´Ù.

# manu2 ±â´É »ý¼º
action2 = QAction('&View', self)
action2.setShortcut('Ctrl+T')           #´ÜÃàÅ°·Î 'Ctrl+T'·Î ¼³Á¤
action2.setStatusTip('View App')        #statusBar¿¡ Ç¥½ÃµÉ tip ¼³Á¤
action2.triggered.connect(self.menu2_clicked)  #¼±Åýÿ¡ menu2_clicked ÇÔ¼ö¸¦ È£ÃâÇÕ´Ï´Ù.
menu2.addAction(action2)  'º¸±â' Ä­¿¡ Ãß°¡ÇØÁÝ´Ï´Ù.

¿©±â±îÁö°¡ À©µµ¿ì ¸Þ´º¹Ù¸¦ »ý¼ºÇÏ´Â ÄÚµåÀÔ´Ï´Ù.
¾Æ·¡´Â Àüü ¼Ò½ºÄÚµå ÀÔ´Ï´Ù. 
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QAction


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # main window setting
        self.setGeometry(30, 70, 400, 200)
        self.setWindowTitle('¸ÞÀÎ À©µµ¿ì')

        menu_bar = self.menuBar()
        menu_bar.setNativeMenuBar(False)
        # manu Ãß°¡
        menu1   = menu_bar.addMenu('&ÆÄÀÏ')
        menu2   = menu_bar.addMenu('&º¸±â')

        # manu ±â´É »ý¼º
        action1 = QAction('&Exit', self)
        action1.setShortcut('Ctrl+Q')           #´ÜÃàÅ°·Î 'Ctrl+Q'·Î ¼³Á¤
        action1.setStatusTip('Exit App')        #statusBar¿¡ Ç¥½ÃµÉ tip ¼³Á¤
        action1.triggered.connect(self.menu1_clicked)
        menu1.addAction(action1)

        action2 = QAction('&View', self)
        action2.setShortcut('Ctrl+T')           #´ÜÃàÅ°·Î 'Ctrl+T'·Î ¼³Á¤
        action2.setStatusTip('View App')        #statusBar¿¡ Ç¥½ÃµÉ tip ¼³Á¤
        action2.triggered.connect(self.menu2_clicked)
        menu2.addAction(action2)

    def menu1_clicked(self):
        print("¸Þ´º¼±ÅÃ1")

    def menu2_clicked(self):
        print("¸Þ´º¼±ÅÃ2")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MyWindow()
    mw.show()
    sys.exit(app.exec_())






 
 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.