菜單和工具欄
這個(gè)章節(jié)言缤,我們會(huì)創(chuàng)建狀態(tài)欄、菜單和工具欄导而。菜單是一組位于菜單欄的命令忱叭。工具欄是應(yīng)用的一些常用工具按鈕。狀態(tài)欄顯示一些狀態(tài)信息今艺,通常在應(yīng)用的底部韵丑。
主窗口
QMainWindow
提供了主窗口的功能,使用它能創(chuàng)建一些簡(jiǎn)單的狀態(tài)欄虚缎、工具欄和菜單欄撵彻。
主窗口是下面這些窗口的合稱,所以教程在最下方实牡。
狀態(tài)欄
狀態(tài)欄是用來(lái)顯示應(yīng)用的狀態(tài)信息的組件千康。
#!/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_())
狀態(tài)欄是由QMainWindow創(chuàng)建的。
self.statusBar().showMessage('Ready')
調(diào)用QtGui.QMainWindow
類的statusBar()
方法铲掐,創(chuàng)建狀態(tài)欄。第一次調(diào)用創(chuàng)建一個(gè)狀態(tài)欄值桩,返回一個(gè)狀態(tài)欄對(duì)象摆霉。showMessage()
方法在狀態(tài)欄上顯示一條信息。
程序預(yù)覽:
菜單欄
菜單欄是非常常用的奔坟。是一組命令的集合(Mac OS下?tīng)顟B(tài)欄的顯示不一樣携栋,為了得到最相似的外觀,我們?cè)黾恿艘痪?code>menubar.setNativeMenuBar(False))咳秉。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
學(xué)習(xí)交流:923414804
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_())
我們創(chuàng)建了只有一個(gè)命令的菜單欄婉支,這個(gè)命令就是終止應(yīng)用。同時(shí)也創(chuàng)建了一個(gè)狀態(tài)欄澜建。而且還能使用快捷鍵Ctrl+Q
退出應(yīng)用向挖。
exitAct = QAction(QIcon('exit.png'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
QAction
是菜單欄、工具欄或者快捷鍵的動(dòng)作的組合炕舵。前面兩行何之,我們創(chuàng)建了一個(gè)圖標(biāo)、一個(gè)exit的標(biāo)簽和一個(gè)快捷鍵組合咽筋,都執(zhí)行了一個(gè)動(dòng)作溶推。第三行,創(chuàng)建了一個(gè)狀態(tài)欄,當(dāng)鼠標(biāo)懸停在菜單欄的時(shí)候蒜危,能顯示當(dāng)前狀態(tài)虱痕。
exitAct.triggered.connect(qApp.quit)
當(dāng)執(zhí)行這個(gè)指定的動(dòng)作時(shí),就觸發(fā)了一個(gè)事件辐赞。這個(gè)事件跟QApplication的quit()
行為相關(guān)聯(lián)部翘,所以這個(gè)動(dòng)作就能終止這個(gè)應(yīng)用。
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
menuBar()
創(chuàng)建菜單欄占拍。這里創(chuàng)建了一個(gè)菜單欄略就,并在上面添加了一個(gè)file菜單,并關(guān)聯(lián)了點(diǎn)擊退出應(yīng)用的事件晃酒。
程序預(yù)覽:
子菜單
子菜單是嵌套在菜單里面的二級(jí)或者三級(jí)等的菜單表牢。
#!/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_())
這個(gè)例子里,有兩個(gè)子菜單贝次,一個(gè)在file菜單下面崔兴,一個(gè)在file的import下面。
impMenu = QMenu('Import', self)
使用QMenu
創(chuàng)建一個(gè)新菜單蛔翅。
impAct = QAction('Import mail', self)
impMenu.addAction(impAct)
使用addAction
添加一個(gè)動(dòng)作敲茄。
程序預(yù)覽:
勾選菜單
下面是一個(gè)能勾選菜單的例子
#!/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_())
本例創(chuàng)建了一個(gè)行為菜單。這個(gè)行為/動(dòng)作能切換狀態(tài)欄顯示或者隱藏山析。
viewStatAct = QAction('View statusbar', self, checkable=True)
用checkable
選項(xiàng)創(chuàng)建一個(gè)能選中的菜單堰燎。
viewStatAct.setChecked(True)
默認(rèn)設(shè)置為選中狀態(tài)。
def toggleMenu(self, state):
if state:
self.statusbar.show()
else:
self.statusbar.hide()
依據(jù)選中狀態(tài)切換狀態(tài)欄的顯示與否笋轨。
程序預(yù)覽:
右鍵菜單
右鍵菜單也叫彈出框(8鸭簟?)爵政,是在某些場(chǎng)合下顯示的一組命令仅讽。例如,Opera瀏覽器里钾挟,網(wǎng)頁(yè)上的右鍵菜單里會(huì)有刷新洁灵,返回或者查看頁(yè)面源代碼。如果在工具欄上右鍵掺出,會(huì)得到一個(gè)不同的用來(lái)管理工具欄的菜單徽千。
#!/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()
方法實(shí)現(xiàn)這個(gè)菜單。
action = cmenu.exec_(self.mapToGlobal(event.pos()))
使用exec_()
方法顯示菜單蛛砰。從鼠標(biāo)右鍵事件對(duì)象中獲得當(dāng)前坐標(biāo)罐栈。mapToGlobal()
方法把當(dāng)前組件的相對(duì)坐標(biāo)轉(zhuǎn)換為窗口(window)的絕對(duì)坐標(biāo)。
if action == quitAct:
qApp.quit()
如果右鍵菜單里觸發(fā)了事件泥畅,也就觸發(fā)了退出事件荠诬,執(zhí)行關(guān)閉菜單行為琅翻。
程序預(yù)覽:
工具欄
菜單欄包含了所有的命令,工具欄就是常用的命令的集合柑贞。
#!/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_())
上面的例子中方椎,我們創(chuàng)建了一個(gè)工具欄。這個(gè)工具欄只有一個(gè)退出應(yīng)用的動(dòng)作钧嘶。
exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit)
和上面的菜單欄差不多棠众,這里使用了一個(gè)行為對(duì)象,這個(gè)對(duì)象綁定了一個(gè)標(biāo)簽有决,一個(gè)圖標(biāo)和一個(gè)快捷鍵闸拿。這些行為被觸發(fā)的時(shí)候,會(huì)調(diào)用QtGui.QMainWindow
的quit方法退出應(yīng)用书幕。
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAct)
把工具欄展示出來(lái)新荤。
程序預(yù)覽:
主窗口
主窗口就是上面三種欄目的總稱,現(xiàn)在我們把上面的三種欄在一個(gè)應(yīng)用里展示出來(lái)台汇。
首先要自己弄個(gè)小圖標(biāo)苛骨,命名為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
"""
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_())
上面的代碼創(chuàng)建了一個(gè)很經(jīng)典的菜單框架,有右鍵菜單苟呐,工具欄和狀態(tài)欄痒芝。
textEdit = QTextEdit()
self.setCentralWidget(textEdit)
這里創(chuàng)建了一個(gè)文本編輯區(qū)域,并把它放在QMainWindow
的中間區(qū)域牵素。這個(gè)組件或占滿所有剩余的區(qū)域严衬。
程序預(yù)覽: