pyqt5——菜單和工具欄

菜單和工具欄

這個章節,咱們會建立狀態欄、菜單和工具欄。菜單是一組位於菜單欄的命令。工具欄是應用的一些經常使用工具按鈕。狀態欄顯示一些狀態信息,一般在應用的底部。python

主窗口

QMainWindow提供了主窗口的功能,使用它能建立一些簡單的狀態欄、工具欄和菜單欄。瀏覽器

主窗口是下面這些窗口的合稱,因此教程在最下方。app

狀態欄

狀態欄是用來顯示應用的狀態信息的組件。框架

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a statusbar.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication


class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        
        self.statusBar().showMessage('Ready')
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Statusbar')    
        self.show()


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

狀態欄是由QMainWindow建立的。ide

self.statusBar().showMessage('Ready')

調用QtGui.QMainWindow類的statusBar()方法,建立狀態欄。第一次調用建立一個狀態欄,返回一個狀態欄對象。showMessage()方法在狀態欄上顯示一條信息。工具

程序預覽:學習

菜單欄

菜單欄是很是經常使用的。是一組命令的集合(Mac OS下狀態欄的顯示不同,爲了獲得最類似的外觀,咱們增長了一句menubar.setNativeMenuBar(False))。ui

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a menubar. The
menubar has one menu with an exit action.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: January 2017
"""

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


class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        
        exitAct = QAction(QIcon('exit.png'), '&Exit', self)        
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Simple menu')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

咱們建立了只有一個命令的菜單欄,這個命令就是終止應用。同時也建立了一個狀態欄。並且還能使用快捷鍵Ctrl+Q退出應用。spa

exitAct = QAction(QIcon('exit.png'), '&Exit', self)        
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')

QAction是菜單欄、工具欄或者快捷鍵的動做的組合。前面兩行,咱們建立了一個圖標、一個exit的標籤和一個快捷鍵組合,都執行了一個動做。第三行,建立了一個狀態欄,當鼠標懸停在菜單欄的時候,能顯示當前狀態。code

exitAct.triggered.connect(qApp.quit)

當執行這個指定的動做時,就觸發了一個事件。這個事件跟QApplication的quit()行爲相關聯,因此這個動做就能終止這個應用。

menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)

menuBar()建立菜單欄。這裏建立了一個菜單欄,並在上面添加了一個file菜單,並關聯了點擊退出應用的事件。

程序預覽:

子菜單

子菜單是嵌套在菜單裏面的二級或者三級等的菜單。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a submenu.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QMenu, QApplication

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):         
        
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        
        impMenu = QMenu('Import', self)
        impAct = QAction('Import mail', self) 
        impMenu.addAction(impAct)
        
        newAct = QAction('New', self)        
        
        fileMenu.addAction(newAct)
        fileMenu.addMenu(impMenu)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Submenu')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

這個例子裏,有兩個子菜單,一個在file菜單下面,一個在file的import下面。

impMenu = QMenu('Import', self)

使用QMenu建立一個新菜單。

impAct = QAction('Import mail', self) 
impMenu.addAction(impAct)

使用addAction添加一個動做。

程序預覽:

勾選菜單

下面是一個能勾選菜單的例子

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a checkable menu.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QApplication

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):         
        
        self.statusbar = self.statusBar()
        self.statusbar.showMessage('Ready')
        
        menubar = self.menuBar()
        viewMenu = menubar.addMenu('View')
        
        viewStatAct = QAction('View statusbar', self, checkable=True)
        viewStatAct.setStatusTip('View statusbar')
        viewStatAct.setChecked(True)
        viewStatAct.triggered.connect(self.toggleMenu)
        
        viewMenu.addAction(viewStatAct)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Check menu')    
        self.show()
        
    def toggleMenu(self, state):
        
        if state:
            self.statusbar.show()
        else:
            self.statusbar.hide()
       
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

本例建立了一個行爲菜單。這個行爲/動做能切換狀態欄顯示或者隱藏。

viewStatAct = QAction('View statusbar', self, checkable=True)

checkable選項建立一個能選中的菜單。

viewStatAct.setChecked(True)

默認設置爲選中狀態。

def toggleMenu(self, state):
    
    if state:
        self.statusbar.show()
    else:
        self.statusbar.hide()

依據選中狀態切換狀態欄的顯示與否。
程序預覽:

右鍵菜單

右鍵菜單也叫彈出框(!?),是在某些場合下顯示的一組命令。例如,Opera瀏覽器裏,網頁上的右鍵菜單裏會有刷新,返回或者查看頁面源代碼。若是在工具欄上右鍵,會獲得一個不一樣的用來管理工具欄的菜單。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a context menu.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):         
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Context menu')    
        self.show()
    
    
    def contextMenuEvent(self, event):
       
           cmenu = QMenu(self)
           
           newAct = cmenu.addAction("New")
           opnAct = cmenu.addAction("Open")
           quitAct = cmenu.addAction("Quit")
           action = cmenu.exec_(self.mapToGlobal(event.pos()))
           
           if action == quitAct:
               qApp.quit()
       
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

仍是使用contextMenuEvent()方法實現這個菜單。

action = cmenu.exec_(self.mapToGlobal(event.pos()))

使用exec_()方法顯示菜單。從鼠標右鍵事件對象中得到當前座標。mapToGlobal()方法把當前組件的相對座標轉換爲窗口(window)的絕對座標。

if action == quitAct:
    qApp.quit()

若是右鍵菜單裏觸發了事件,也就觸發了退出事件,執行關閉菜單行爲。

程序預覽:

工具欄

菜單欄包含了全部的命令,工具欄就是經常使用的命令的集合。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a toolbar.
The toolbar has one action, which
terminates the application, if triggered.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

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

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        
        exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.triggered.connect(qApp.quit)
        
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAct)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Toolbar')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

上面的例子中,咱們建立了一個工具欄。這個工具欄只有一個退出應用的動做。

exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit)

和上面的菜單欄差很少,這裏使用了一個行爲對象,這個對象綁定了一個標籤,一個圖標和一個快捷鍵。這些行爲被觸發的時候,會調用QtGui.QMainWindow的quit方法退出應用。

self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAct)

把工具欄展現出來。

程序預覽:

主窗口

主窗口就是上面三種欄目的總稱,如今咱們把上面的三種欄在一個應用裏展現出來。

首先要本身弄個小圖標,命名爲exit24.png
#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a skeleton of
a classic GUI application with a menubar,
toolbar, statusbar, and a central widget. 

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017

學習交流:923414804
"""

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


class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        
        textEdit = QTextEdit()
        self.setCentralWidget(textEdit)

        exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(self.close)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)

        toolbar = self.addToolBar('Exit')
        toolbar.addAction(exitAct)
        
        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('Main window')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

上面的代碼建立了一個很經典的菜單框架,有右鍵菜單,工具欄和狀態欄。

textEdit = QTextEdit()
self.setCentralWidget(textEdit)

這裏建立了一個文本編輯區域,並把它放在QMainWindow的中間區域。這個組件或佔滿全部剩餘的區域。

程序預覽:

相關文章
相關標籤/搜索