程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The process of developing Python gadgets

編輯:Python

Preface

Because pressure measurement is a system , A lot of id. The tools found on the Internet can generate at most 500 individual , It's a drop in the bucket . I'll find it python Generated code , Modify it to generate once 36 m , This is enough . For the convenience of future use , Just learn to develop one GUI Interface , Pack it into exe Gadget . Here is a record of the whole learning process .
Choose to use wxPython Supporting development tools wxFormBuilder, Its graphical and visual interface can help me set up GUI Interface .

One 、wxPython Environmental preparation

1、wxPython From the official website http://wxpython.org download , Or use pip download wxPython Dependency Library .

pip installer wxPython

2、 Install the graphic visualization generation tool
wxFormBuilder It's an open source cross platform WYSIWYG GUI Builder , Can be wxWidget GUI The design transforms into C ++,Python,PHP or XML Format .
github Project address https://github.com/wxFormBuilder/wxFormBuilder,
Download the latest version here https://github.com/wxFormBuilder/wxFormBuilder/releases, Install locally . Default installation .

Two 、wxFormBuild Create tool graphical visualization interface

1、 First select the project save code as python, Code file name window.

2、 choice Frame Control 、Layout Way, etc , Here is a reference to learn :https://blog.csdn.net/abc6368765/article/details/121796397
https://zhuanlan.zhihu.com/p/107451739?utm_source=wechat_session
Create the following interface , You can enter the year , Select region , Then generate , You can select a folder below to save the generated txt file .
Click on ‘ To start generating ’ After button , The program starts running , establish txt file , The progress bar is updated in real time .
3、 Create an error reporting and success prompt pop-up window Dialog, When selecting the wrong address and the generation is complete , Pop up tips .

Add event binding , Button and progress bar method
4、F8 Save code , Or copy python The code of the page uses .

3、 ... and 、 Interface method debugging

1、 Put the window.py File under Project , Import in the main file GUI Interface
import window. Because it is automatically generated above window.py The content will change as the interface changes , So don't modify this file directly .

2、 Initialize window , How to improve the progress bar and generate buttons .
Will generate id Code , Put it under the button function .( It won't stick if it's too long )

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import wx #wxPython
import window
class ff1(window.MyDialog1):
''' Failure prompt popup '''
def __init__(self, parent):
window.MyDialog1.__init__(self, parent)
class ff2(window.MyDialog11):
''' Success prompt pop-up '''
def __init__(self, parent):
window.MyDialog11.__init__(self, parent)
class MainWindow(window.sfz):
''' Main window program '''
def __init__(self, parent):
window.sfz.__init__(self,parent)
self.jindutiao=0 # Progress bar value 
def m_gauge1OnChar(self, event):
''' Progress bar '''
self.m_gauge1.SetValue(self.jindutiao) # Update progress bar 
if self.jindutiao == 100 :
self.m_button1.Enable() # Enable button 
dialog=ff2(main_win) # Successful pop-up 
dialog.ShowModal()
def gennerator(self, event):
''' Generate button : Get year 、 region 、 Save address, etc '''
self.jindutiao=0 # Press the button , Set as 0. No error is reported, and then the gray button is pressed 
year= str(self.year1.GetValue()) # year GetValue() Returns all documents in the current text box 
where=self.where1.GetStringSelection() # region GetStringSelection() Returns the selected text in the current text box 
for key, value in bianma.AREA_INFO.items():
if value == where:
AREA2 = key # Area code 
path=self.path.GetPath() # Save path judgment 
if path == " Don't choose an address , Files are saved on the desktop by default ":
path = os.path.join(os.path.expanduser("~"), 'Desktop') # Get desktop path 
elif os.path.exists(path) == True: # Determine if the path exists 
path = self.path.GetPath()
else:
dialog=ff1(main_win) # Error box 
dialog.ShowModal()
self.jindutiao=-1
if self.jindutiao>=0:
event.GetEventObject().Disable() # Disable button 

3、 start-up GUI Interface

if __name__ == '__main__':
app = wx.App() # every last wxPython Applications are all one wx.App example 
main_win = MainWindow(None) #None Express this frame Is the top-level framework , No parent frame 
main_win.Show(True) #show() Method to activate the form ,True Show this Frame( window );False, Hidden window ;
app.MainLoop() # Run the program 

4、 Run the program , Click generate button , The program has been successfully established on the desktop txt file , I thought it could be finished , But the tool interface prompt did not respond immediately , Program feign death ...

Four 、 Thread resolution wxpython The interface is not responding to the fake death problem

1、 Something is wrong. , Baidu said that the calculation time is too long ,ui The interface doesn't respond , The program is not responding . This of course needs to be solved , The idea is to call the time-consuming code of thread calculation ,pubsub Communicating between threads and interfaces .
Reference resources :
https://www.bajins.com/Python/PythonGUI.html# Multi thread anti fake dynamic refresh interface
https://blog.csdn.net/rockcandy123/article/details/121310660

2、 The main program adds pub.subscribe(self.m_gauge1OnChar, “update”), subscribe

class MainWindow(window.sfz):
''' Main window program '''
def __init__(self, parent):
window.sfz.__init__(self,parent)
pub.subscribe(self.m_gauge1OnChar, "update")# Release a call update The theme of , Trigger m_gauge1OnChar function 
def m_gauge1OnChar(self, event):
''' Progress bar '''
self.jindutiao = event
self.m_gauge1.SetValue(self.jindutiao) # Update progress bar 

3、 The generator is put in the thread , use pub towards GUI Send a message .
After debugging , The problem of pretending to die is solved .

class TestThread(threading.Thread):
''' Multi thread anti feign death and message passing between threads '''
def __init__(self,year,where,path,AREA2):
self.year=year
self.where=where
self.path=path
self.AREA2=AREA2
threading.Thread.__init__(self) # Start immediately when the thread is instantiated 
self.start()
self.jindutiao=0
def run(self):
""" The thread executes the composite ID code """
''' The calculation time-consuming code is slightly '''
pub.sendMessage("update", event=self.jindutiao) # Hair update news , Appoint event The value of the parameter 

5、 ... and 、pyinstaller pack

1、 First installation pyinstaller package

pip install pyinstaller

In the code folder , open cmd, Use the command to package into a exe.

pyinstaller -F -w -i logo.ico sfz.py

Generate exe The file in dist Inside . But there is another problem , The generated file is too large , That can't endure . Baidu go .

2、 The main reason is that pyinstaller Will be able to python Packages that are not used by many projects in the environment are packaged together , This increases exe Size .
The solution is to use a virtual environment , Such as :pipenv library 、anaconda Create a virtual environment
Reference resources :
https://blog.csdn.net/yueyi0221/article/details/121717471
https://zhuanlan.zhihu.com/p/348120084
3、 Both environments have tried , Both can be used. , The packaged program is about the same size . Last tool 9M A little more .

summary

The road to making gadgets , Step by step , But also learned wxPython、pub And so on , I believe that similar development will be more handy in the future .
Life is too short , I learned python.
Tools and code are on the disk , link :https://pan.baidu.com/s/18sJiBU6cmVOeitVNgjyT1Q
Extraction code :b2ie


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved