资源简介

该例程包含有各种类型的例子,代码有详细的注释,通俗易懂。能够使你快速入门掌握PYQT5,设计出优美的界面。

资源截图

代码片段和文件信息

#conding=utf-8

‘‘‘
这是一个关于文本保存以及打印文件相关对话框的小例子!
文章链接:http://www.xdbcb8.com/archives/281.html
‘‘‘

import sys
from PyQt5.QtWidgets import QWidget QApplication QPushButton QColorDialog QFontDialog QTextEdit QFileDialog QDialog
from PyQt5.QtPrintSupport import QPageSetupDialog QPrintDialog QPrinter

class Example(QWidget):
    ‘‘‘
    文本保存以及打印文件对话框
    ‘‘‘
    def __init__(self):
        ‘‘‘
        一些初始设置
        ‘‘‘
        super().__init__()
        self.printer = QPrinter()
        self.initUI()
    
    def initUI(self):
        ‘‘‘
        界面初始设置
        ‘‘‘
        self.setGeometry(300 300 500 400)
        self.setWindowtitle(‘关注微信公众号:学点编程吧--保存、打印文件‘)

        self.tx = QTextEdit(self)
        self.tx.setGeometry(20 20 300 270)
        
        self.bt1 = QPushButton(‘打开文件‘ self)
        self.bt1.move(350 20)
        self.bt2 = QPushButton(‘打开多个文件‘ self)
        self.bt2.move(350 70)
        self.bt3 = QPushButton(‘选择字体‘ self)
        self.bt3.move(350 120)
        self.bt4 = QPushButton(‘选择颜色‘ self)
        self.bt4.move(350 170)
        self.bt5 = QPushButton(‘保存文件‘ self)
        self.bt5.move(350 220)
        self.bt6 = QPushButton(‘页面设置‘ self)
        self.bt6.move(350 270)
        self.bt7 = QPushButton(‘打印文档‘ self)
        self.bt7.move(350 320)
        
        self.bt1.clicked.connect(self.openfile)
        self.bt2.clicked.connect(self.openfiles)
        self.bt3.clicked.connect(self.choicefont)
        self.bt4.clicked.connect(self.choicecolor)
        self.bt5.clicked.connect(self.savefile)
        self.bt6.clicked.connect(self.pagesettings)
        self.bt7.clicked.connect(self.printdialog)
        
        self.show()
    
    def openfile(self):
        ‘‘‘
        打开文件对话框
        ‘‘‘
        fname = QFileDialog.getOpenFileName(self ‘学点编程吧:打开文件‘ ‘./‘)
        if fname[0]:
            # fname[0]绝对路径
            with open(fname[0] ‘r‘ encoding=‘gb18030‘ errors=‘ignore‘) as f:
                self.tx.setText(f.read())
                
    def openfiles(self):
        ‘‘‘
        打开多个文件对话框
        ‘‘‘
        fnames = QFileDialog.getOpenFileNames(self ‘学点编程吧:打开多个文件‘ ‘./‘)#注意这里返回值是元组
        if fnames[0]:
            for fname in fnames[0]:
                with open(fname ‘r‘ encoding=‘gb18030‘ errors=‘ignore‘) as f:
                    self.tx.append(f.read())#读取的文件附加到之前的文件之后
                
    def choicefont(self):
        ‘‘‘
        字体选择对话框
        ‘‘‘
        font ok = QFontDialog.getFont()
        if ok:
            self.tx.setCurrentFont(font)
        
    def choicecolor(self):
        ‘‘‘
        颜色选择对话框
        ‘‘‘
        col = QColorDialog.getColor()

        if col.isValid():
            self.tx.setTextColor(col)
            
    def savefile(self):
        ‘‘‘
        文件保存对话框
        ‘‘‘
        fileName = QFileDialog.getSaveFileName(self ‘学点编程吧:保存文件‘ ‘./‘ “Text files (*.txt)“)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     101226  2017-09-21 16:33  PyQt5All\PyQt510\document1.pdf

     文件       4388  2019-01-29 22:00  PyQt5All\PyQt510\save print.py

     文件        226  2017-09-21 16:32  PyQt5All\PyQt510\十九大(new).txt

     文件        215  2017-09-13 22:01  PyQt5All\PyQt510\十九大.txt

     文件       2716  2017-09-21 10:22  PyQt5All\PyQt510\美文.txt

     文件      22714  2017-09-28 17:24  PyQt5All\PyQt511\beauty.png

     文件       6159  2019-01-29 22:03  PyQt5All\PyQt511\messagebox.py

     文件       1404  2019-01-25 21:23  PyQt5All\PyQt511\Qt\Dialog_msgbox.py

     文件       1704  2017-09-29 17:38  PyQt5All\PyQt511\Qt\msgbox.e4p

     文件       1325  2017-09-29 16:53  PyQt5All\PyQt511\Qt\ui_messagebox.ui

     文件       1746  2017-09-29 16:53  PyQt5All\PyQt511\Qt\Ui_ui_messagebox.py

     文件        240  2017-09-29 17:38  PyQt5All\PyQt511\Qt\_eric6project\msgbox.e4q

     文件        910  2017-09-29 17:38  PyQt5All\PyQt511\Qt\_eric6project\msgbox.e6t

     文件          0  2017-09-29 16:51  PyQt5All\PyQt511\Qt\__init__.py

     文件       1694  2017-09-29 16:55  PyQt5All\PyQt511\Qt\__pycache__\Ui_ui_messagebox.cpython-35.pyc

     文件       1525  2019-01-19 16:02  PyQt5All\PyQt511\Qt\__pycache__\Ui_ui_messagebox.cpython-36.pyc

     文件       2244  2019-01-25 21:23  PyQt5All\PyQt512\main.py

     文件       3844  2019-01-29 22:05  PyQt5All\PyQt512\PasswdDialog.py

     文件       2910  2017-10-15 23:01  PyQt5All\PyQt512\__pycache__\PasswdDialog.cpython-35.pyc

     文件       3250  2019-01-29 22:06  PyQt5All\PyQt512\__pycache__\PasswdDialog.cpython-36.pyc

     文件       3298  2019-02-13 16:36  PyQt5All\PyQt512\__pycache__\PasswdDialog.cpython-37.pyc

     文件       2470  2019-01-29 22:07  PyQt5All\PyQt513\progressdialog.py

     文件       3883  2019-01-29 22:09  PyQt5All\PyQt514\checkbox.py

     文件       3290  2019-01-30 21:39  PyQt5All\PyQt515\RadioButton.py

     文件      10725  2017-11-08 10:09  PyQt5All\PyQt516\01.jpg

     文件      10961  2017-11-08 10:11  PyQt5All\PyQt516\02.jpg

     文件      11088  2017-11-08 10:11  PyQt5All\PyQt516\03.jpg

     文件      11386  2017-11-08 10:10  PyQt5All\PyQt516\04.jpg

     文件       2571  2019-01-29 22:12  PyQt5All\PyQt516\qslider.py

     文件       4229  2019-02-13 17:08  PyQt5All\PyQt517\QProgressBar.py

............此处省略1122个文件信息

评论

共有 条评论