Preface:

Use PyQt5 to implement a simple GUI. Without further ado, let’s begin happily

The development tools

**Python version: **3.6.4

Related modules:

Requests module;

PyQt5 module;

Pillow module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Introduction of the principle

The principle is simple. First, find a website that will generate your art signature based on the name you enter:

http://www.jiqie.com/a/14.htm
Copy the code

The website above looks something like this:

Simple packet capture (F12 open developer tools or right mouse button review elements, and then click “Design for me” to see which requests need to be made to generate art signature, simple analysis is ok) you can find the request below the link can return the generated art signature image link address:

The following parameters must be carried during the request:

It can be easily analyzed that:

1 id: Your name2Zhenbi: a fixed value3Id1 and ID2: parameters related to the art signature type4Id3 and ID4: art signature font color parametersCopy the code

So we can easily implement the generated art signature based on the entered name:

Generate a signature
def generate(self) :
    font2ids_dict = {
                        'A stroke of art': ['901'.'15'].'Serial Business signature': ['904'.'15'].'A business deal': ['905'.'14'].'Human handwriting': ['343'.'14'].'Cartoon Round character': ['397'.'14'].'The Grumpy Word': ['380'.'14']
                }
    color2ids_dict = {
                        'Black': ['# 000000'.'#FFFFFF'].'Blue': ['#0000FF'.'#FFFFFF'].'Red': ['#FF0000'.'#FFFFFF'].'Green': ['#00FF00'.'#FFFFFF'].'Yellow': ['#FFFF00'.'#FFFFFF'].'Pink': ['#FFC0CB'.'#FFFFFF'].'DeepSkyBlue': ['#00BFFF'.'#FFFFFF'].'Cyan': ['#00FFFF'.'#FFFFFF'].'Orange': ['#FFA500'.'#FFFFFF'].'Seashell': ['#FFF5EE'.'#FFFFFF']
                    }
    url = 'http://www.jiqie.com/a/re14.php'
    headers = {
                'Referer': 'http://www.jiqie.com/a/14.htm'.'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'.'Host': 'www.jiqie.com'.'Origin': 'http://www.jiqie.com'
            }
    ids_0 = font2ids_dict[self.font_combobox.currentText()]
    ids_1 = color2ids_dict[self.color_combobox.currentText()]
    data = {
                'id': self.name_edit.text(),
                'zhenbi': '20191123'.'id1': ids_0[0].'id2': ids_0[1].'id3': ids_1[0].'id5': ids_1[1]
            }
    res = requests.post(url, headers=headers, data=data)
    image_url = re.findall(r'src="(.*?) "', res.text)[0]
    self.show_image_ext = image_url.split('. ')[-1].split('? ') [0]
    res = requests.get(image_url)
    fp = open('tmp.%s' % self.show_image_ext, 'wb')
    fp.write(res.content)
    fp.close()
    self.show_image = Image.open('tmp.%s' % self.show_image_ext).convert('RGB')
    self.updateimage()
    os.remove('tmp.%s' % self.show_image_ext)
Copy the code

Then use PyQt5 to create a simple GUI interface that looks something like this:

QLabel() can be used to display images in GUI. A simple example is as follows:

1 show_label = QLabel()
2 show_image = Image.open('resource/image/ori.jpg').convert('RGB')
3 fp = io.BytesIO()
4 show_image.save(fp, 'JPEG')
5 qtimage = QtGui.QImage()
7 qtimage.loadFromData(fp.getvalue(), 'JPEG')
8 qtimage_pixmap = QtGui.QPixmap.fromImage(qtimage)
9 show_label.setPixmap(qtimage_pixmap)
Copy the code

Basically all pyQt5 operations.

This is the end of the article, thank you for watching, follow me every day to share a series of Python widgets, next article to share PyQt5+qrcode to create a qrcode generation tool

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ Complete source code + dry plus Python novice learning exchange community: 594356095