python 直接调用py代码执行

# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'TESTpythoncode.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
 
import sys,json
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication

sys.path.append("..")
from QhSetting import QHJSPATH
print(QHJSPATH)
 
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(901, 616)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(270, 160, 221, 61))
        self.pushButton.setObjectName("pushButton")
 
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
 
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))
 
 
class TESTpythoncode(QtWidgets.QWidget,Ui_Form):
    # 用的是多继承
    def __init__(self,qh_parent = None):
        super().__init__(qh_parent)
        # self._translate = QCoreApplication.translate  # 修改控件名称初始化 函数
        # self.QH_UI = Qh_Ui_Form.Ui_Form()   # 实例化对象QH_QmyUI
        self.setupUi(self)                   # 调用Qh_SetupUI()函数,并把窗口作为实参传递给Qh_SetupUI()函数
        # self._Qh_SetQss()                    # 调用设置Qss
        # self._Qh_close_max_min_icon()        # 设置最大最小化关闭
        # self._Qh_CaoFunction_Connect()       # 调用槽函数
        # self._QH_init_main_window()          # 设置窗口无标题栏
        self.pushButton.clicked.connect(self.QhTescmd)
 
    def QhTescmd(self):
        # 导入subprocess模块
        import subprocess,os
        QhRunDickies= {
            "Status":""
        }
        Qhtxt = "QhTescmd"
        QhtxtPath = r"{}\test\QhRunLog".format(QHJSPATH)
        QhtxtPath = r"{}\{}.txt".format(QhtxtPath,Qhtxt)
        # 假设name和qcy是你要传递给脚本的变量
        name = "exampleName"
        qcy = "exampleQcy"
 
        if os.path.exists(QhtxtPath):
            with open(QhtxtPath,'r') as f:
                msg = f.read() # 读取所有内容-下面的再读就读不到了
                print(msg)
                QhRunDickies = json.loads(msg.replace("'",'"'))
                print(msg)
                f.close()
            if QhRunDickies["Status"] == "Running":
                print("正在运行")
                return
            else:
                print("正在运行")
                command = '''start cmd /k "E: & cd {}\QhInterFace & python QhDfInterfaceH.py  && exit"'''.format(QHJSPATH)
                # 执行命令
                subprocess.run(command, shell=True,check=True)
                
                QhRunDickies["Status"] = "Running"
                with open(QhtxtPath,'w') as f:
                    msg = f.write(str(QhRunDickies)) # 读取所有内容-下面的再读就读不到了
                    print(msg)
                    f.close()
        else:
            # # 构建命令字符串,注意参数的引号处理
            command = '''start cmd /k "E: & cd {}\QhInterFace & python QhDfInterfaceH.py && exit"'''.format(QHJSPATH)
            # 执行命令
            subprocess.run(command, shell=True,check=True)
            # command =  '''start cmd /k "E: & cd E:\QhStockResApp8.1\QhInterFace & python QhDfInterfaceH.py "{$name$:$qcy$}" && exit"'''
            # # command = f'start cmd /k "e: & cd E:\QhStockResApp8.0 & python QhAllStockMain.py QhAllStock && pause"'
 
            # os.system(command)
            
            print("执行中")
            QhRunDickies["Status"] = "Running"
            with open(QhtxtPath,'w') as f:
                msg = f.write(str(QhRunDickies)) # 读取所有内容-下面的再读就读不到了
                print(msg)
                f.close()
            
 
        
 
        # import os
 
 
        # command =  '''start cmd /k "d: & cd D:/QhTest/Stock/QhStockResApp/QhInterFace & python QhDfInterface.py "{$name$:$qcy$}" && exit"'''
 
        # os.system(command)
 
        
 
if __name__ == "__main__":
    qh_app = QApplication(sys.argv)                 # 创建应用实例
    qh_MyWindows = TESTpythoncode()                        # 创建窗口实例
    qh_MyWindows.show()                             # 显示窗口
    qh_n = qh_app.exec()              # 执行exec()方法,进入事件循环,如果遇到窗口退出命令,返回整数qh_n
    print(qh_n)                       # 输出输出关闭事件返回的整数
    try:                              # 捕获程序退出事件
        sys.exit(qh_n)                    # 通知python系统,结束程序运行
    except SystemExit:
        print("请在此做一些其他工作。")       # python解释器停止执行前的工作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值