V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Tspm1eca
V2EX  ›  Python

请问 Python 中如何以管理员权限打开 PyQt 的 QLabel 外部连结?

  •  
  •   Tspm1eca · 2015-07-14 15:36:09 +08:00 · 2067 次点击
    这是一个创建于 3220 天前的主题,其中的信息可能已经有所发展或是发生改变。
    当我用管理员权限打开外部连结时,会出现错误:
    shellexecute failed (error 2)

    我的程序需要管理员权限。

    先谢谢任何帮助我的朋友。

    测试代码:
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from win32com.shell import shell
    import win32con, win32event, win32process
    from win32com.shell import shellcon
    import sys, ctypes

    class Win(QWidget):
    def __init__(self):
    super().__init__()
    self.resize(100, 100)
    version = QLabel('''<a style= "color:#55aaff; text-decoration:none; font-size:11pt; font-family:Consolas; font-weight: bold;" \
    href="http://www.google.com">Open Url</a>''', self)
    version.setOpenExternalLinks(True)

    def runAsAdmin(wait=True):

    cmd = sys.executable
    params = " ".join(['"{}"'.format(x) for x in sys.argv])

    procInfo = shell.ShellExecuteEx(nShow = win32con.SW_SHOWNORMAL,
    fMask = shellcon.SEE_MASK_NOCLOSEPROCESS,
    lpVerb = 'runas',
    lpFile = cmd,
    lpParameters = params)

    if wait:
    procHandle = procInfo['hProcess']
    obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE)
    rc = win32process.GetExitCodeProcess(procHandle)

    if not ctypes.windll.shell32.IsUserAnAdmin():
    runAsAdmin()

    else:
    app = QApplication(sys.argv)
    win = Win()
    win.show()
    sys.exit(app.exec_())
    2 条回复    2015-07-14 22:48:06 +08:00
    salemilk
        1
    salemilk  
       2015-07-14 17:42:29 +08:00
    line 9
    def __init__(self):
    ^
    IndentationError: expected an indented block

    初学者,,,但是测试的时候提示缩进错误呢。
    Tspm1eca
        2
    Tspm1eca  
    OP
       2015-07-14 22:48:06 +08:00
    @salemilk 這是因為在這貼上代碼, 缩进全不見了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3534 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 169ms · UTC 11:36 · PVG 19:36 · LAX 04:36 · JFK 07:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.