PyQt5的学习之路(三)

时间:2022-04-06 08:33:59

状态栏

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

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

class Myform(QMainWindow):
def __init__(self):
super(Myform, self).__init__()
self.initUI()

def initUI(self):
self.statusBar().showMessage('Ready') # 创建状态栏
self.setGeometry(300, 300, 200, 200)
self.setWindowTitle('status bar')
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())

菜单栏

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

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

class Myform(QMainWindow):
def __init__(self):
super(Myform, self).__init__()

self.initUI()

def initUI(self):

self.statusBar()

exitAction = QAction(QIcon('1.jpg'), '&Exit', self) # 创建功能按钮
# exitAction.setStatusTip('Exit application')
exitAction.setShortcut('Ctrl+Q') # 为功能按钮添加快捷键
exitAction.triggered.connect(qApp.quit) # 为功能按钮绑定事件

menuBar = self.menuBar() # 添加菜单栏
QuitMenu = menuBar.addMenu('&Quit') # 菜单栏中添加Quit菜单
QuitMenu.addAction(exitAction) # 在Quit菜单中添加功能按钮


self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('menubar')
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())

工具栏

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

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

class Myform(QMainWindow):
def __init__(self):
super(Myform, self).__init__()
self.initUI()

def initUI(self):
exitAction = QAction(QIcon('1.jpg'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)

toolbar = self.addToolBar('Exit') # 在窗体内添加包含一个名为Exit按钮的工具条
toolbar.addAction(exitAction)

self.setGeometry(300, 300, 200, 150)
self.setWindowTitle('toolbar')
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())

综合

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

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

class Myform(QMainWindow):
def __init__(self):
super(Myform, self).__init__()

self.initUI()

def initUI(self):
textEdit = QTextEdit()
self.setCentralWidget(textEdit)

exitAction = QAction(QIcon('1.jpg'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)

self.statusBar()

menuBar = self.menuBar()
QuitMenu = menuBar.addMenu('&Quit')
QuitMenu.addAction(exitAction)

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

self.setGeometry(300, 300, 300, 210)
self.setWindowTitle('summary')
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())