方法
描述
menuBar()
返回主窗口的 QMenuBar 对象
addMenu()
在菜单栏中添加一个新的 QMenu 对象
addAction()
向 QMenu 小控件添加一个操作按钮,其中包含文本或图标
setEnable()
将操作按钮状态设置为启用/禁用
addSeperator()
在菜单中添加一条分隔线
clear()
删除菜单/菜单栏中的内容
setShortcut()
将快捷键关联到操作按钮
setText()
设置菜单项的文本
setTitle()
设置 QMenu 小控件的标题
text()
返回与 QAction 对象关联的文本
title()
返回 QMenu 小控件的标题
QMenuBar 使用示例:
# -*- coding:utf-8 -*-
# Time : 2019/08/18 下午 4:42
# Author : 御承扬
# e-mail:2923616405@qq.com
# project: PyQt5
# File : qt34_MenuTest.py
# @software: PyCharm
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MenuDemo(QMainWindow):
def __init__(self, parent=None):
super(MenuDemo, self).__init__(parent)
self.setWindowTitle("Menu 示例")
self.setWindowIcon(QIcon("./images/Python2.ico"))
layout = QHBoxLayout()
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
save = QAction("Save", self)
save.setShortcut("Ctrl+S")
file.addAction(save)
edit = file.addMenu("Edit")
edit.addAction("copy")
edit.addAction("paste")
quit = QAction("Quit", self)
file.addAction(quit)
file.triggered[QAction].connect(self.processTrigger)
self.setLayout(layout)
@staticmethod
def processTrigger(q):
print(q.text() + " is triggered")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MenuDemo()
win.show()
sys.exit(app.exec_())
控制台输出:
Quit is triggered
New is triggered
Save is triggered
copy is triggered
paste is triggered
方法
描述
addAction()
添加具有文本或图标的工具按钮
addSeperator()
分组显示工具按钮
addWidget()
添加工具栏中按钮以外的控件
addToolBar()
使用 QMainWindow 类的方法添加一个新的工具栏
setMovable()
工具栏变得可移动
setOrientation()
工具栏方向可以设置为 Qt.Horizontal 或 Qt.vertical
QToolBar 使用例子:
# -*- coding:utf-8 -*-
# Time : 2019/08/18 下午 8:23
# Author : 御承扬
# e-mail:2923616405@qq.com
# project: PyQt5
# File : qt35_ToolBar.py
# @software: PyCharm
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ToolBarDemo(QMainWindow):
def __init__(self, parent=None):
super(ToolBarDemo, self).__init__(parent)
self.setWindowTitle("工具栏示例")
self.setWindowIcon(QIcon("./images/Python2.ico"))
self.resize(300, 200)
layout = QVBoxLayout()
tb = self.addToolBar("File")
new = QAction(QIcon("./images/new.png"), "new", self)
tb.addAction(new)
open = QAction(QIcon("./images/open.png"), "open", self)
tb.addAction(open)
save = QAction(QIcon("./images/save.png"), "save", self)
tb.addAction(save)
tb.actionTriggered[QAction].connect(self.toolBtnPressed)
self.setLayout(layout)
@staticmethod
def toolBtnPressed(a):
print("pressed tool button is ", a.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
demo = ToolBarDemo()
demo.show()
sys.exit(app.exec_())
方法
描述
addWidget()
在状态栏中添加给定的窗口小控件对象
addPermanentWidget()
在状态栏中永久添加给定的窗口小控件对象
showMessage()
在状态栏中显示一条临时信息指定时间间隔
clearMessage()
删除正在显示的临时信息
removeWidget()
从状态栏中删除指定的小控件
QStatusBar 示例:
# -*- coding:utf-8 -*-
# Time : 2019/08/18 下午 9:05
# Author : 御承扬
# e-mail:2923616405@qq.com
# project: PyQt5
# File : qt36_StatusBar.py
# @software: PyCharm
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class StatusDemo(QMainWindow):
def __init__(self, parent=None):
super(StatusDemo, self).__init__(parent)
self.setWindowTitle("状态栏示例")
self.setWindowIcon(QIcon("./images/Python2.ico"))
menuBar = self.menuBar()
file = menuBar.addMenu("File")
file.addAction("show")
file.triggered[QAction].connect(self.processTrigger)
self.setCentralWidget(QTextEdit())
self.statusBar = QStatusBar()
self.setStatusBar(self.statusBar)
def processTrigger(self, q):
if q.text() == "show":
self.statusBar.showMessage(q.text() + " 菜单选项被点击了", 5000)
if __name__ == "__main__":
app = QApplication(sys.argv)
demo = StatusDemo()
demo.show()
sys.exit(app.exec_())
QPrinter 使用示例
# -*- coding:utf-8 -*-
# Time : 2019/08/19 下午 9:04
# Author : 御承扬
# e-mail:2923616405@qq.com
# project: PyQt5
# File : qt37_printer.py
# @software: PyCharm
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.PrintAction = QAction(QIcon("./images/printer.png"), self.tr("打印"), self)
self.setWindowTitle(self.tr("打印图片"))
self.setWindowIcon(QIcon("./images/Python2.ico"))
self.imageLabel = QLabel()
self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.setCentralWidget(self.imageLabel)
self.image = QImage()
self.createActions()
self.createMenus()
self.createToolBars()
if self.image.load("./images/back.jpg"):
self.imageLabel.setPixmap(QPixmap.fromImage(self.image))
self.resize(self.image.width(), self.image.height())
def createActions(self):
self.PrintAction.setShortcut("Ctrl+P")
self.PrintAction.setStatusTip(self.tr("打印"))
self.PrintAction.triggered.connect(self.slotPrint)
def createMenus(self):
PrintMenu = self.menuBar().addMenu(self.tr("打印"))
PrintMenu.addAction(self.PrintAction)
def createToolBars(self):
fileToolBar = self.addToolBar("Print")
fileToolBar.addAction(self.PrintAction)
def slotPrint(self):
printer = QPrinter()
printDialog = QPrintDialog()
if printDialog.exec_():
painter = QPainter(printer)
rect = painter.viewport()
size = self.image.size()
size.scale(rect.size(), Qt.KeepAspectRatio)
painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
painter.setWindow(self.image.rect())
painter.drawImage(0, 0, self.image)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
原网址: 访问
创建于: 2024-05-23 17:25:31
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论