Python3 + PyQt5 + lxml 教程

安裝開發環境

  • pycharm 安裝python

上網下載默認安裝bash

  • Python3 安裝app

上網下載默認安裝框架

  • PyQt5安裝工具


在settings--> Project -->Interpreter 中安裝PyQt5,注意須要安裝5.8.2 版本,爲了後期pyinstall打包成exe。ui

開發插件配置

  • pyqt5編碼



Program:D:\workspace\python\crossroad\venv\Lib\site-packages\pyqt5_tools\designer.exespa

Working directory:$FileDir$插件

  • pyuic命令行

    Program:C:\Python\Python37\python.exe
    Arguments:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
    Working directory:$FileDir$



  • 使用pyqt5


  • 使用pyuic


開發小工具

文件比對複製:

  • 需求:已知2個xml文件(a和b),須要把a文件中的節點複製或替換到b文件中。替換規則,當節點屬性id、name、type相同的時候替換,不一樣的時候新增。

  • 實現方案:使用lxml插件讀取a、b文件,分別把這兩個文件顯示到界面QTreeView中,左側是須要複製的文件a,實現能夠勾選執行須要複製或新增的記錄。右側爲結果文件b,實現能夠勾選刪除。


lxml 知識點

  • 讀取文件

    parserRight = etree.XMLParser(remove_blank_text=True)
    xmlRight = etree.parse(self.rightPath, parserRight)
    self.rightRoot = xmlRight.getroot()  # 獲取根節點複製代碼

  • 查詢節點

    roadEleList = root.xpath("//OpenDRIVE/road")複製代碼
  • 按照屬性查詢節點

    roadEleList = root.xpath("//OpenDriveData/road[@id='" + filterStr + "']")
    ​
    # 按多個屬性查詢
    objectList = self.rightRoot.xpath(
                        "//OpenDriveData/road[@id='" + roadId + "']/objects/object[@id='" +
                        id + "'and @type='" + type + "' and @name='" + name + "' ]")複製代碼
  • 刪除節點

    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 # 刪除節點 rightObjectsList[0].remove(objectList[0])複製代碼
  • 新增節點

    # 新增已有節點
    rightObjectsList[0].append(leftObjectList[0])
    ​
    junction = etree.SubElement(self.root, 'junction', attrib={"id": junctionID})
    links = etree.SubElement(junction, "links")
    link = etree.SubElement(links, "link")
    # 使用set方法添加屬性,屬性值不排序,而etree.SubElement(links, "link", attrib={})設置,屬性值導出時是有序的
    link.set("type", type)
    link.set("fromroad", fromroad)
    link.set("toroad", toroad)
    link.set("id", id)
    ​
    ​複製代碼
  • 保存文件

    # 節點轉爲tree對象
        tree = etree.ElementTree(self.rightRoot)
        ''' 各個參數含義以下: 1)第1個參數是xml的完整路徑(包括文件名); 2)pretty_print參數是否美化代碼; 3)xml_declaration參數是否寫入xml聲明,就是咱們看到xml文檔第1行文字; 4)encoding參數很明顯是保存的編碼。 '''
        tree.write("b.xml", pretty_print=True, xml_declaration=True, encoding='utf-8')
        QMessageBox.information(self, "舒適提示", "xml生成成功!", QMessageBox.Yes, QMessageBox.Yes)
    ​
        
    except Exception as err:
        print('錯誤信息:{0}'.format(err))
        QMessageBox.information(self, "舒適提示", "生成xml失敗!", QMessageBox.Yes, QMessageBox.Yes)複製代碼

打包

  • 安裝pyinstall


  • pyinstall 命令:

    • -F, –onefile 打包一個單個文件,若是你的代碼都寫在一個.py文件的話,能夠用這個,若是是多個.py文件就別用

    • -D, –onedir 打包多個文件,在dist中生成不少依賴文件,適合以框架形式編寫工具代碼,我我的比較推薦這樣,代碼易於維護

    • -K, –tk 在部署時包含 TCL/TK

    • -a, –ascii 不包含編碼.在支持Unicode的python版本上默認包含全部的編碼.

    • -d, –debug 產生debug版本的可執行文件

    • -w,–windowed,–noconsole 使用Windows子系統執行.當程序啓動的時候不會打開命令行(只對Windows有效)

    • -c,–nowindowed,–console

例子:pyinstaller -F -w CrossRoadTool5.py

個人路徑在cmd進入C:\Python\Python37\Scripts,

執行:pyinstaller.exe -F -w D:\workspace\python\crossroad\FileDiffs.py

  • 注意事項

    • PyQt5 安裝時注意版本,須要安裝5.8.2(親測)版本或如下(未測),最新版本的pyinstall打包報錯

    • pycharm中配置的python.exe,確認是哪一個,是C中的,仍是本項目中的,影響tool配置。

相關文章
相關標籤/搜索