I have a PySide app that needs to run as admin. I've used a technique recommended by 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 pops up and asks the user to allow admin rights. If the user selects yes, a new instance of the application starts up in admin mode. If the user selects no, the try statement fails, and the program continues to run in non-admin mode.
The problem is, when running in non-admin mode, the application starts up behind other windows, so it's easy to think it's not running at all. After using cx_freeze to make an executable, it can also happen even when you do activate admin mode.
I can't have the window always be on top. I've tried temporarily setting it as always on top (to bring it to front) and then turn it off, but this does not work. I've also tried using various libraries and built in functions to locate the process ID and tell windows to bring it to front. They either haven't worked at all, or don't work reliably.
Does anyone have a reliable way to make sure the PySide window appears on top?