上一篇內容我們使用PyQt5構造了一個GUI程序的通用框架(圖形界面編程技巧,用Python中PyQt5庫快速創建一個應用程式框架)。這樣的圖形用戶接口相對來說比較簡單。我們知道,日常使用的圖形界面都是比較複雜的。
比如,一個應用程式中可能有很多圖形接口供用戶來使用。常見的菜單欄、工具欄、狀態欄都沒有在我們的框架中定義。
今天,帶大家了解一個功能更加豐富的GUI應用程式框架。
原始碼
不贅述了,直接上代碼
#!/usr/bin/env python
# coding:utf-8
import sys
from PyQt5.Qt import *
from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget, QWidget
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QTextEdit, QAction, QToolBar
from PyQt5.QtGui import QIcon
class MainWindow(QMainWindow):
def __init__(self, parent=None, *args, **kwargs):
super(MainWindow, self).__init__(parent, *args, **kwargs)
self.initUI()
def initUI(self):
# 窗體設置
self.setWindowTitle('PyQt MainWindow示例')
self.setWindowIcon(QIcon('icon\\main.png'))
self.resize(800, 600)
# 創建一個Action(範例,其他Acion參照這個)
self.createActions()
# 頂部菜單欄
self.createMenuBar()
# 次頂部工具欄
self.createToolBar()
# 創建狀態欄
self.createStatusBar()
# 創建主工作區
self.createMainUI()
# 窗體居中
self.center()
# 創建主工作區
def createMainUI(self):
self.qtextEdit = QTextEdit()
layout = QHBoxLayout()
layout.addWidget(self.qtextEdit)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
# 創建菜單欄
def createMenuBar(self):
self.menu = self.menuBar()
file = self.menu.addMenu('文件')
file.addAction(self.newAction)
file.addAction(self.openAction)
file.addAction(self.exitAction)
tool = self.menu.addMenu('編輯')
tool.addAction(self.copyAction)
tool.addAction(self.pasteAction)
tool.addAction(self.cutAction)
helps = self.menu.addMenu('幫助')
helps.addAction(self.helpAction)
# 創建工具欄
def createToolBar(self):
self.toolBar = QToolBar()
self.addToolBar(Qt.LeftToolBarArea,self.toolBar)
self.toolBar.addAction(self.exitAction)
self.toolBar.addAction(self.openAction)
self.toolBar.addAction(self.copyAction)
self.toolBar.addAction(self.pasteAction)
self.toolBar.addAction(self.cutAction)
# 創建狀態欄
def createStatusBar(self):
self.status = self.statusBar()
self.status.showMessage('這是狀態欄提示信息', 0)
# 創建Action
def createActions(self):
# 退出程序
self.exitAction = QAction(QIcon('icon\\exit.png'), '退出', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('退出程序')
self.exitAction.triggered.connect(self.close)
# 打開文件
self.openAction = QAction(QIcon('icon\\open.png'), '打開', self)
self.openAction.setShortcut('Alt+O')
self.openAction.setStatusTip('打開文件')
self.openAction.triggered.connect(self.open)
# 新建文件
self.newAction = QAction(QIcon('icon\\new.png'), '新建', self)
self.newAction.setShortcut('Alt+N')
self.newAction.setStatusTip('新建文件')
self.newAction.triggered.connect(self.new)
# 複製
self.copyAction = QAction(QIcon('icon\\copy.png'), '複製', self)
self.copyAction.setShortcut('Ctrl+C')
self.copyAction.setStatusTip('複製內容')
self.copyAction.triggered.connect(self.copy)
# 粘貼
self.pasteAction = QAction(QIcon('icon\\paste.png'), '粘貼', self)
self.pasteAction.setShortcut('Ctrl+V')
self.pasteAction.setStatusTip('粘貼內容')
self.pasteAction.triggered.connect(self.paste)
# 剪切
self.cutAction = QAction(QIcon('icon\\cut.png'), '剪切', self)
self.cutAction.setShortcut('Ctrl+X')
self.cutAction.setStatusTip('剪切內容')
self.cutAction.triggered.connect(self.cut)
# 幫助
self.helpAction = QAction(QIcon('icon\\help.png'), '關於', self)
self.helpAction.setShortcut('Alt+H')
self.helpAction.setStatusTip('單擊獲取幫助信息')
self.helpAction.triggered.connect(self.helps)
# 窗口位置居中
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) // 2,
(screen.height() - size.height()) // 2)
def open(self):
print('打開文件')
def new(self):
print('新建文件')
def copy(self):
print('複製內容')
def paste(self):
print('粘貼內容')
def cut(self):
print("剪切內容")
def helps(self):
print('幫助信息')
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('icon.png'))
form = MainWindow()
form.show()
sys.exit(app.exec_())
為方便大家測試,程序截圖如下
使用時,大家講函數內容粘貼到相應的位置即可。
結果展示
上面代碼我們得到了如下圖所示的GUI應用程式框架。
功能很齊全,不是嗎?標題、菜單欄、工具欄、狀態欄都有。想製作自己的GUI程序界面,直接修改相應的內容即可。
有什麼用
上面的代碼有什麼用呢?
簡化輸入代碼
我們知道,PyCharm中有個很強大的功能叫live template模板,可使用簡短的輸入來代替大量代碼的錄入。(參見還在使用PyCharm一行行敲代碼嗎?使用這個方法讓你節省一半時間),你可以將上述代碼製作成一個live template模板,以後創建自己的GUI應用程式時,你就擁有了自己的工具包……想想都很酷。
代碼更易讀、易理解
這樣封裝之後的代碼,更加容易理解,在創建各個控制項時,我們使用了相應的方法。如果你想擴展自己的程序功能,只需要修改相應的方法即可。
應用程式更容易擴展
代碼這樣封裝,使得業務邏輯和界面做到了分離(本程序並沒有分離徹底,但作為示例,為了幫助大家理解,所以沒有進行徹底分割)。大家在實際應用中可以將上面的內容進行合理的調整。
live template效果展示
此時,在編輯器中輸入qtmw,按下回車鍵後,這串100多行的代碼就自動進行了輸入,輸入程序標題和窗口大小後,運行看下結果,怎麼樣?我們不到一分鐘的時間創建了這麼一個複雜的GUI應用程式,是不是很有成就感?
好了,今天的內容就到這裡了,小夥伴們這項技能你Get到了嗎?
需要源碼的小夥伴關注並私信我,我將原始碼發給你,以後就不用一行一行敲代碼了……
喜歡Python編程的小夥伴一定要關注我哦,後續會推出一些更有意思的實戰內容。
轉載請註明出處,百家號:Python高手養成