I'm currently trying to write an Image Viewer, but the "Forward/Backward" Buttons on the mouse are not triggering any of the possible binds in tkinter.
I tried the binds:
<Key>, <KeyPress>, <ButtonPress>, <Return>
but none of them can detect the mouse clicks on the forward/backward button.
Has Python even the possiblity to detect them?
Edit:
The buttons are called XButtons and are hookable with PyHook or PyQt, but if I try to let them run in the background, they are lagging massively, any way to prevent that?
I'm currently trying to let this script run smoothly in the background without causing extreme lags
Edit:
Using wxPython with PyHook solved my problem, since Tkinter and PyHook is causing huge lags, which don't appear with wxPython
The answer may vary by platform, and also by mouse driver.
On my OSX device where I have a logitech mouse with a total of five buttons, the fourth and fifth buttons are considered buttons 4 and 5. For example, I can bind to <Button-4> and <Button-5>.
That might work for you, or your mouse driver might assign those buttons to something else.
Related
I am writing a python script that automates running a program and performing different tasks within the program. My main problem is figuring out how to click buttons and interact with the GUI of the program to be controlled.
I am currently using the pyautogui library and using pyautogui.click(X,Y) to advance through prompts and click on different menus and menu items. The problem with this approach is that I am relying on a separate script to inform me of the coordinates of interest in my environment by telling me the coordinates of where my cursor is hovering. This probably will not work on other machines and just seems like a one case solution.
My question is how can I automate using a program in windows (clicking around) without having to hard code the exact position of the items I need to click?
For example, If I need to click a "ok" box to accept some setting, how can I make Windows grab the program window, read through the options and click what I need without any prior knowledge of the position of the dialog box and where the "Ok" button is located?
Code:
import pyautogui as gui
gui.click(x,y)
The way you can do this using pyautogui is with their locating methods. You will need a picture (for example of the OK box) and then you can have pyautogui find it on the screen and give you its coordinates. Check out the official documentation on this.
Python 3
Tkinter
Hey, I'm looking for something that don't allow the user to leave a window if a variable is true/false.
basicly, somethings that keeps the mouse inside the tkinter window
Not directly, no. However you could monitor the mouse position with the <Motion> event and correct it every time it goes outside. How to correct it would depend on your OS; look into pyautogui for a crossplatform solution.
You can't force the mouse to stay in the window, though you can "grab" all of the mouse events, preventing the user from clicking anywhere but on your app. This is very dangerous as you can lock up your computer if your code has a bug in it.
See How can I make area outside toplevel unclickable?
Pressing command-H in OSX immediately hides the active window. How do I achieve the same effect, programmatically, from Python? Specifically, I'd like to find a particular window that my application creates and then be able to show & hide it programmatically.
I already know how to do this with pywin32 but I'm afraid my expertise there doesn't quite cover OSX as well.
If it helps, the window in question is one created by pygame. I know that pygame has pygame.display.iconify() but that doesn't satisfy my requirements - the window doesn't disappear immediately, but rather the disappearance is animated, and there's no corresponding "uniconify" function that I can find.
Well, this ended up working. When I want to hide the window, I do pygame.display.quit() and make my code properly handle not having a display. When I want to show it, I do pygame.display.set_mode(...) with the former resolution.
The net effect is that of hiding & showing the window. Unfortunately the window gets created in a different spot than where it started, and although apparently you can tell SDL to create the window in a particular spot, I haven't been able to find a way to get the window's location...
I am searching for a way to get the events when clicking the 'Back' and 'Forward' buttons on the side of a normal mouse.
This function can be found in Firefox (for going back/forward) and Sublime Text 2 (for switching between tabs).
I know that there is a Windows program set that is called pyHook, but I would like to have something for Python in Linux.
Thanks in advance.
I think the example code in the answer to this question might be useful to you: Mouse wheel in python curses
I would like to create an application that has 3-4 frames (or windows) where each frame is attached/positioned to a side of the screen (like a task bar). When a frame is inactive I would like it to auto hide (just like the Windows task bar does; or the dock in OSX). When I move my mouse pointer to the position on the edge of the screen where the frame is hidden, I would like it to come back into focus.
The application is written in Python (using wxPython for the basic GUI aspects). Does anyone know how to do this in Python? I'm guessing it's probably OS dependent? If so, I'd like to focus on Windows first.
I don't do GUI programming very often so my apologies if this makes no sense at all.
As far as I know, there's nothing built in for this.
When the window is hidden, do you want it completely invisible or can a border of a few pixels be showing? That would be an easy way to get a mouse hover event. Otherwise you might have to use something like pyHook to get system-wide mouse events to know when to expand your window.
The events EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW might also be useful here to know when the user has entered/left the window so you can expand/collapse it.
Expanding/collapsing can just be done by showing/hiding windows or resizing them. Standard window functions, nothing fancy.
By the way, you might want to use wx.ClientDisplayRect to figure out where to position your window. That will give you a rectangle of the desktop that does NOT include the task bar or any other toolbars the user has, assuming you want to avoid overlapping with those things.
Personally, I would combine the EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW that FogleBird mentioned with a wx.Timer. Then whenever it the frame or dialog is inactive for x seconds, you would just call its Hide() method.
I think you could easily just make a window that is the same size as the desktop then do some while looping for an inactivity variable based on mouse position, then thread off a timer for loop for the 4 inactivity variables. I'd personally design it so that when they reach 0 from 15, they change size and position to become tabular and create a button on them to reactivate. lots of technical work on this one, but easily done if you figure it out