QT简单使用--给nuitka打包的exe添加界面

  前两天将提高阅读量的脚本使用 nuitka 打包成了exe文件。虽然可以使用命令行传递参数给程序,但是整体来说不太友好。
  趁着将python打包的兴致还在,干脆给打包的程序增加一个操作页面。

梳理一下自己的功能需求:

  1. 通过软件界面填写csdn账号id
  2. 在界面中可以看到每篇文章的阅读量变化

实现步骤

QT ui文件:
QT简单使用--给nuitka打包的exe添加界面_第1张图片 1. QLabel 提示信息:作者ID
2. QLineEdit csdn账号输入组件
3. QPushButton 确定按钮
4. QTextBrowser 显示文章当前阅读数量
5. 将.ui文件转换为python对象python.exe -m PyQt5.uic.pyuic XXX.ui -o XXX.py

QT对象调用及使用:

  1. 通过 QLineEdit 获取界面参数
    uid = self.lineEdit.text()

  2. 完成原 ReadArticleCounts 脚本的方法调用

    def start_script(self):
         uid = self.lineEdit.text()
    
         # 获取进程的pid
         self.pid = os.getpid()
    
         from ReadArticleCounts import main as readAC
         readAC(uid)
    
  3. 实现 button 按钮点击操作和对应函数的关联
    self.pushButton.clicked.connect(self.start_script)

  4. 重写关闭方法

     def closeEvent(self, event):
          """Shuts down application on close."""
          QMessageBox.about(self, '退出确认', '结束程序的运行')
          kill(self.pid)
          super().closeEvent(event)
    
  5. qt对象的调用完整代码:

    
    import os
    import sys
    
    from PyQt5.QtCore import QObject
    from PyQt5.QtCore import pyqtSignal
    from PyQt5.QtGui import QTextCursor
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWidgets import QMainWindow
    from PyQt5.QtWidgets import QMessageBox
    
    from hello import Ui_hello
    
    
    class myForm(QMainWindow, Ui_hello):
       def __init__(self):
           super(myForm, self).__init__()
           self.setupUi(self)
           self.pushButton.clicked.connect(self.start_script)
    
           # 获取终端输出流数据.
           sys.stdout = Stream(newText=self.updateText)
    
       def updateText(self, text):
           """将终端输出转到程序界面"""
           cursor = self.textEdit.textCursor()
           cursor.movePosition(QTextCursor.End)
           cursor.insertText(text)
           self.textEdit.setTextCursor(cursor)
           self.textEdit.ensureCursorVisible()
    
       def start_script(self):
           uid = self.lineEdit.text()
    
           # 获取进程的pid
           self.pid = os.getpid()
    
           from ReadArticleCounts import main as readAC
           readAC(uid)
    
       def closeEvent(self, event):
           """Shuts down application on close."""
           # Return stdout to defaults.
           QMessageBox.about(self, '退出确认', '结束程序的运行')
           # 关闭当前进程
           kill(self.pid)
           super().closeEvent(event)
    
    
    class Stream(QObject):
       """Redirects console output to text widget."""
       newText = pyqtSignal(str)
    
       def write(self, text):
           self.newText.emit(str(text))
    
    def kill(pid):
       # Windows系统
       cmd = 'taskkill /pid ' + str(pid) + ' /f'
       try:
           os.system(cmd)
       except Exception as e:
           print(e)
    
    
    if __name__ == "__main__":
       app = QApplication(sys.argv)
       win = myForm()
       win.show()
       sys.exit(app.exec_())
    

原有代码调整:

  • 引入界面后,不再通过 cmd 交互获取参数:csdn 账号,将相关的代码删掉.

nuitka 生成exe文件:

  • nuitka --standalone --mingw64 --nofollow-imports --follow-import-to=ReadArticleCounts,hello --remove-output --plugin-enable=pyqt5 --windows-icon-from-ico=readCounts.ico --output-dir=Rs --windows-disable-console main.py

代码地址:百度网盘 提取码:vjxw

运行效果:
QT简单使用--给nuitka打包的exe添加界面_第2张图片

你可能感兴趣的:(小程序,PyQt,qt,python)