current position:Home>Python application tool development: PIP instruction GUI program

Python application tool development: PIP instruction GUI program

2022-05-15 05:56:49SteveDraw

One , Development requirements and development ideas

1. Development requirements

python As a super language , There are many third-party libraries , Suitable for our secondary development into other functions , Then the tools for managing these third-party libraries have to say pip 了 !pip Tools make it easier for us to manage third-party libraries , But how not to integrate IDE Professional software (pycharm That kind of ), Used in the local environment pip The tool can only be opened in cmd Operation command in . First , It needs to be opened every time cmd The window is a more troublesome thing , secondly , For a library ( Inquire about , Download and install , uninstall ) Instruction discontinuity , And repeatedly enter different instructions , This makes our work and life time and energy wasted in these troublesome things ! So can we develop a pip Tools GUI Program , Can alleviate pip The trouble brought by tools ?
The answer is yes , Let's explore !
At the end of the paper, the GitHub Address Links !
 Insert picture description here

2. The development train of thought

pip Disadvantages of tool operation Corresponding solutions
It needs to be opened every time and in cmd Window run Let the program automatically perform the corresponding operation ( In most cases, do not open cmd window )
For the same library ,pip Command input is discontinuous stay GUI Window integration common pip Tool command function , Reduce input overload

3. Preparation

Knowledge reserve remarks
object-oriented programming : Classes and objects Use class , It can make the secondary development and maintenance of the program better , And the program code is modular and the structure is clear , Conducive to reading and development
GUI Development tkinterpython Self contained tkinterGUI To write , Although overall UI The interface is not more beautiful than the mainstream , But development is simple , And the appearance logic can meet our needs
pyinstallerpython Common and popular packaging Libraries , Need to download and install

Two , Write code

The Files are mainly placed in PIPS class ,PIPS Class mainly implements the corresponding pip How to operate !

import os
class PIPS:#PIPS class : It mainly stores specific information pip Class that instructs the actual operation 
    def __init__(self):
        pass# There is no class variable , Set to pass Just go 

    def install(self,packages):# Download the installed cmd Operation function 
        cmd='pip install '+packages
        operation = os.popen(cmd)# Different open cmd window , You can run the command 
        return Return the information content after success 

    def uninstall(self,packages):# Unloaded cmd Operation function 
        cmd='pip uninstall '+packages# Because of the cmd Make a second choice in (y or n), So open cmd window 

    def piplist(self):# List of libraries cmd Operation function 
        cmd='pip list'
        operation = os.popen(cmd)

    def checkpip(self,packages):# Library query cmd Operation function 
        cmd='pip show '+packages
        operation = os.popen(cmd)

    def uppip(self,packages):# Update the library's cmd Operation function 
        cmd='python -m pip install --upgrade '+packages
        operation = os.popen(cmd)

    def opencmd(self):#cmd Open window operation function 
        os.system('cmd/c start')

The file ( Is the main program ) The main place PIPGUI class ,PIPGUI Class is mainly GUI Write classes and import and use the previous PIPS Class method .

from tkinter import *
from PIPS import PIPS
    #GUI Control class definition 
    def __init__(self):
        super().__init__()# Inherited from PIPS class 
        # Main form 
        root.title('pip Download program ')
        root.geometry('1020x550+200+80')# Program window size and position 
        root.iconbitmap('pips.ico')# Window icons 

        # install Button 
        install_button = Button(root, text=' Download and install ', width=18, height=2, command=self.installs), y=100)

        # uninstall Button 
        uninstall__button = Button(root, text=' uninstall ', width=18, height=2, command=self.uninstalls), y=100)

        #  Library List button 
        list_button = Button(root, text=' Third library list ', width=18, height=2, command=self.piplists), y=160)

        #  Query library information button 
        checkp_button = Button(root, text=' Library query ', width=18, height=2, command=self.checkpips), y=160)

        # pip Command Update button 
        up_button = Button(root, text=' Update library package ', width=18, height=2, command=self.uppips), y=220)

        # cmd Turn on the button 
        self.cmd_textout_button = Button(root, text=' open cmd window ', width=18, height=2, command=self.opencmds), y=220)

        #  Input box clear button 
        self.in_textout_button = Button(root, text=' Clear the input box ', width=18, height=2, command=self.in_textouts), y=280)

        #cmd Run content clear button 
        self.cmd_textout_button=Button(root, text=' Empty cmd Running results ', width=18, height=2, command=self.cmd_textout),y=280)

        #  Run content display 
        self.cmd_text = Text(root, width=85, height=40), y=10)

        #  Input display window 
        self.in_text = Text(root, width=50, height=2), y=10)

        #  Program status label 
        self.tip_label = Label(root, text=' Program operation tips ', width=35, height=2, bg='green', font=(' Song style ', 14)), y=430)

        #  Operation prompt bar 
        self.tipout_label = Label(root, width=50, height=3, bg='green',font=(' Song style ',10)), y=485)

    #  Function part :
    def text_get(self):  #  Get input box content function 
        return self.in_text.get('1.0', 'end')

    def cmd_in(self,result):  # cmd Run the result input function 
        self.cmd_text.insert(1.0, result)

    def cmd_textout(self):  # cmd Content cleanup function 
        self.cmd_text.delete('1.0', 'end')
        self.tipout_label['text'] = ' You emptied cmd Run content box !'

    def installs(self):  #  Download and install button functions 
        self.tipout_label['text'] = ' You used the download and install function ! Be careful not to open the proxy node before use !'
        result = self.install(self.text_get())

    def uninstalls(self):  #  Unload button function 
        self.tipout_label['text'] = ' Do not enter a command with an equal sign ! Please go to the pop-up cmd The window determines whether to uninstall !'

    def piplists(self):  #  Library List button function 
        self.tipout_label['text'] = ' You checked the library list , The query results can slide down to see more content '
        result = self.piplist()

    def checkpips(self):  #  Library query button function 
        self.tipout_label['text'] = ' Be careful not to enter with an equal sign !'
        result = self.checkpip(self.text_get())

    def uppips(self):  #  Library update button function 
        self.tipout_label['text'] = ' You used the library update function !'
        result = self.uppip(self.text_get())

    def in_textouts(self):# Input box clear function 
        self.in_text.delete('1.0', 'end')
        self.tipout_label['text'] = ' You empty the input box window !'
    def opencmds(self):#cmd Open window operation function 
        self.tipout_label['text']=' You opened it. cmd window !'

if __name__ == '__main__':

3、 ... and , Program packaging

1, Packaging process

(1) Find the directory of your code files , Copy down
 Insert picture description here
(2) open cmd window , Copy the copied directory to cmd window , And add cd, As shown in the following example :
 Insert picture description here
(3) Make sure you have downloaded and installed pyinstaller, Enter the following command :

pyinstaller -F < Program master file >.py

 Insert picture description here
(4) In the generated file , Only keep < The main program >.spec file , Delete the rest ( Of course, our original py The code files are also in this directory , Definitely don't delete , Just keep it )! as follows :
 Insert picture description here

(5) Open suffix is .spec file , stay Analysis A list of the first line in , Then add the dependencies of our main program and the modules to be imported The string form of the file , Here's the picture :
 Insert picture description here

(6) Open it again cmd, Enter file directory , Enter the following command :

pyinstaller -F  < Main program name >.spec

Here's the picture :
 Insert picture description here
(7) Prompt after successful packaging , We can delete the generated _pycache_ and build file , Only keep dist Folder , Because there is a generator exe Program , In addition, the program form needs to use pips.ico file , By default, packaging will not move it to dist The folder , We want to copy to dist Under the document !
 Insert picture description here

2. Function operation description and use

Packaged program , After opening, the interface is as follows :
 Insert picture description here
The following is the functional description of each part :

Control Function description
Input box Accept the entered library name , According to the corresponding command function , You can add the specified version number
cmd Run result box hold cmd Enter the operation results into this box to display , The largest on the far right is the box
Download and install button Download and install the Library
Unload button Unload Library , After clicking, you need to go to the attached cmd Filling in the window y( yes ) or n( no ), Don't close the window after filling in
Third party library list button After clicking , You can install it into the general list of the local third Library , And feed back the information to cmd Run result box
Library query button Query the corresponding library information , And feed back the information to cmd Run result box
Update library package button Update the corresponding library , And feed back the information to cmd Run result box
open cmd Window buttons Open up new cmd window
eliminate cmd Run result button Click to put cmd The contents of the run result box are empty
Clear input box button Click to clear the input box
Program operation tips Mainly some tips for the operation of various functions of the program

3. Summary of problems encountered in development and solutions :

pyinstaller pack python Program ( Multiple files )
Python-Tkinter Graphical interface design ( Detailed tutorial )

Four , Finished program download

In order to better learn and share the content of this part , And provide the finished product program for everyone to directly observe , Specially put the resource link here , For all readers to download !
pip Instructions GUI Program download link
If you have conditions , You can come to my Github Look at my project file and code , Attached below GitHub Project address :
Last , If there is any deficiency in the text , it is respectful to have you criticize and correct sth !

copyright notice
author[SteveDraw],Please bring the original link to reprint, thank you.

Random recommended