win32com打开2个进程

时间:2015-01-17 12:18:05

标签: python process double pyqt5

我使用QT-Designer使用PyQt5创建一个窗口。一切正常,但是当我运行冻结的exe文件时,两个进程被打开,只有一个进程在按下X后关闭。

有人能帮助我吗?感谢。

如果您需要有关该计划的更多信息,请发表评论!

编辑: 我找到了问题所在的地方:

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()

    # Run as admin
    import win32com.shell.shell as shell

    ASADMIN = 'asadmin'
    if sys.argv[-1] != ASADMIN:
        script = os.path.abspath(sys.argv[0])
        params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
        try:
            shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
        except:
            easygui.msgbox("You have to be admin.", "Error")
            sys.exit()

    sys.exit(app.exec_())

“#admin as admin”部分正在打开第二个进程。但为什么?也许你现在可以帮助我;)

编辑: 我从脚本中删除了这部分。我现在正在尝试修改py2exe脚本,以便强制用户成为管理员。这不起作用:

setup(
    options = {'py2exe': {'includes': "sip, PyQt5.QtNetwork, PyQt5.QtWebKit, PyQt5.QtPrintSupport", 'compressed': True}},
    windows = [{'script': file,
                "icon_resources": [(1, "[Icon]")],
                'uac_info': "requireAdministrator"}],
    data_files = [('platforms', [
        'C:/Python34/Lib/site-packages/PyQt5/plugins/platforms/qwindows.dll'
        ])],
)

有人知道如何强迫用户成为管理员吗?

1 个答案:

答案 0 :(得分:2)

好。看起来Python 3的py2exe不能强迫用户成为管理员。我已经在py2exe程序中添加了一些代码来实现这个功能:

# -*- coding: utf-8 -*-

from distutils.core import setup
import py2exe, sys, os
import easygui

manifest = '''
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
       <asmv3:trustInfo xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
         <asmv3:security>
           <asmv3:requestedPrivileges>
             <asmv3:requestedExecutionLevel
               level="requireAdministrator"
               uiAccess="false" />
           </asmv3:requestedPrivileges>
         </asmv3:security>
       </asmv3:trustInfo>
     </assembly>
'''

file = easygui.fileopenbox("Script")

sys.argv.append('py2exe')
setup(
    options = {'py2exe': {'includes': "sip, PyQt5.QtNetwork, PyQt5.QtWebKit, PyQt5.QtPrintSupport", 'compressed': True}},
    windows = [{'script': file,
                "icon_resources": [(1, "[Icon]")],
                'other_resources':[(24, 1, manifest)]}],
    data_files = [('platforms', [
        'C:/Python34/Lib/site-packages/PyQt5/plugins/platforms/qwindows.dll'
        ])],
)

尽管如此,感谢你的帮助:D