Installing the development environment
-
Pycharm installation
Download the default installation online
-
Python3 installation
Download the default installation online
-
PyQt5 installation
Install PyQt5 in Settings –> Project –>Interpreter. Note that version 5.8.2 needs to be installed and packaged as an exe for later PyInstall.
Develop plug-in configuration
-
pyqt5
Program:D:\workspace\python\crossroad\venv\Lib\site-packages\pyqt5_tools\designer.exe
Working directory:$FileDir$
-
pyuic
Program:C:\Python\Python37\python.exe
Arguments:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory:$FileDir$ -
-
Using pyqt5
-
Using pyuic
Developing gadgets
File comparison and replication:
-
Requirement: Given two XML files (A and B), nodes in file A need to be copied or replaced into file B. Replacement rule: If the id, name, and type of node attributes are the same, replace the rule, and add the rule if the node attributes are different.
-
Implementation scheme: use LXML plug-in to read a, B files, respectively, the two files displayed in the interface QTreeView, the left is the file a to copy, implementation can be selected to copy or new records. On the right is the result file B, which can be deleted.
LXML knowledge
-
Read the file
parserRight = etree.XMLParser(remove_blank_text=True) xmlRight = etree.parse(self.rightPath, parserRight) self.rightRoot = xmlRight.getroot() Get the root nodeCopy the code
-
Query node
roadEleList = root.xpath("//OpenDRIVE/road")Copy the code
-
Query nodes by properties
roadEleList = root.xpath("//OpenDriveData/road[@id='" + filterStr + "']") Query by multiple attributes objectList = self.rightRoot.xpath( "//OpenDriveData/road[@id='" + roadId + "']/objects/object[@id='" + id + "'and @type='" + type + "' and @name='" + name + "']")Copy the code
-
Remove nodes
objectList = self.rightRoot.xpath( "//OpenDriveData/road[@id='" + roadId + "']/objects/object[@id='" + id + "'and @type='" + type + "' and @name='" + name + "']") rightObjectsList = self.rightRoot.xpath("//OpenDriveData/road[@id='" + roadId + "']/objects # delete objectList[0]. Remove (objectList[0])Copy the code
-
The new node
# Add an existing node rightObjectsList[0].append(leftObjectList[0]) junction = etree.SubElement(self.root, 'junction', attrib={"id": junctionID}) links = etree.SubElement(junction, "links") link = etree.SubElement(links, "link") Etree.SubElement(links, "link", attrib={}) link.set("type".type) link.set("fromroad", fromroad) link.set("toroad", toroad) link.set("id", id) Copy the code
-
Save the file
The node is converted to a tree object tree = etree.ElementTree(self.rightRoot) ' ''The first parameter is the full path to the XML (including the file name). 2) whether the pretty_print parameter beautificates the code; 3) Whether the xml_declaration parameter is written to the XML declaration, which is the first line of the XML document; 4) The encoding argument is obviously the saved encoding. '' ' tree.write("b.xml", pretty_print=True, xml_declaration=True, encoding='utf-8') QMessageBox.information(self, "Warm reminder"."XML generation successful!", QMessageBox.Yes, QMessageBox.Yes) except Exception as err: print('Error: {0}'.format(err)) QMessageBox.information(self, "Warm reminder"."XML generation failed!", QMessageBox.Yes, QMessageBox.Yes)Copy the code
packaging
-
Install pyinstall
-
Pyinstall command:
-
-f, — onefile packages a single file. Use this if you write all your code in a.py file, but don’t use multiple.py files
-
-d, — onedir packages multiple files and generates many dependency files in DIST, which is suitable for writing tool code in framework form. I personally prefer this, the code is easy to maintain
-
-k, – TK contains TCL/TK at deployment time
-
-a, — ASCII does not contain encoding. All encodings are included by default on Unicode supported Versions of Python.
-
-d, – debug Generates the debug executable file
-
-w, — WINDOwed, — NoConsole executes using the Windows subsystem. Does not open the command line when the program starts (Windows only)
-
– c – nowindowed, – the console
-
Example: PyInstaller -f -w CrossRoadTool5.py
My path in CMD goes into C:\Python\Python37\Scripts,
Exe -f -w D:\workspace\python\ CrossRoad \FileDiffs.py
-
Matters needing attention
-
PyQt5 5.8.2 (tested) or later (not tested) must be installed. The latest pyInstall package error occurs
-
Pycharm pycharm pycharm pycharm pycharm pycharm pycharm pycharm
-