Tkinter session active whilst tabbed out? - python

The title may be a touch vague, but I'd like to make a Tkinter session in Python 3 be active whilst it is tabbed out.
Say I have a function bound to a keypress in my code, and this is built in Tkinter, is there a way for me to keep this keypress function active when I'm on other windows?

In short, no. Tkinter only processes keyboard events when it has the keyboard focus. If you want to capture all keypresses no matter which app has focus, you'll have to use something else, like pyhook if you're on windows. Other platforms will require other platform-specific solutions.

Related

Is there a way to simulate user input in the background

I'm searching for a programming language or python plugin to simulate user input, like clicking a button in a application(a bit like the python plugin 'Selenium'). The problem i have is, that I would like to have the application running in the background, so that I can still use my PC while the program is running.
Sorry if the question is vague, i just don't know where to start.
simulate user input, like clicking a button in a application
I suggest taking look at PyAutoGUI, it allows you to cause mouse move, mouse click, keyboard write and so on. It is cross-platform. I do not know if it will met your have the application running in the background requirement on all platforms you must support.

Use keyboard without calling pygame.display.set_mode

Does pygame.display.set_mode have to be used to utilize the keyboard keys?
Once I added it to my code everything worked - but I do not want to open a pygame window.
Does anyone know how to utilize the keyboard without having to call pygame.display.set_mode ?
You have to use pygame.display.set_mode to create a window, since it's this very window that recieves the keyboard events from your OS' window manager.
You can run pygame without a window (by setting the environment variable SDL_VIDEODRIVER to "dummy"), but you can't recieve keyboard events with pygame then.
Maybe you want to look into low-level hooks (e.g. here's an example for windows).

Key Event Handling using Tkinter in Python

check the following link out :
[PyObjC Key Event Handling Question] Key Events Handling using PyObjC in Mac OS X
This was my initial question. I somehow managed to find a built-in plugin to solve the Key Event Management, but using Python. It is called Tkinter.
from Tkinter import *
root = Tk()
def screenshot(*ignore): os.system("screencapture -s %s" % check_snapshot)
root.bind('<Return>', greet)
root.mainloop( )
On pressing return (enter) key, it would successfully call screenshot function, and it would work.
Now, what I am looking for is, whenever I press combination of keys, like Command+Shift+4, the above function should be call.
This should be done in the same manner for Command+Shift+3 and Command+Shift+5 as well.
This should be done by checking which combination of keys are pressed, and accordingly, their respective screenshot functions should be called.
Also, this app shortcuts shouldn't be just relied on this app's window or frame, the window / frame of this window shouldn't be visible, yet, the shortcuts should work and trigger their respective functions.
root.withdraw()
This is the built-in function which hides the Tkinter window, but then, I am unable to invoke any of the functions. These functions only work on Tkinter window, or else, keys shortcuts don't work.
Any help would be appreciated.
Tkinter events only work when the tkinter window has focus,and for it to have focus it must be visible. You cannot use tkinter to handle events while another program is in the foreground.
The format of an event is <modifier-modifier-event-detail>, with modifier and event being optional. Event is something like KeyPress, ButtonPress, ButtonRelease and so on. Detail gives more detail, such as which key, or which button. For example, <ButtonRelease-1> is for releasing mouse button 1 (one).
Modifier is where you specify control, alt, delete or shift, and you can have more than one. Shift is a bit special, because it is often interpreted by the OS before tkinter ever sees is. So, for example, "Shift-3" on an American English keyboard is "#". Thus, instead of <Shift-3> you would use <#>.
Putting that all together, command-shift-3 would be <Command-#>. However, if you do that on a Mac, it will intercept that event and do a screenshot, so the binding will only work on Windows and Linux. On each OS there are a few key bindings you cannot override.
The best description of the format to use for specifying events is the tcl/tk man page on bind. Even though you're asking about tkinter, the underlying engine is tcl/tk.

Can a Tkinter dialog be tied to a parent who is a completely different application

I am new to both python and gui programming.
I am working on a python script which would pop-up a Tkinter dialog which needs to sit on top of another application (say Outlook for example) and should not allow me to do anything in Outlook until I click the cancel button on the tkinter dialog or until the dialog is withdrawn.
I am looking for some examples on how to do this.
It's possible, but you have to be very, very careful. If you make a programming mistake you can effectively disable your computer.
What you want to do is called a "global grab". That is, you grab all events. You do this with the tkinter command grab_set_global. You should test your application by adding a timer that automatically kills your program after a set amount of time so you aren't forced to reboot.

How do I make a dialog box that waits for user response?

When you tkSimpleDialog.askinteger, the program stalls and waits for user input. What are the basics of writing my own method that would have the same effect? I want to make the same kind of dialog box, I just want to be able to request more information.
The problem that I'm having is that when I open the new window using Tk.Toplevel, the program does not wait for user input the way tkSimpleDialog.askinteger does.
First off, if you can use some other widget system like PyGtk or PyQt, you should seriously consider it. Tkinter is ancient, and the newer libraries have a lot more functionality (read: more things you don't have to reinvent). I've used PyGtk and like it a lot more than Tkinter, which I used in the old Python 1.x days.
That said, in Tkinter, you need to do this:
widget.wait_window(window)
This ties up the event loop waiting for the user to dismiss the dialog.
Reference: http://www.pythonware.com/library/tkinter/introduction/dialog-windows.htm

Categories

Resources