Make a window top priority - python

I have a root window with a panel on it. Then there is this function, in which I create a TopLevel (another window) for asking input from user. I'm trying to find some way to make it compulsory for user to either enter input and click OK or cancel to dismiss the window before being able to access the root window. It's like when an error message pops up, you can't just ignore it and do other things in the root window. Does anyone have any suggestion for me?

Have a look at Dialog Windows. You can use widget.wait_window(window) to achieve this.

You can do what is called a grab, which forces all events into the window of your choice. There are several methods for managing grab, including grab_set and grab_release.
For an example, see NiceGrab.
When working with grabs, exercise extreme care. It's possible to lock up your computer if you do a global grab and then have a bug that prevents you from releasing it. During development I will often implement a timer that kills the program after a minute or so, so if I lock everything up it will be automatically released after a short wait.

Related

How to disable keyboard shortcut Alt+F4, in PyQt5? [duplicate]

Ctrl+Escape is a global Windows shortcut for opening main system menu. But I would like my Qt application to use this shortcut without triggering Windows main menu. I know it is probably a bad idea to override system shortcuts in general, but I would like to use this shortcut is a very limited use case.
This usecase is as follows. I have a popup window containing several rows or items. This window is opened by Ctrl+Tab and while the user holds Ctrl and keep pressing Tab, the current rows are cycled through. When the user releases Ctrl, the current row is used for some operation... But sometimes it happens that user presses Ctrl+Tab and then realizes he does not want to continue. He usually presses Escape while still holding Ctrl. And then it triggers Windows system menu and normal user gets confused, choleric user get angry... which is a bad thing. In other words I would like to be able to close the popup window when user presses Ctrl+Escape. How to do that? It is even possible?
If I write the code using this shortcut like any other short, it does not work and it always triggers Windows main menu.
As I understand it, Qt will typically not receive the key event if the underlying window system has intercepted it. For example even QtCreator cannot override system-wide shortcuts.
This question is almost a duplicate of: C++/Qt Global Hotkeys
While that question is asking specifically to capture shortcuts in a hidden/background application, I think the basic concept is the same -- capture shortcuts before the window system processes them.
From that answer, UGlobalHotkey seems pretty good, and the How to use System-Wide Hotkeys in your Qt application blog post could be useful for your limited-use case (but read the comments on that blog post about fixing the example).
Also found:
https://github.com/mitei/qglobalshortcut
https://github.com/Skycoder42/QHotkey (looks like a more detailed version of above)

Window moving behind other open windows when button pressed (Python with Tkinter)

Apologies if this is a completely easy fix and I've just missed it. I am learning Python and I am trying to develop a GUI alongside my backend code using Tkinter. This is completely foreign to me and I have recently come across the problem that when I press my buttons there is a very small chance that it will make my window move behind all other open programs.
I am not entirely sure what is causing this but my guess is it stems somehow from one of two functions I have; one meant to minimise the main root window and the second to reveal it. However, these functions are not called in any place in my program that I would not expect them and the windows being minimised are not the root window (which my two functions act on).
I have both functions added below (hopefully, I am pretty new to SO) but if any additional code is needed I will supply it. I have quite a bit of code and everything else functions perfectly so I didn't want to post all my code is all.
There is no particular combination of button presses or buttons in particular which cause it, it appears to be any of them seemingly at random. It sends whatever window I have up to the taskbar.
def revealMenu():
root.update()
root.deiconify()
def hideMenu():
root.withdraw()

pywinauto does not detect child window

I'm using the latest version (to date) of pywinauto; and also using PyInspect (uia) to identify controls.
I'm automating controls on an application, and part of the process is to check a few boxes on a window that pops up after triggering the window to appear from a menu selection (like Edit->Settings).
The problem is, pywinauto doesn't seem to be able to detect the new window. I see successfully opens; and can see the window and elements as a sub (child?) window of the application in PyInspect.
I've tried wait methods, thinking the automation is occurring too fast- but to no avail. something like:
mysettings = app['app-name'].child_window(title_re="my target settings window", class_name="#32770").wait('exists', timeout=10)
this will just timeout. And if I print control identifiers, "my target settings window" is never included.
app['app-name'].print_control_identifiers()
I also tried set_focus on top_window.. that didn't work either. My conclusion is that pywinauto is having trouble detect that it is there. Any thoughts on this?
I was able to resolve this issue of pywinauto detecting the child window, and the issue that immediately followed: accessing the child window.
First, I was able to get pywinauto to detect the new window by defining backend='uia' in the application definition, like this:
app = application.Application(backend='uia')
I previously just had:
app = application.Application()
My next issue was accessing elements on the child window. I could not access the window directly, as I may have anticipated:
app['my app']['child window']['textbox'].set_edit_text("hello world")
Instead, this code worked:
app['my app'].child_window(title='child window name').Edit1.set_edit_text("hello world")
While I have resolved my issue, I have noticed that after defining backend='uia', the process now executes much slower than before. If anyone who stumbles across this has any feedback in that regard (or optimizing my efforts above), please contribute.
Thanks

Take all input in Python (like UAC)

Is there any way I can create a UAC-like environment in Python? I want to basically lock the workstation without actually using the Windows lock screen. The user should not be able to do anything except, say, type a password to unlock the workstation.
You cannot do this without cooperation with operating system. Whatever you do, Ctrl-Alt-Del will allow the user to circumvent your lock.
The API call you're looking for Win32-wise is a combination of CreateDesktop and SetThreadDesktop.
In terms of the internals of Vista+ desktops, MSDN covers this, as does this blog post. This'll give you the requisite background to know what you're doing.
In terms of making it look like the UAC dialog - well, consent.exe actually takes a screenshot of the desktop and copies it to the background of the new desktop; otherwise, the desktop will be empty.
As the other answerer has pointed out - Ctrl+Alt+Delete will still work. There's no way around that - at least, not without replacing the keyboard driver, anyway.
As to how to do this in Python - it looks like pywin32 implements SetThreadDesktop etc. I'm not sure how compatible it is with Win32; if you find it doesn't work as you need, then you might need a python extension to do it. They're not nearly as hard to write as they sound.
You might be able to get the effect you desire using a GUI toolkit that draws a window that covers the entire screen, then do a global grab of the keyboard events. I'm not sure if it will catch something like ctrl-alt-del on windows, however.
For example, with Tkinter you can create a main window, then call the overrideredirect method to turn off all window decorations (the standard window titlebar and window borders, assuming your window manager has such things). You can query the size of the monitor, then set this window to that size. I'm not sure if this will let you overlay the OSX menubar, though. Finally, you can do a grab which will force all input to a specific window.
How effective this is depends on just how "locked out" you want the user to be. On a *nix/X11 system you can pretty much completely lock them out (so make sure you can remotely log in while testing, or you may have to forcibly reboot if your code has a bug). On windows or OSX the effectiveness might be a little less.
I would try with pygame, because it can lock mouse to itself and thus keep all input to itself, but i wouldn't call this secure without much testing, ctr-alt-del probably escape it, can't try on windows right now.
(not very different of Bryan Oakley's answer, except with pygame)

Tell windows which monitor to display dialogs on

I've got a program which is using multiple monitors. The program is showing special visualizations on the second monitor. At one point, the program uses windows shell functions to send files to the recycle bin. However, when it does this, the delete confirmation dialog comes on top of my visualization. This is particularly problematic, as when the mouse is on the second monitor, my program uses mouse hooks to capture all mouse input, so the user cannot even click the confirmation dialog.
Is it possible to somehow tell Windows to only place dialog boxes on a particular display?
I'm using python, though if I have to call C WinAPI functions that shouldn't be a problem
which function are you using to send the files to the recycle bin? if you use SHFileOperation you can pass a parent HWND. perhaps make that an invisible WS_EX_TOOLWINDOW window on the other monitor.
i would expect the API, treating that window as a parent, would center relative to that window, but i haven't tried it.
depending on which version of Windows you are targeting, there used to be a capability to create desk bands that 'dock' to the sides of the screen. this automatically gets factored into the area returned as rcWork by GetMonitorInfo and should prevent dialogs from overlapping this space. There might be another way to declare that a region is "in use" in a way that declares space off-limits, but I don't know of it so it probably doesn't exist...
the ugly and crude thing you could do is poll and move the dialog yourself, but if this is any kind of widely deployed or commercial app that would likely cause more harm than good.

Categories