Python   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了过年了,用 PyQt5 生成一副春联吧...大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

由于篇幅有限,完整源代码的获取方式放在文末了。

【阅读全文】

需求说明:
通过在界面上输入春联的上、下批和横批汉字从而生成春联图像,最后将春联图片保存。有实际需要的还可以将春联打印。

过年了,用 PyQt5 生成一副春联吧...

过年了,用 PyQt5 生成一副春联吧...

实现过程:
实现思路是先下载好春联的背景图片,再下载每个汉字的文字图片将文字图片粘贴到春联背景上。所以这里有用了一个春联图片的三方获取地址。

http://xufive.sdysit.com/tk

春联生成部分参了 CSDN 博客平台。

网络数据获取相关模块

import io  # python IO 处理模块
from PIL import Image  # 图像处理模块
import requests  # 网络请求模块

UI 相关模块

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

主题样式模块引用

from QCAndyUi import CAndyWindow

应用操作相关模块

import sys
import os

UI界面主要代码展示

 def init_ui(self):
        self.setWindowtitle('春联生成器')
        self.setWindowIcon(QIcon('春联.ico'))

        vbox_main = QVBoxLayout()

        self.image_label = QLabel()
        self.image_label.setScaledContents(true)
        self.image_label.setMaximumSize(650,150)
        self.image_label.setPixmap(QPixmap('横批演示.jpg'))

        hbox = QHBoxLayout()
        self.brower = QTextBrowser()
        self.brower.setFont(QFont('宋体', 8))
        self.brower.setReadOnly(true)
        self.brower.setPlaceholderText('信息展示区域')
        self.brower.ensurecursorVisible()

        form = QFormLayout()

        self.up_label = QLabel()
        self.up_label.setText('设置上联')

        self.up_text = QLineEdit()
        self.up_text.setPlaceholderText('请输入上联')

        self.down_label = QLabel()
        self.down_label.setText('设置下联')

        self.down_text = QLineEdit()
        self.down_text.setPlaceholderText('请输入下联')

        self.h_label = QLabel()
        self.h_label.setText('设置横批')

        self.h_text = QLineEdit()
        self.h_text.setPlaceholderText('请输入横批')

        self.thread_ = WorkThread(self)
        self.thread_.trigger.connect(self.update_log)
        self.thread_.finished.connect(self.finished)

        self.save_path = QLineEdit()
        self.save_path.setReadOnly(true)

        self.save_btn = QPushButton()
        self.save_btn.setText('存储路径')
        self.save_btn.clicked.connect(self.save_btn_click)

        form.addRow(self.up_label, self.up_text)
        form.addRow(self.down_label, self.down_text)
        form.addRow(self.h_label, self.h_text)
        form.addRow(self.save_path, self.save_btn)

        vbox = QVBoxLayout()

        self.starT_Btn = QPushButton()
        self.starT_Btn.setText('开始生成春联')
        self.starT_Btn.clicked.connect(self.starT_Btn_click)

        vbox.addLayout(form)
        vbox.addWidget(self.starT_Btn)

        hbox.addWidget(self.brower)
        hbox.addLayout(vboX)

        vbox_main.addWidget(self.image_label)
        vbox_main.addLayout(hboX)

        self.setLayout(vbox_main)

槽函数的应用

 def update_log(self, text):
        '''
        槽函数:向文本浏览器中写入内容
        :param text:
        :return:
        '''
        cursor = self.brower.textcursor()
        cursor.movePosition(QTextcursor.End)
        self.brower.append(text)
        self.brower.setTextcursor(cursor)
        self.brower.ensurecursorVisible()

    def save_btn_click(self):
        Dicr = QFileDialog.getExisTingDirectory(self, '选择文件夹', os.getcwd())
        self.save_path.setText(Dicr)

    def starT_Btn_click(self):
        self.starT_Btn.setEnabled(false)
        self.thread_.start()

    def finished(self, finished):
        if finished is True:
            self.starT_Btn.setEnabled(true)
            h_image = self.save_path.text().Strip() + '/横批.jpg'
            if os.path.isfile(h_imagE):
                self.image_label.setPixmap(QPixmap(h_imagE))
            self.update_log('由于上下联不好预览,请使用图片查看器预览,目前仅支持横批图片预览...')

春联文字获取主题代码

  def run(self):
        up_text = self.parent.up_text.text().Strip()
        down_text = self.parent.down_text.text().Strip()
        h_text = self.parent.h_text.text().Strip()
        save_path = self.parent.save_path.text().Strip()
        if up_text == '' or down_text == '' or h_text == '' or save_path == '':
            self.trigger.emit('参数设置不允许为空,请设置好后重新开始!')
            self.finished.emit(true)
        else:
            text = up_text + ' ' + down_text
            self.generate_image(text, layout='V', pre=0.75, out_file=save_path + '/上下联.jpg')
            self.generate_image(h_text, layout='H', pre=0.75, out_file=save_path + '/横批.jpg')
            self.finished.emit(true)

文字图片获取部分

def get_word_image(self, ch='bg', pre=1.0):
        '''
        单文字图片下载函数
        :param ch: 默认网络请求参数'bg'
        :param pre: 单个文字对象
        :return: 图像对象
        '''
        res = io.bytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch': ch}).content)
        image = Image.open(res)
        w, h = image.size
        w, h = int(w * float(prE)), int(h * float(prE))
        return image.resize((w, h))  # 单个文字的形状是正方形,所以这里的长、宽都是一致的

完整源码获取方式:公众号内回复"春联生成器"。

过年了,用 PyQt5 生成一副春联吧...

我是 [Python 集中营]、很高兴您看到了最后, 我是一个专注于 Python 知识分享的公众号,希望可以得到您的关注~

过年了,用 PyQt5 生成一副春联吧...

【往期推荐】

记录一下python中的十大%占位符对应的格式化...

PyQt5 UI 制作一个豆瓣电影信息查看器,初识QThread多线程...

PyQt5 最小化到托盘,升级小闹钟...

pyinstaller打包exe文件太大,利用pipenv轻松解决!

PyQt5 小工具:Excel数据分组汇总器...

大佬总结

以上是大佬教程为你收集整理的过年了,用 PyQt5 生成一副春联吧...全部内容,希望文章能够帮你解决过年了,用 PyQt5 生成一副春联吧...所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: