Life is short, learn Python!

Last week posted an article “8000 words, Tkinter detailed use tutorial!” , many partners hope to be able to point more tutorial, today came.

Tkinter is the standard GUI library in Python that allows you to quickly create GUI applications. Today we are going to use another small case to deepen your understanding of Tkinter.

Introduction of Tkinter

Since Tkinter is a standard library that comes with Python, we can simply import it when we want to use it.

from tkinter import *
Copy the code

Tkinter supports the following components:

Tkinter works well with simple graphical interfaces, but the interface isn’t as aesthetically pleasing as PyQt5’s and the code is clunky compared to Pysimplegui’s. These two repositories bury a pit, open a separate introduction behind.

Today’s general idea is: first choose an online signature site, find the interface simulation request, and then download the generated signature picture to the local, and finally display it in the GUI window generated by Tkinter.

Get the personality signature graph

Before creating signature software, the first thing to do is to find a suitable website and get their interface.

Why do you need to find the right site?

A search engine found “signature design” and it was really fancy. My aesthetic was shocked. “Messy flowers” to find a circle, just picked a slightly more normal website.

Website: www.yishuzi.com/b/13.htm

F12 Developer mode, select Network. Manually change the color to #000000 black and the background to #FFFFFF white, enter a name and click Generate.

Very easily, you can find the request that the site generates the signature to send. As follows:

Without further ado, let’s get straight to Python!

Request the url with requests.post() and import the parameters. The most important parameters are id for the generated name and ID1 for the art font.

import requests
from urllib.request import urlretrieve
url = 'http://www.yishuzi.com/b/re13.php'
d = requests.post(url, data={'id': 'Test signature'.'zhenbi': '20191123'.'id1': '904'.'id2': '#FFFFFF'.'id4': '# 000000'.'id6': '# 000000'})
d.content.decode("utf-8")
Copy the code

As you can see, post() returns a string with a link.

You can easily extract the generated growing signature image from the returned string. You then download the image locally with the URlRetrieve (), and finally encapsulate it as a custom function.

def create_sign(word) :
    url = 'http://www.yishuzi.com/b/re13.php'
    d = requests.post(url, data={'id': word, 'zhenbi': '20191123'.'id1': '904'.'id2': '#FFFFFF'.'id4': '# 000000'.'id6': '# 000000'})
    myurl = d.content.decode("utf-8").split('"') [1]
    urlretrieve(myurl, word + '.png')
Copy the code

Later, when designing the GUI interface of the software, call this function directly, and you can directly generate the personality signature.

Design software GUI interface

Now that the function create_sign() has been created, the job is really half done.

The rest is to design the software interface, as well as a simple sketch.

Here we mainly use: Label (Label control), Entry (input control), Button (Button control), Frame (Frame control) and Combobox (drop-down list box) when selecting fonts.

The controls in front are introduced, see this article 8000 words, Tkinter detailed use tutorial! Here, I’ll just focus on the drop-down list box.

Drop – down list box

Python Forms (Tkinter) drop-down list box Combobox. It is a widget of the TTK module under the Tkinter module and produces a drop-down list box that is a widget in the GUI. When the user clicks on the drop-down list to get a value selected from the list, a virtual event named <

> is generated.

Its common parameters are:

grammar role
cv = tk.stringVar() Bind variables
com = ttk.Combobox(root, textvariable=cv) Create a drop-down box
com.pack() Place the drop-down box
Com [“value”] = (‘ text ‘, text ‘) Set the drop-down data
Com. Current (index) Setting defaults
demo = com.get() Variable acceptance value
Com.bind (“<>”, function name) Pull down data click to call function

Let me give you a quick example to help you understand.

import tkinter
from tkinter import ttk

root = tkinter.Tk()
root.geometry("400x200") 

xVariable = tkinter.StringVar()   Create a variable
 
com = ttk.Combobox(root, textvariable=xVariable)  Create a drop-down menu
com.pack()     Bind the dropdown menu to the form
com["value"] = ("Option 1"."Option 2"."Option 3"."Option 4")
com.current(0)   Set the default value of the drop-down menu

root.mainloop()
Copy the code

After running, the effect is shown below.

So we can use com.get() to get the last value selected in the dropdown.

Design interface

The rest of the interface design is much simpler. Just arrange Label, Entry, Button, Frame, and Combobox.

Think back to the sketch you drew before, and turn it on!

Set the window size to 600×400, and then set the basic properties of the window.

Place geometry method is adopted for all components, and the size and layout of components are reasonably planned. The Button component is also linked to its corresponding function create_sign(word).

Part of the code is shown below, the complete code is at the end of the article.

root = Tk()
root.title("Signature Design by: Learn Python fast")
root.geometry("600x400")
bg_image = PhotoImage(file = 'background.png')
bg_label = Label(root, image = bg_image)
bg_label.place(relwidth = 1, relheight = 1)
frame = Frame(root, bg = '#edcc79', bd = 5)
frame.place(relx = 0.5, rely = 0.1, relwidth = 0.75, relheight = 0.1,anchor = 'n')
font_label = Label(frame, text = 'Enter signature :', font = (Microsoft Yahei.16), fg ='black')
font_label.place(relwidth = 0.25, relheight = 1)
name_entry = Entry(frame, font = (Microsoft Yahei.16))
Copy the code

The final product, let me show you.

Enter your name, click Generate, and go! Different styles, one key conversion!

This completes another Tkinter case.

Life is short, learn Python, and I’ll see you tomorrow!

If you are interested in the code of this article, click on the following public account “learn Python” background reply “personality signature”, you can get all the files!

1