Getting Started with PyQt5 (11) Menu Bar & Tool Bar & Status Bar & Use Printer & Display Print Dialog Box (including output as PDF)

table of Contents

1. Create and use menus

2. Create and use the toolbar

Three. Create and use the status bar 

Four. Use the printer

Five. Display the print dialog box


1. Create and use menus

Code:

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_())


operation result:

 

 2. Create and use the toolbar

The toolbar is generally at the bottom of the menu, such as in word:

Code:

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_())


operation result:

 

 

 Three. Create and use the status bar 

Used to display status information, usually at the bottom of the window

Example:

pycharm bottom right corner

Code:

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_())


operation result:

   After clicking show in File, the status bar below will display information for five seconds.

 

 

Four. Use the printer

The output is output in the form of images and output to the printer.

Code:

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_())

operation result:

Connect the printer and see the result

 

Five. Display the print dialog box

Code:

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_())

operation result:

Try it yourself, it's fun, no printer can output to pdf file 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/113248533