【发布时间】:2016-02-22 14:19:50
【问题描述】:
我有一个需要以管理员身份运行的 PySide 应用程序。我使用了this post.推荐的技术
import os
import sys
import time
from PySide.QtGui import *
from win32com.shell import shellcon
import win32com.shell.shell as shell
import win32con
def force_elevated():
try:
if sys.argv[-1] != 'asadmin':
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + ['asadmin'])
shell.ShellExecuteEx(nShow=win32con.SW_SHOWNORMAL,
fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
lpVerb='runas',
lpFile=sys.executable,
lpParameters=params)
sys.exit()
except Exception as ex:
print ex
class MyGui(QWidget):
def __init__(self):
super(MyGui, self).__init__()
self.show()
app = QApplication(sys.argv)
force_elevated()
splash = QSplashScreen("logo.png")
splash.show()
time.sleep(2)
gui = MyGui()
app.exec_()
Windows UAC 弹出并要求用户允许管理员权限。如果用户选择是,应用程序的一个新实例将以管理员模式启动。如果用户选择 no,try 语句将失败,程序继续在非管理员模式下运行。
问题是,在非管理员模式下运行时,应用程序在其他窗口后面启动,所以很容易认为它根本没有运行。在使用 cx_freeze 生成可执行文件后,即使您确实激活了管理员模式,它也可能发生。
我不能让窗口总是在最上面。我尝试将其临时设置为始终在顶部(将其置于前面),然后将其关闭,但这不起作用。我还尝试使用各种库和内置函数来定位进程 ID 并告诉 Windows 将其置于前面。它们要么根本没有工作,要么工作不可靠。
有没有人有可靠的方法来确保 PySide 窗口出现在顶部?
【问题讨论】: