PyQt5入门(十一)菜单栏 & 工具栏 & 状态栏 &使用打印机 &显示打印对话框(含输出为PDF)

目录

一.创建和使用菜单

二.创建和使用工具栏

三.创建和使用状态栏 

四.使用打印机

五.显示打印对话框


一.创建和使用菜单

代码:

import sys
from PyQt5.QtWidgets import *


class Menu(QMainWindow):
    def __init__(self):
        super(Menu, self).__init__()
        bar=self.menuBar() #这个无自动补全,需要手敲

        file=bar.addMenu('文件') #顶层菜单栏
        # 文件的子菜单(法一:直接传文本,内部会自动创建动作QAction)
        file.addAction('新建')

        #法二:自己用动作来创建子菜单
        save=QAction('保存',self) #必须加self,代表在当前窗口加QAction
        save.setShortcut('Ctrl+S')#快捷键
        file.addAction(save)
        quit=QAction('退出',self)
        file.addAction(quit)

        edit=bar.addMenu('Edit')#顶层菜单
        edit.addAction('copy')#子菜单
        edit.addAction('paste')

        save.triggered.connect(self.process)

        self.resize(400,300)

    #槽函数
    #事件自动传给槽函数的一个实参,在本例具体指的是菜单项是否被选中,是一个bool类型的值
    def process(self,a):
        print(self.sender().text())#注意这里是self.而不是a.


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=Menu()
    main.show()
    sys.exit(app.exec_())


运行结果:

 二.创建和使用工具栏

工具栏一般在菜单的下方,比如word里的:

代码:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


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

    def initUI(self):
        self.setWindowTitle('工具栏例子')
        self.resize(300,200)

        tb1=self.addToolBar('File') #一行工具栏
        '''
        工具栏默认按钮:只显示图标,将文本作为悬停提示
        即鼠标悬停到图标上之后,提示就是下面的第二个参数
        '''
        new=QAction(QIcon('../picture/LinuxLogo.jpg'),'new',self)#self代表放在当前窗口上
        tb1.addAction(new)

        open=QAction(QIcon('../picture/WindowsLogo.jpg'),'open',self)
        tb1.addAction(open)

        save=QAction(QIcon('../picture/fairy.jpg'),'save',self)
        tb1.addAction(save)
        '''
        工具栏按钮有3中显示状态
        1.只显示图标
        2.只显示文本
        3.同时显示文本和图标
        '''
        # 设置工具栏按钮显示状态:既显示文本又显示图标
        tb1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)#设置文本在图标的下方显示,还有好多,按ctrl查看自己试试

        tb2=self.addToolBar('File1')
        new1=QAction(QIcon('../picture/LinuxLogo.jpg'),'新建',self)
        tb2.addAction(new1)
        tb2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        tb1.actionTriggered.connect(self.toolbtnpressed)
        tb2.actionTriggered.connect(self.toolbtnpressed)

    def toolbtnpressed(self,a):
        print('按下的工具栏按钮是',a.text())


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=Toolbar()
    main.show()
    sys.exit(app.exec_())


运行结果:

 

 三.创建和使用状态栏 

用于显示状态信息,一般在窗口的最下方

例:

pycharm的右下角

代码:

import sys
from PyQt5.QtWidgets import *


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

    def initUI(self):
        self.setWindowTitle('状态栏演示')
        self.resize(300,200)

        bar=self.menuBar() #顶层菜单栏
        file=bar.addMenu('File') #给菜单栏添加选项
        file.addAction('show')#子菜单

        file.triggered.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)#信息显示5秒


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=Statusbar()
    main.show()
    sys.exit(app.exec_())


运行结果:

   点击了File里的show之后下面状态栏就会显示五秒信息。

四.使用打印机

输出都是以图像形式输出,输出到打印机。

代码:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtGui,QtPrintSupport


class PrintSupport(QMainWindow):
    def __init__(self):
        super(PrintSupport, self).__init__()
        self.setGeometry(500,200,300,300)
        self.btn=QPushButton('打印QTextEdit控件中的内容',self)
        self.btn.setGeometry(20,20,260,30)# x,y,w,h
        self.editor=QTextEdit('默认文本',self)
        self.editor.setGeometry(20,60,260,200)

        self.btn.clicked.connect(self.print)

    def print(self):
        #打印机对象
        printer=QtPrintSupport.QPrinter()
        painter=QtGui.QPainter()
        #将绘制的目标重定向到打印机上
        painter.begin(printer)#painter画在begin的参数上,即printer上,若是self,则画在当前窗口上
        #获得多行输入控件editor的整个框架
        screen=self.editor.grab()
        #从(10,10)开始将screen上的文字输出到打印机上
        #drawPixmap:从图像文件中提取 Pixmap 并将其显示在指定位置
        painter.drawPixmap(10,10,screen)
        painter.end()
        print('print')


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=PrintSupport()
    main.show()
    sys.exit(app.exec_())

运行结果:

连接打印机就看到结果了

五.显示打印对话框

代码:

import sys
from PyQt5.QtPrintSupport import QPrinter,QPageSetupDialog,QPrintDialog
from PyQt5.QtWidgets import *


class PrintDialog(QMainWindow):
    def __init__(self):
        super(PrintDialog, self).__init__()
        #打印机对象
        self.printer=QPrinter()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 400)
        self.setWindowTitle('打印对话框')

        self.editor = QTextEdit(self)
        self.editor.setGeometry(20, 20, 300, 270)

        self.openButton = QPushButton('打开文件', self)
        self.openButton.move(350, 20)

        self.settingButton = QPushButton('打印设置', self)
        self.settingButton.move(350, 50)

        self.printButton = QPushButton('打印文档', self)
        self.printButton.move(350, 80)

        self.openButton.clicked.connect(self.openFile)
        self.settingButton.clicked.connect(self.showSettingDialog)
        self.printButton.clicked.connect(self.showPrintDialog)

    #打开文件
    def openFile(self):
        fname=QFileDialog.getOpenFileName(self,'打开文本文件','./')
        print(fname)
        print(fname[0])
        if fname[0]:
            with open(fname[0],'r',encoding='utf-8',errors='ignore') as f:
                self.editor.setText(f.read())

    #显示打印设置对话框
    def showSettingDialog(self):
        printDialog=QPageSetupDialog(self.printer,self)
        printDialog.exec()

    #显示打印对话框
    def showPrintDialog(self):
        printdailog=QPrintDialog(self.printer,self)
        if QDialog.Accepted == printdailog.exec():
            #将editor里的文字输出到打印机中
            self.editor.print(self.printer)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=PrintDialog()
    main.show()
    sys.exit(app.exec_())

运行结果:

自己试试,挺好玩,没有打印机可以输出为pdf文档 

猜你喜欢

转载自blog.csdn.net/weixin_44593822/article/details/113248533