推荐学习书目
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 · Jul 14, 2015 · 3003 views
    This topic created in 3999 days ago, the information mentioned may be changed or developed.
    当我用管理员权限打开外部连结时,会出现错误:
    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 replies    2015-07-14 22:48:06 +08:00
    salemilk
        1
    salemilk  
       Jul 14, 2015
    line 9
    def __init__(self):
    ^
    IndentationError: expected an indented block

    初学者,,,但是测试的时候提示缩进错误呢。
    Tspm1eca
        2
    Tspm1eca  
    OP
       Jul 14, 2015
    @salemilk 這是因為在這貼上代碼, 缩进全不見了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1259 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 23:54 · PVG 07:54 · LAX 16:54 · JFK 19:54
    ♥ Do have faith in what you're doing.