current position:Home>Python develops a color dynamic two-dimensional code generator in one hour, and uses the virtual environment to package and release the EXE program.

Python develops a color dynamic two-dimensional code generator in one hour, and uses the virtual environment to package and release the EXE program.

2022-02-02 00:53:05 Lu Jacob

Preface

Beginners Python I learned about this project when I was , It's amazing ! By calling MyQR The library realizes the synthesis of color two-dimensional codes . I won't introduce this library here , Those interested can search by themselves , There are many related detailed tutorials .

At first, I wanted to share it with my good friends , But I can't write a graphical interface , It has always been in my blog draft box , Today, I can finally write it and share it with you , Super happy !

The following code explanation is piecemeal , Finally, there is complete code . Including the virtual environment packaging and release at the end of the article EXE Program tutorial editing in graphite document , It's been said in great detail , I don't know. You can talk about me in private .

design sketch

 Insert picture description here

Core function design

Support .png、.jpg、.gif、.bmp Format conversion , Choose your own materials and pictures .

UI Layout

The program requires few variables , common Three inputs :“ QR code analysis link ”、“ Material name ”、“ Finished product name ”, Because the input box for resolving links is long , So center layout , The rest “ Material name ”、“ Finished product name ” The input field of is short , So the left-right symmetrical layout , In this way, the whole presents “ symmetry 、 Standard Qi ” The effect of .  Insert picture description here

The material is divided into .png、.jpg、.gif、.bmp Format , use Checkbutton Check the part , Check according to your own material type ! The image generated in this way is also consistent with the material format type .

On this basis, the design is relatively simple , In addition to the beautification of color , I didn't insert some pictures to decorate , You can carry out secondary development on this basis .

On the design idea of graphical interface

The graphical interface I use is Python Self contained tkinter library , I don't think it's difficult to learn the graphical interface , When you are proficient , You'll find out , He is just one “ window + parts + function ” A process of , The window provides a good basis for our further design “ Containers ”, Components lay the foundation for the functions we realize “ Basics ”, The function is realized by the trigger of the button “ function ”.

Implementation steps ( One ): Start by building an interface

The interface is like a drawing board , With a drawing board, we can draw on it . besides , We also need to prepare some paint , Only in this way can the painting be colorful , And the interface also has a lot of decoration , For example, the title of the interface 、 Icon 、 These sizes .

from tkinter import *
import tkinter as tk

TOP6 = Tk()# Initialize a window 
TOP6.geometry("575x375")# Set the size for this window 
TOP6.iconbitmap(' Icon .ico')# Specify the window icon 
TOP6.title(' Color dynamic QR code generator V1.0')# Give the window a name 
TOP6.mainloop()# Window loop 
 Copy code 

 Insert picture description here

How to get .ico Format icons ?

stay use Python Write an office gadget for the girl you like , She said it was great ! In this post , We have explained .

First, choose your favorite picture , And then in Icon online conversion website It can be converted .

As long as the pictures are not too much, they are free .  Insert picture description here

Implementation steps ( Two ): Add features to the interface

according to UI Design we need four components : label ( Describe the data type )、 Input box ( Receive data )、Checkbutton Check the part ( Select the material format type )、 Button ( Trigger function ).

# QR code analysis link 
label = StringVar()
input_name = Label(TOP6, text=' QR code analysis link ', font=(' Regular script ', 20)).place(x=175, y=20)
entry20 = Entry(TOP6, bg='#ffffff', width=30, textvariable=label, font=(' Regular script ', 20)).place(x=75, y=80)

# Material type options 
var1 = StringVar()
var1.set("F")
c = tk.Checkbutton(TOP6, text=".jpg", variable=var1, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=100, y=125)

var2 = StringVar()
var2.set("F")
c = tk.Checkbutton(TOP6, text=".png", variable=var2, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=200, y=125)

var3 = StringVar()
var3.set("F")
c = tk.Checkbutton(TOP6, text=".gif", variable=var3, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=300, y=125)

var4 = StringVar()
var4.set("F")
c = tk.Checkbutton(TOP6, text=".bmp", variable=var4, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=400, y=125)

# Material name 
label2 = StringVar()
input_name = Label(TOP6, text=' Material name ', font=(' Regular script ', 20)).place(x=35, y=180)
entry20 = Entry(TOP6, bg='#ffffff', width=8, textvariable=label2, font=(' Regular script ', 20)).place(x=25, y=230)

# Finished product name 
label3 = StringVar()
input_name = Label(TOP6, text=' Finished product name ', font=(' Regular script ', 20)).place(x=450, y=180)
entry20 = Entry(TOP6, bg='#ffffff', width=8, textvariable=label3, font=(' Regular script ', 20)).place(x=440, y=230)


# Button 
BUTTON = Button(TOP6, text="(๑¯ิε ¯ิ๑)",width = 15, height = 2,
                    font=(" Regular script ", 15))

BUTTON.place(x=205, y=320)

 Copy code 

 Insert picture description here

Implementation steps ( 3、 ... and ): Build a function to receive data

Just having an input box is not enough , The meaning of the input box is to receive the required data , So we need to build a function to receive data .

# Receive the value of wechat QR code link 
def get_QR():
    QR = label.get()
    return QR
# Receive material name 
def get_name():
    name = label2.get()
    return name
# Receiving finished product name 
def get_end_name():
    end_name = label3.get()
    return end_name
#.jpg
def get_v1():
    v1 = var1.get()
    return v1
#.png
def get_v2():
    v2 = var2.get()
    return v2
#.gif
def get_v3():
    v3 = var3.get()
    return v3
#.bmp
def get_v4():
    v4 = var4.get()
    return v4
 Copy code 

Implementation steps ( Four ): Build function function

After receiving the data, we build the function , And pass command Command binding button .

The following code section , We put the received data into the function and call .

Because the material has different formats , The generated finished product should be consistent with the format of the material , So we can divide it into four cases by checking different items , Using simple conditional judgment statements .

There is still a flaw in the design of this function , No successful prompt was generated ! You can do it yourself and add !!!!

BUTTON = Button(TOP6, text="(๑¯ิε ¯ิ๑)",width = 15, height = 2,command = QR_Code,
                    font=(" Regular script ", 15))

BUTTON.place(x=205, y=320)
 Copy code 
# Image generation function part 

def newmethod718():
    return myqr

def QR_Code():
    

    QR = get_QR()
    name = get_name()
    end_name = get_end_name()
    v1 = get_v1()
    v2 = get_v2()
    v3 = get_v3()
    v4 = get_v4()


    if v1 == 'T' and v2 == 'F' and v3 == 'F' and v4 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.jpg',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.jpg', 
            )
        


    elif v2 == 'T' and v1 == 'F' and v3 == 'F' and v4 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.png',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.png', 
            )
        

    elif v3 == 'T' and v1 == 'F' and v2 == 'F' and v4 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.gif',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.gif', 
            )
        

    elif v4 == 'T' and v1 == 'F' and v3 == 'F' and v2 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.bmp',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.bmp', 
            )
 Copy code 

Interface beautification

stay Python The graphical interface is not beautiful enough ? Three lines of code completely beautify your Tkinter Interface in , We've shown you how to beautify your... With three lines of code and one click tkinter Interface .

First quote ttkbootstrap library

from ttkbootstrap import Style
 Copy code 

And then TOP6=tk() Change to the following code .

style = Style()
style = Style(theme='minty')
TOP6 = style.master
 Copy code 

 Insert picture description here

Complete code

from tkinter import *
import tkinter as tk
from ttkbootstrap import Style
from MyQR import myqr

style = Style()
style = Style(theme='minty')
TOP6 = style.master
#TOP6 = Tk()# Initialize a window 
TOP6.geometry("575x375")
TOP6.iconbitmap(' Icon .ico')# Assign icons 
TOP6.title(' Color dynamic QR code generator V1.0')# Assign icons 

# Receive the value of wechat QR code link 
def get_QR():
    QR = label.get()
    return QR
# Receive material name 
def get_name():
    name = label2.get()
    return name
# Receiving finished product name 
def get_end_name():
    end_name = label3.get()
    return end_name
#.jpg
def get_v1():
    v1 = var1.get()
    return v1
#.png
def get_v2():
    v2 = var2.get()
    return v2
#.gif
def get_v3():
    v3 = var3.get()
    return v3
#.bmp
def get_v4():
    v4 = var4.get()
    return v4

# Image generation function part 

def newmethod718():
    return myqr

def QR_Code():
    

    QR = get_QR()
    name = get_name()
    end_name = get_end_name()
    v1 = get_v1()
    v2 = get_v2()
    v3 = get_v3()
    v4 = get_v4()


    if v1 == 'T' and v2 == 'F' and v3 == 'F' and v4 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.jpg',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.jpg', 
            )
        


    if v2 == 'T' and v1 == 'F' and v3 == 'F' and v4 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.png',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.png', 
            )
        

    if v3 == 'T' and v1 == 'F' and v2 == 'F' and v4 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.gif',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.gif', 
            )
        

    if v4 == 'T' and v1 == 'F' and v3 == 'F' and v2 == 'F':
        
        newmethod718().run(
            words=QR,
            #  After scanning QR code , Display content , Or jump Links 
            version=5,  #  Set fault tolerance 
            level='H',  #  Control error correction level , The scope is L、M、Q、H, From left to right 
            picture=name+'.bmp',  #  The directory of the pictures , It could be moving pictures 
            colorized=True,  #  Black and white (False) Or color (True)
            contrast=1.0,  #  To adjust the contrast of the picture ,1.0  Represents the original picture . The default is 1.0.
            brightness=1.0,  #  Used to adjust the brightness of the picture , Same as above .
            save_name=end_name+'.bmp', 
            )
        




# QR code analysis link 
label = StringVar()
input_name = Label(TOP6, text=' QR code analysis link ', font=(' Regular script ', 20)).place(x=175, y=20)
entry20 = Entry(TOP6, bg='#ffffff', width=30, textvariable=label, font=(' Regular script ', 20)).place(x=75, y=80)

# Material type options 
var1 = StringVar()
var1.set("F")
c = tk.Checkbutton(TOP6, text=".jpg", variable=var1, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=100, y=125)

var2 = StringVar()
var2.set("F")
c = tk.Checkbutton(TOP6, text=".png", variable=var2, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=200, y=125)

var3 = StringVar()
var3.set("F")
c = tk.Checkbutton(TOP6, text=".gif", variable=var3, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=300, y=125)

var4 = StringVar()
var4.set("F")
c = tk.Checkbutton(TOP6, text=".bmp", variable=var4, onvalue="T", offvalue="F")
c.grid(row=30, column=0, sticky=W)
c.place(x=400, y=125)

# Material name 
label2 = StringVar()
input_name = Label(TOP6, text=' Material name ', font=(' Regular script ', 20)).place(x=35, y=180)
entry20 = Entry(TOP6, bg='#ffffff', width=8, textvariable=label2, font=(' Regular script ', 20)).place(x=25, y=230)

# Finished product name 
label3 = StringVar()
input_name = Label(TOP6, text=' Finished product name ', font=(' Regular script ', 20)).place(x=450, y=180)
entry20 = Entry(TOP6, bg='#ffffff', width=8, textvariable=label3, font=(' Regular script ', 20)).place(x=440, y=230)



BUTTON = Button(TOP6, text="(๑¯ิε ¯ิ๑)",width = 15, height = 2,command = QR_Code,
                    font=(" Regular script ", 15))

BUTTON.place(x=205, y=320)

TOP6.mainloop()
 Copy code 

Packaging with virtual environments , Publish your EXE Program

EXE Release  Insert picture description here

Epilogue

After learning, children can share it with their girlfriends ......, You can also entertain yourself . This project is my first learning Python I learned when I was , It's been a long time , It was quite hot at that time . If you like a friend, please give me “ One key, three links ”+“ Focus on ”.

Progress together !

copyright notice
author[Lu Jacob],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020053042325.html

Random recommended