PySide window appears below other windows - windows

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?

You can bring the window to the front with gui.raise_()

Related

icon not showing in taskbar on Windows [duplicate]

How do I set an application's taskbar icon in PyQt4?
I have tried setWindowIcon, and it successfully sets the icon in the top-left of the main window, but it does not affect the icon shown in the Windows 7 taskbar -- the taskbar icon remains the default Python pyw icon. Here is my code:
from PyQt4 import QtGui
app = QtGui.QApplication([])
mainwindow = QtGui.QMainWindow()
mainwindow.show()
app.setWindowIcon(QtGui.QIcon('chalk.ico'))
mainwindow.setWindowIcon(QtGui.QIcon('chalk.ico'))
app.exec_()
[update] I've tried placing the setWindowIcon() before the show(). I've tried it with other images, ico and png. Nothing helps.
I've found the answer, after some digging.
In Windows 7, the taskbar is not for "Application Windows" per se, it's for "Application User Models". For example, if you have several different instances of your application running, and each instance has its own icon, then they will all be grouped under a single taskbar icon. Windows uses various heuristics to decide whether different instances should be grouped or not, and in this case it decided that everything hosted by Pythonw.exe should be grouped under the icon for Pythonw.exe.
The correct solution is for Pythonw.exe to tell Windows that it is merely hosting other applications. Perhaps a future release of Python will do this. Alternatively, you can add a registry key to tell Windows that Pythonw.exe is just a host rather than an application in its own right. See MSDN documentation for AppUserModelIDs.
Alternatively, you can use a Windows call from Python, to explicitly tell Windows what the correct AppUserModelID is for this process:
import ctypes
myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
EDIT: Please see Ronan's answer: the myappid string should be unicode.
#DamonJW's answer will work, but there is a minor catch: myappid should be unicode (argument type is PCWSTR).
import ctypes
myappid = u'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
Otherwise getting the AppUserModelID will get wrong unicode characters (祭潣灭湡⹹祭牰摯捵⹴畳灢潲畤瑣瘮牥楳湯):
import ctypes
from ctypes import wintypes
lpBuffer = wintypes.LPWSTR()
AppUserModelID = ctypes.windll.shell32.GetCurrentProcessExplicitAppUserModelID
AppUserModelID(ctypes.cast(ctypes.byref(lpBuffer), wintypes.LPWSTR))
appid = lpBuffer.value
ctypes.windll.kernel32.LocalFree(lpBuffer)
if appid is not None:
print(appid)
That said, it is a minor thing, since Windows will still recognize the unicode string as "another process" and switch the icon accordingly.
You must set the AppUserModelID before your app shows any GUI. If you need to access other Windows 7 features you can have a look at Q7Goodies which is a Qt add-on for Windows 7 with a PyQt bindings.

break points in pycharm 2020.3

I am new to Pycharm and I have the below code with a breakpoint on 'print' (I have more code underneath this break point). When i run on debug, previously it would bring me to the debugger window to show me my variables up to the breakpoint. Now it doesn't and it stays on the console view and i have to manually click on the debugger view. Can someone please help me?
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print() # breakpoint is here
I have attached a screenshot below with the console window circled at the bottom once i run the code. I need it to go to debugger when i run the code in debug (shortcut = ^D)

Why does my daemon mysteriously close by itself?

I've created a small daemon which allows me to control my external display's brightness via DDC. The thing is, after it's been running for ~30 mins or so, it inexplicably closes.
My main.swift is very straightforward:
import Foundation
let application = NSApplication.shared
let applicationDelegate = AppDelegate()
application.delegate = applicationDelegate
application.activate(ignoringOtherApps: true)
application.run()
The strange thing is, it seems to exit with exit code 0, and when in the debugger Xcode says it ended normally. Yet if I print something after application.run() or add a breakpoint there, it never seems to fire.
I've tried adding various symbolic breakpoints to exit, but it never manages to catch where it's happening, and I've got no calls to exit in the codebase anyway.
Why might my application be closing by itself? What can I do to work out the cause?

How to get application name and version relating to the foreground window in Python

I want to register the application name and it's version relating to the foregroundwindow. I am able to get the application name as follows (in Python):
import win32gui
import win32process
import psutil
fgWindow = win32gui.GetForegroundWindow()
threadID, ProcessID = win32process.GetWindowThreadProcessId(fgWindow)
procname = psutil.Process(ProcessID)
applicname = procname.name()
But I would like to know, for example, if it is OUTLOOK.EXE 2010 or OUTLOOK.EXE 2013. How could I retrieve this information from the system?

Find windows on application from another desktop

I have same user logged in into windows 7 station with several simultaneous sessions (like Concurrent RDP or log in at station and then via RDP).
UPDATE:
Ok, my research in this question has been stuck at this point (python example to write less complicated code):
#!/usr/bin/env python
import ctypes
import ctypes.wintypes as wintypes
def enum_desktops():
GetProcessWindowStation = user32.GetProcessWindowStation
EnumDesktops = user32.EnumDesktopsW
EnumDesktopsProc = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.LPWSTR, wintypes.LPARAM)
hwinsta = GetProcessWindowStation()
def foreach_desktop(desk_name, lparam):
print("Desktop %s"%desk_name)
return True
EnumDesktops(hwinsta, EnumDesktopsProc(foreach_desktop), desk_lparam)
This function prints information about "Default" and "Winlogon" dektops. If we try to enumerate window stations, we'll get only "WinSta0", while I can see potentially target process started on different logon session.
So, what should I use to find window for target Desktop?
daemon is not an option at this point at all.
Have a background app or task tray applet that gets launched with every desktop session. (Easily installed by adding an EXE path to the following registry key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Current Version\Run).
The code that lives in that installed application will do two things:
All the desktop windows enumeration and manipulation that you need to do that can only interact with the local desktop.
Acts as a "Client" to your "server" app that runs on another desktop session. Your server app is what triggers the clients to do the window scanning. You can use almost any interprocess communication mechanism you want for this.
Already some time passed from the time the question was posted, but in case someone needs it, I will post an answer.
What you have to do is set a desktop for your current thread, which is calling FindWindow. In this way, your calling thread will operate in other desktop and will find a window. For this to achieve, you have to use SetThreadDesktop WinAPI function.
For more info, check MSDN documentation on SetThreadDesktop.

Resources