在较为大型复杂,功能较多的应用程序中,我们通常继承QMainWindow类来进行开发。该主窗口为搭建应用用户界面提供了非常好的框架,请看下图:
可以看出该主窗口类为我们提供了菜单栏(Menu Bar)、工具栏(Tool Bar)、控件停靠区域(Dock Widgets)和状态栏(Status Bar),我们可以往其中加入很多自己想要的东西,这也使得我们可以快速地开发一个功能复杂并且界面友好的应用程序
例子一记事本应用
接下来我们完成一个简易的记事本应用来了解一下QMainWindow的用法
1 #资料 https://blog.csdn.net/La_vie_est_belle/article/details/82819766 2 3 import sys 4 from PyQt5.QtGui import QIcon 5 from PyQt5.QtCore import QMimeData 6 from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog, QMessageBox,\ 7 QFontDialog, QColorDialog 8 9 class Demo(QMainWindow): 10 is_saved = True 11 is_saved_first = True 12 path = '' 13 14 def __init__(self): 15 super(Demo, self).__init__() 16 self.file_menu = self.menuBar().addMenu('File') #给菜单栏添加一个菜单 17 self.edit_menu = self.menuBar().addMenu('Edit') 18 self.help_menu = self.menuBar().addMenu('Help') 19 20 self.file_toolbar = self.addToolBar('File') #给工具栏添加一个按钮 21 self.edit_toolbar = self.addToolBar('Edit') 22 23 self.status_bar = self.statusBar() #设置一个状态栏 24 #接下来我们只需要往菜单栏和工具栏上面添加各种动作——也就是QAction,该类通常与菜单栏和工具栏搭配使用。可以将一个动作看作一种命令,每当用户点击某个动作时,就会触发某种命令,程序从而执行相应的命令 25 self.new_action = QAction('New', self) #实例化一个动作 26 self.open_action = QAction('Open', self) 27 self.save_action = QAction('Save', self) 28 self.save_as_action = QAction('Save As', self) 29 self.close_action = QAction('Close', self) 30 self.cut_action = QAction('Cut', self) 31 self.copy_action = QAction('Copy', self) 32 self.paste_action = QAction('Paste', self) 33 self.font_action = QAction('Font', self) 34 self.color_action = QAction('Color', self) 35 self.about_action = QAction('Qt', self) 36 37 self.text_edit = QTextEdit(self) 38 self.mime_data = QMimeData() 39 self.clipboard = QApplication.clipboard() 40 41 self.setCentralWidget(self.text_edit) # 设置中央控件 42 self.resize(450, 600) 43 44 self.menu_init() 45 self.toolbar_init() 46 self.status_bar_init() 47 self.action_init() #执行动作函数 48 self.text_edit_int() 49 50 def action_init(self): #动作函数 51 #图片下载地址:链接: https://pan.baidu.com/s/1Rp2-G_8PdvFfDIoIesMNEg 提取码: ag8w 52 self.new_action.setIcon(QIcon('images/f1.ico')) # 给动作设置图标(菜单项的左侧显示,按钮显示) 53 self.new_action.setShortcut('Ctrl+N') #给动作设置快捷键 54 self.new_action.setToolTip('创建新文件') #设置气泡提示【鼠标在工具栏按钮上时提示的内容】 55 self.new_action.setStatusTip('Create a new file')#设置在状态栏提示的信息。当鼠标停留在该动作上时,状态栏会显示相应的信息 56 self.new_action.triggered.connect(self.new_func) #动作的触发信号 57 # 58 #self.open_action.setIcon(QIcon('images/open.ico')) 59 self.open_action.setShortcut('Ctrl+O') 60 self.open_action.setToolTip('Open an existing file') 61 self.open_action.setStatusTip('Open an existing file') 62 self.open_action.triggered.connect(self.open_file_func) 63 # 64 #self.save_action.setIcon(QIcon('images/save.ico')) 65 self.save_action.setShortcut('Ctrl+S') 66 self.save_action.setToolTip('Save the file') 67 self.save_action.setStatusTip('Save the file') 68 self.save_action.triggered.connect(lambda: self.save_func(self.text_edit.toHtml())) 69 # 70 #self.save_as_action.setIcon(QIcon('images/save_as.ico')) 71 self.save_as_action.setShortcut('Ctrl+A') 72 self.save_as_action.setToolTip('Save the file to a specified location') 73 self.save_as_action.setStatusTip('Save the file to a specified location') 74 self.save_as_action.triggered.connect(lambda: self.save_as_func(self.text_edit.toHtml())) 75 # 76 #self.close_action.setIcon(QIcon('images/close.ico')) 77 self.close_action.setShortcut('Ctrl+E') 78 self.close_action.setToolTip('Close the window') 79 self.close_action.setStatusTip('Close the window') 80 self.close_action.triggered.connect(self.close_func) 81 # 82 #self.cut_action.setIcon(QIcon('images/cut.ico')) 83 self.cut_action.setShortcut('Ctrl+X') 84 self.cut_action.setToolTip('Cut the text to clipboard') 85 self.cut_action.setStatusTip('Cut the text') 86 self.cut_action.triggered.connect(self.cut_func) 87 # 88 # self.copy_action.setIcon(QIcon('images/copy.ico')) 89 self.copy_action.setShortcut('Ctrl+C') 90 self.copy_action.setToolTip('Copy the text') 91 self.copy_action.setStatusTip('Copy the text') 92 self.copy_action.triggered.connect(self.copy_func) 93 # 94 # self.paste_action.setIcon(QIcon('images/paste.ico')) 95 self.paste_action.setShortcut('Ctrl+V') 96 self.paste_action.setToolTip('Paste the text') 97 self.paste_action.setStatusTip('Paste the text') 98 self.paste_action.triggered.connect(self.paste_func) 99 # 100 # self.font_action.setIcon(QIcon('images/font.ico')) 101 self.font_action.setShortcut('Ctrl+T') 102 self.font_action.setToolTip('Change the font') 103 self.font_action.setStatusTip('Change the font') 104 self.font_action.triggered.connect(self.font_func) 105 # 106 self.color_action.setIcon(QIcon('images/color.ico')) 107 self.color_action.setShortcut('Ctrl+R') 108 self.color_action.setToolTip('Change the color') 109 self.color_action.setStatusTip('Change the color') 110 self.color_action.triggered.connect(self.color_func) 111 # 112 # self.about_action.setIcon(QIcon('images/about.ico')) 113 # self.about_action.setShortcut('Ctrl+Q') 114 # self.about_action.setToolTip('What is Qt?') 115 # self.about_action.setStatusTip('What is Qt?') 116 # self.about_action.triggered.connect(self.about_func) 117 118 def text_edit_int(self): 119 self.text_edit.textChanged.connect(self.text_changed_func) 120 121 def text_changed_func(self): 122 if self.text_edit.toPlainText(): 123 self.is_saved = False 124 else: 125 self.is_saved = True 126 127 def font_func(self): 128 font, ok = QFontDialog.getFont() 129 if ok: 130 self.text_edit.setFont(font) 131 132 def color_func(self): 133 color = QColorDialog.getColor() 134 if color.isValid(): 135 self.text_edit.setTextColor(color) 136 137 def paste_func(self): 138 self.text_edit.insertHtml(self.clipboard.mimeData().html()) 139 140 def copy_func(self): 141 self.mime_data.setHtml(self.text_edit.textCursor().selection().toHtml()) 142 self.clipboard.setMimeData(self.mime_data) 143 144 def cut_func(self): 145 self.mime_data.setHtml(self.text_edit.textCursor().selection().toHtml()) 146 self.clipboard.setMimeData(self.mime_data) 147 self.text_edit.textCursor().removeSelectedText() 148 149 def close_func(self): 150 if not self.is_saved: 151 choice = QMessageBox.question(self, 'Save File', 'Do you want to save the text?', 152 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 153 if choice == QMessageBox.Yes: 154 self.save_func(self.text_edit.toHtml()) 155 self.close() 156 elif choice == QMessageBox.No: 157 self.close() 158 else: 159 pass 160 161 def closeEvent(self, QCloseEvent): 162 if not self.is_saved: 163 choice = QMessageBox.question(self, 'Save File', 'Do you want to save the text?', 164 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 165 if choice == QMessageBox.Yes: 166 self.save_func(self.text_edit.toHtml()) 167 QCloseEvent.accept() 168 elif choice == QMessageBox.No: 169 QCloseEvent.accept() 170 else: 171 QCloseEvent.ignore() 172 173 def save_as_func(self, text): 174 self.path, _ = QFileDialog.getSaveFileName(self, 'Save File', './', 'Files (*.html *.txt *.log)') 175 if self.path: 176 with open(self.path, 'w') as f: 177 f.write(text) 178 self.is_saved = True 179 self.is_saved_first = False 180 181 def open_file_func(self): 182 if not self.is_saved: 183 choice = QMessageBox.question(self, '', 'Do you want to save the text?', 184 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 185 if choice == QMessageBox.Yes: 186 self.save_func() 187 file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.html *.txt *.log)') 188 if file: 189 with open(file, 'r') as f: 190 self.text_edit.clear() 191 self.text_edit.setText(f.read()) 192 self.is_saved = True 193 elif choice == QMessageBox.No: 194 file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.html *.txt *.log)') 195 if file: 196 with open(file, 'r') as f: 197 self.text_edit.clear() 198 self.text_edit.setText(f.read()) 199 self.is_saved = True 200 else: 201 pass 202 else: 203 file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.html *.txt *.log)') 204 if file: 205 with open(file, 'r') as f: 206 self.text_edit.clear() 207 self.text_edit.setText(f.read()) 208 self.is_saved = True 209 210 def new_func(self): 211 if not self.is_saved: 212 choice = QMessageBox.question(self, '', 'Do you want to save the text?', 213 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 214 if choice == QMessageBox.Yes: 215 self.save_func() 216 self.text_edit.clear() 217 elif choice == QMessageBox.No: 218 self.text_edit.clear() 219 else: 220 pass 221 else: 222 self.text_edit.clear() 223 224 def save_func(self, text): 225 if self.is_saved_first: 226 self.save_as_func(text) 227 else: 228 with open(self.path, 'w') as f: 229 f.write(text) 230 self.is_saved = True 231 232 def save_as_func(self, text): 233 self.path, _ = QFileDialog.getSaveFileName(self, 'Save File', './', 'Files (*.html *.txt *.log)') 234 if self.path: 235 with open(self.path, 'w') as f: 236 f.write(text) 237 self.is_saved = True 238 self.is_saved_first = False 239 240 def status_bar_init(self): 241 self.status_bar.showMessage('Ready to compose') #设置状态栏显示的内容 242 243 def toolbar_init(self): 244 self.file_toolbar.addAction(self.new_action) #给工具栏添加按钮动作 245 #注意:菜单栏、工具栏相同动作是同一个QAction对象 246 self.file_toolbar.addAction(self.open_action) 247 self.file_toolbar.addAction(self.save_action) 248 self.file_toolbar.addAction(self.save_as_action) 249 250 self.edit_toolbar.addAction(self.cut_action) 251 self.edit_toolbar.addAction(self.copy_action) 252 self.edit_toolbar.addAction(self.paste_action) 253 self.edit_toolbar.addAction(self.font_action) 254 self.edit_toolbar.addAction(self.color_action) 255 256 def save_func(self, text): 257 if self.is_saved_first: 258 self.save_as_func(text) 259 else: 260 with open(self.path, 'w') as f: 261 f.write(text) 262 self.is_saved = True 263 264 def menu_init(self): 265 self.file_menu.addAction(self.new_action) #给菜单添加一个动作 266 self.file_menu.addAction(self.open_action) 267 self.file_menu.addAction(self.save_action) 268 self.file_menu.addAction(self.save_as_action) 269 self.file_menu.addSeparator() #加一个分割条 270 self.file_menu.addAction(self.close_action) 271 272 self.edit_menu.addAction(self.cut_action) 273 self.edit_menu.addAction(self.copy_action) 274 self.edit_menu.addAction(self.paste_action) 275 self.edit_menu.addSeparator() 276 self.edit_menu.addAction(self.font_action) 277 self.edit_menu.addAction(self.color_action) 278 279 self.help_menu.addAction(self.about_action) 280 281 282 if __name__ == '__main__': 283 app = QApplication(sys.argv) 284 demo = Demo() 285 demo.show() 286 sys.exit(app.exec_())
启动画面QSplashScreen
1 #资料 https://blog.csdn.net/La_vie_est_belle/article/details/82819766 2 3 import sys 4 import time 5 from PyQt5.QtGui import QIcon, QPixmap 6 from PyQt5.QtCore import QMimeData, Qt 7 from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog, QMessageBox,\ 8 QFontDialog, QColorDialog, QSplashScreen 9 10 11 class Demo(QMainWindow): 12 is_saved = True 13 is_saved_first = True 14 path = '' 15 16 def __init__(self): 17 super(Demo, self).__init__() 18 self.file_menu = self.menuBar().addMenu('File') #给菜单栏添加一个菜单 19 self.edit_menu = self.menuBar().addMenu('Edit') 20 self.help_menu = self.menuBar().addMenu('Help') 21 22 self.file_toolbar = self.addToolBar('File') #给工具栏添加一个按钮 23 self.edit_toolbar = self.addToolBar('Edit') 24 25 self.status_bar = self.statusBar() #设置一个状态栏 26 #接下来我们只需要往菜单栏和工具栏上面添加各种动作——也就是QAction,该类通常与菜单栏和工具栏搭配使用。可以将一个动作看作一种命令,每当用户点击某个动作时,就会触发某种命令,程序从而执行相应的命令 27 self.new_action = QAction('New', self) #实例化一个动作 28 self.open_action = QAction('Open', self) 29 self.save_action = QAction('Save', self) 30 self.save_as_action = QAction('Save As', self) 31 self.close_action = QAction('Close', self) 32 self.cut_action = QAction('Cut', self) 33 self.copy_action = QAction('Copy', self) 34 self.paste_action = QAction('Paste', self) 35 self.font_action = QAction('Font', self) 36 self.color_action = QAction('Color', self) 37 self.about_action = QAction('Qt', self) 38 39 self.text_edit = QTextEdit(self) 40 self.mime_data = QMimeData() 41 self.clipboard = QApplication.clipboard() 42 43 self.setCentralWidget(self.text_edit) # 设置中央控件 44 self.resize(450, 600) 45 46 self.menu_init() 47 self.toolbar_init() 48 self.status_bar_init() 49 self.action_init() #执行动作函数 50 self.text_edit_int() 51 52 def action_init(self): #动作函数 53 #图片下载地址:链接: https://pan.baidu.com/s/1Rp2-G_8PdvFfDIoIesMNEg 提取码: ag8w 54 self.new_action.setIcon(QIcon('images/f1.ico')) # 给动作设置图标 55 self.new_action.setShortcut('Ctrl+N') #给动作设置快捷键 56 self.new_action.setToolTip('创建新文件') #设置气泡提示【鼠标在工具栏按钮上时提示的内容】 57 self.new_action.setStatusTip('Create a new file')#设置在状态栏提示的信息。当鼠标停留在该动作上时,状态栏会显示相应的信息 58 self.new_action.triggered.connect(self.new_func) #动作的触发信号 59 # 60 #self.open_action.setIcon(QIcon('images/open.ico')) 61 self.open_action.setShortcut('Ctrl+O') 62 self.open_action.setToolTip('Open an existing file') 63 self.open_action.setStatusTip('Open an existing file') 64 self.open_action.triggered.connect(self.open_file_func) 65 # 66 #self.save_action.setIcon(QIcon('images/save.ico')) 67 self.save_action.setShortcut('Ctrl+S') 68 self.save_action.setToolTip('Save the file') 69 self.save_action.setStatusTip('Save the file') 70 self.save_action.triggered.connect(lambda: self.save_func(self.text_edit.toHtml())) 71 # 72 #self.save_as_action.setIcon(QIcon('images/save_as.ico')) 73 self.save_as_action.setShortcut('Ctrl+A') 74 self.save_as_action.setToolTip('Save the file to a specified location') 75 self.save_as_action.setStatusTip('Save the file to a specified location') 76 self.save_as_action.triggered.connect(lambda: self.save_as_func(self.text_edit.toHtml())) 77 # 78 #self.close_action.setIcon(QIcon('images/close.ico')) 79 self.close_action.setShortcut('Ctrl+E') 80 self.close_action.setToolTip('Close the window') 81 self.close_action.setStatusTip('Close the window') 82 self.close_action.triggered.connect(self.close_func) 83 # 84 #self.cut_action.setIcon(QIcon('images/cut.ico')) 85 self.cut_action.setShortcut('Ctrl+X') 86 self.cut_action.setToolTip('Cut the text to clipboard') 87 self.cut_action.setStatusTip('Cut the text') 88 self.cut_action.triggered.connect(self.cut_func) 89 # 90 # self.copy_action.setIcon(QIcon('images/copy.ico')) 91 self.copy_action.setShortcut('Ctrl+C') 92 self.copy_action.setToolTip('Copy the text') 93 self.copy_action.setStatusTip('Copy the text') 94 self.copy_action.triggered.connect(self.copy_func) 95 # 96 # self.paste_action.setIcon(QIcon('images/paste.ico')) 97 self.paste_action.setShortcut('Ctrl+V') 98 self.paste_action.setToolTip('Paste the text') 99 self.paste_action.setStatusTip('Paste the text') 100 self.paste_action.triggered.connect(self.paste_func) 101 # 102 # self.font_action.setIcon(QIcon('images/font.ico')) 103 self.font_action.setShortcut('Ctrl+T') 104 self.font_action.setToolTip('Change the font') 105 self.font_action.setStatusTip('Change the font') 106 self.font_action.triggered.connect(self.font_func) 107 # 108 self.color_action.setIcon(QIcon('images/color.ico')) 109 self.color_action.setShortcut('Ctrl+R') 110 self.color_action.setToolTip('Change the color') 111 self.color_action.setStatusTip('Change the color') 112 self.color_action.triggered.connect(self.color_func) 113 # 114 # self.about_action.setIcon(QIcon('images/about.ico')) 115 # self.about_action.setShortcut('Ctrl+Q') 116 # self.about_action.setToolTip('What is Qt?') 117 # self.about_action.setStatusTip('What is Qt?') 118 # self.about_action.triggered.connect(self.about_func) 119 120 def text_edit_int(self): 121 self.text_edit.textChanged.connect(self.text_changed_func) 122 123 def text_changed_func(self): 124 if self.text_edit.toPlainText(): 125 self.is_saved = False 126 else: 127 self.is_saved = True 128 129 def font_func(self): 130 font, ok = QFontDialog.getFont() 131 if ok: 132 self.text_edit.setFont(font) 133 134 def color_func(self): 135 color = QColorDialog.getColor() 136 if color.isValid(): 137 self.text_edit.setTextColor(color) 138 139 def paste_func(self): 140 self.text_edit.insertHtml(self.clipboard.mimeData().html()) 141 142 def copy_func(self): 143 self.mime_data.setHtml(self.text_edit.textCursor().selection().toHtml()) 144 self.clipboard.setMimeData(self.mime_data) 145 146 def cut_func(self): 147 self.mime_data.setHtml(self.text_edit.textCursor().selection().toHtml()) 148 self.clipboard.setMimeData(self.mime_data) 149 self.text_edit.textCursor().removeSelectedText() 150 151 def close_func(self): 152 if not self.is_saved: 153 choice = QMessageBox.question(self, 'Save File', 'Do you want to save the text?', 154 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 155 if choice == QMessageBox.Yes: 156 self.save_func(self.text_edit.toHtml()) 157 self.close() 158 elif choice == QMessageBox.No: 159 self.close() 160 else: 161 pass 162 163 def closeEvent(self, QCloseEvent): 164 if not self.is_saved: 165 choice = QMessageBox.question(self, 'Save File', 'Do you want to save the text?', 166 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 167 if choice == QMessageBox.Yes: 168 self.save_func(self.text_edit.toHtml()) 169 QCloseEvent.accept() 170 elif choice == QMessageBox.No: 171 QCloseEvent.accept() 172 else: 173 QCloseEvent.ignore() 174 175 def save_as_func(self, text): 176 self.path, _ = QFileDialog.getSaveFileName(self, 'Save File', './', 'Files (*.html *.txt *.log)') 177 if self.path: 178 with open(self.path, 'w') as f: 179 f.write(text) 180 self.is_saved = True 181 self.is_saved_first = False 182 183 def open_file_func(self): 184 if not self.is_saved: 185 choice = QMessageBox.question(self, '', 'Do you want to save the text?', 186 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 187 if choice == QMessageBox.Yes: 188 self.save_func() 189 file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.html *.txt *.log)') 190 if file: 191 with open(file, 'r') as f: 192 self.text_edit.clear() 193 self.text_edit.setText(f.read()) 194 self.is_saved = True 195 elif choice == QMessageBox.No: 196 file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.html *.txt *.log)') 197 if file: 198 with open(file, 'r') as f: 199 self.text_edit.clear() 200 self.text_edit.setText(f.read()) 201 self.is_saved = True 202 else: 203 pass 204 else: 205 file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.html *.txt *.log)') 206 if file: 207 with open(file, 'r') as f: 208 self.text_edit.clear() 209 self.text_edit.setText(f.read()) 210 self.is_saved = True 211 212 def new_func(self): 213 if not self.is_saved: 214 choice = QMessageBox.question(self, '', 'Do you want to save the text?', 215 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 216 if choice == QMessageBox.Yes: 217 self.save_func() 218 self.text_edit.clear() 219 elif choice == QMessageBox.No: 220 self.text_edit.clear() 221 else: 222 pass 223 else: 224 self.text_edit.clear() 225 226 def save_func(self, text): 227 if self.is_saved_first: 228 self.save_as_func(text) 229 else: 230 with open(self.path, 'w') as f: 231 f.write(text) 232 self.is_saved = True 233 234 def save_as_func(self, text): 235 self.path, _ = QFileDialog.getSaveFileName(self, 'Save File', './', 'Files (*.html *.txt *.log)') 236 if self.path: 237 with open(self.path, 'w') as f: 238 f.write(text) 239 self.is_saved = True 240 self.is_saved_first = False 241 242 def status_bar_init(self): 243 self.status_bar.showMessage('Ready to compose') #设置状态栏显示的内容 244 245 def toolbar_init(self): 246 self.file_toolbar.addAction(self.new_action) #给工具栏添加按钮动作 247 #注意:菜单栏、工具栏相同动作是同一个QAction对象 248 self.file_toolbar.addAction(self.open_action) 249 self.file_toolbar.addAction(self.save_action) 250 self.file_toolbar.addAction(self.save_as_action) 251 252 self.edit_toolbar.addAction(self.cut_action) 253 self.edit_toolbar.addAction(self.copy_action) 254 self.edit_toolbar.addAction(self.paste_action) 255 self.edit_toolbar.addAction(self.font_action) 256 self.edit_toolbar.addAction(self.color_action) 257 258 def save_func(self, text): 259 if self.is_saved_first: 260 self.save_as_func(text) 261 else: 262 with open(self.path, 'w') as f: 263 f.write(text) 264 self.is_saved = True 265 266 def menu_init(self): 267 self.file_menu.addAction(self.new_action) #给菜单添加一个动作 268 self.file_menu.addAction(self.open_action) 269 self.file_menu.addAction(self.save_action) 270 self.file_menu.addAction(self.save_as_action) 271 self.file_menu.addSeparator() #加一个分割条 272 self.file_menu.addAction(self.close_action) 273 274 self.edit_menu.addAction(self.cut_action) 275 self.edit_menu.addAction(self.copy_action) 276 self.edit_menu.addAction(self.paste_action) 277 self.edit_menu.addSeparator() 278 self.edit_menu.addAction(self.font_action) 279 self.edit_menu.addAction(self.color_action) 280 281 self.help_menu.addAction(self.about_action) 282 283 284 if __name__ == '__main__': 285 app = QApplication(sys.argv) 286 splash = QSplashScreen() #实例化启动画面对象 287 splash.setPixmap(QPixmap('images/splash.jpg')) #设置启动画面的图片 288 splash.show() #显示启动画面 289 splash.showMessage('欢迎使用PYQT5', 290 Qt.AlignBottom | Qt.AlignCenter, Qt.white) 291 #设置在启动画面上要显示的文字 292 #参数1:要显示的文本 293 #参数2:显示的位置。Qt.AlignBottom(垂直方向的位置) 底部;Qt.AlignCenter(水平方向的位置) 居中 294 #参数3:文字的颜色 295 time.sleep(2) 296 297 demo = Demo() 298 demo.show() 299 splash.finish(demo)#把显示画面切换到主窗口 300 sys.exit(app.exec_())