how to make tkinter program open above its icon in taskbar? - python

I made a program with tkinter to make groups of apps in taskbar
I want to open it above its icon in the taskbar but I think there isn't a specific function (after research) for it in tkinter so I made it to get mouse position using this code
to get x coordinate of the mouse (subtract 65 to be on the middle)
x = root.winfo_pointerx() - 65
and for y it will get the work area using win32api and then subtracts 60 so it can be above the taskbar a little
monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
work_area = monitor_info.get("Work")
constant_y = work_area[3] - 60
and this the geometry
root.geometry(f'100x50+{str(x)}+{str(constant_y)}')
but the problem that the program take a few seconds to open so if you move the mouse it won't open in the right place
https://drive.google.com/file/d/1jjFTB4xDM21yEV2Mn9nD1YLb9GSu8Fhd/view?usp=sharing
first this how I want it to open
second is the problem
my questions:
is there any function to do it directly without all this calculation?
or can I do all of this algorithms but the code will take the x and y when clicked the program and be constant even if you move the mouse?

Related

Autoclicker does not activate the window

I wanted to create an autoclicker that would "remember" current position of my mouse pointer, move to specific location on the desktop, perform a double click and then come back to where it was and will do this randomly every 1 to 4 seconds. This way I wanted to achieve an autoclick in a specific place and more or less be able to use my mouse to browse other stuff.
What I want to click is in a different window, it is a program that I leave open visible on one half of my desktop and on the other half I want to do other things. The problem is that auto clicker does not make the program an active window and the click does not work.
import pyautogui
import threading
import random
def makro():
z = random.randint(1,4) #timer set to random value between 1 and 4 seconds
(x, y) = pyautogui.position() #remember current position of mouse pointer
threading.Timer(z, makro).start()
pyautogui.doubleClick(1516, 141) #perform a double click in this location (this clicks do not make the window active and clicks do not work)
pyautogui.moveTo(x, y) #come back to original mouse pointer location
makro()
Thank you for help
I think adding
pyautogui.click(1516, 141) before pyautogui.doubleClick(1516, 141) could activate the window.

Why isn't win32api.SendMessage able to perfom mouse clicks in some applications

I'm on Windows OS and I'm trying to perform clicks on background (inactive) applications without bringing them to the front.
I've been successful in doing in in Microsoft Paint (painting a dot on a minimised paint instance) but for some reason this doesn't seem to work on a Java based application (RuneLite.exe) which I'm writing this program for.
Some additional information:
Paint has a lot of child windows and performing the click only seems to work when I execute it on a specific child window (class: Afx:00007FF758890000:8) this is the most inner window.
The RuneLite window structure is as follows:
RuneLite (class: SunAwtFrame)
unknown (class: SunAwtCanvas)
unknown (class SunAwtCanvas)
I've tried using the handle of all 3 of the above windows but none seem to work
The code is able to find the handle of the window and it is correct (checked with Spy++)
The window blinks orange in the taskbar after running the code so some event must have been fired
I have determined the coordinates by screenshotting the window and using paint to find coordinates of a specific location. (If there is a better way of doing this, please let me know)
from pywinauto.application import Application
amount = 2
handles = []
def start_apps():
for i in range(amount):
a = Application()
a.start("D:\\Users\\Arno\\AppData\\Local\\RuneLite\\RuneLite.exe")
time.sleep(15)
handle = a.window().child_window(class_name="SunAwtCanvas", found_index=1).handle # select the most inner window
handles.append(handle)
def do_click(h, x, y):
long_position = win32api.MAKELONG(x, y) # simulate the mouse pointer and send it to the specified coordinates
win32api.SendMessage(h, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position) # simulate mouse press
win32api.SendMessage(h, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position) # Simulate mouse up
start_apps()
print(handles)
time.sleep(20)
for h in handles:
do_click(h, 450, 290)
Output of the code
Does anyone know why this happens and how I could go about fixing it?

How to get window origin (position) in mate applet?

I try to get window origin in a mate panel applet.
To be more precise I want to know the position of my applet (x and y) on the screen because I have a button which show/hide a Gtk.window but I need to move that window next to my button (above, below, right, left depending on where the mate panel is)
The only way that I found is to call get_origin but there is a problem. It should return a tuple x,y but instead like the c function it require two integers and since python use pass by value of course it doesn't work.
This code is valid but useless:
window = self.get_window()
x = 0
y = 0
window.get_origin(x, y)
All other "way to use" get_origin (that you can found in any doc) does not work because it require 3 args (I don't know why)
So I'm looking for a way to get the position of my applet (even if it's not accurate) or to move my window next to my button.
I found an alternative get_root_coords
window = self.get_window()
x,y = window.get_root_coords(0, 0)
It works even with multiple screens and I'm able to move my Gtk.Window next to my button with it.

Python script to control mouse clicks

I created a small Python script using win32api to use on the popular game Cookie Clicker (a game where you have to click on a Big Cookie to gain points) just for fun. It has a function called "auto_clicker" that do just that: keeps clicking on the screen on the point the user defined. This is the script:
# -*- coding: utf-8 -*-
import win32con
import win32api
def clicker(x,y):
"""Clicks on given position x,y
Input:
x -- Horizontal position in pixels, starts from top-left position
y -- Vertical position in pixels, start from top-left position
"""
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def auto_clicker(x = -1,y = -1):
"""Keep clicking on position x,y. If no input is given, gets from actual
mouse position.
"""
if x == -1 | y == -1:
x,y = win32api.GetCursorPos()
while True:
clicker(x,y)
It works nicely, but I want to make some improvements:
How can I get the cursor position only when the user clicks instead when the function is called? I would prefer to not add another module
since win32api seems to contain everything I needed. Tried this
method without success.
How can I detect a keypress like "Escape", so I can exit from my program without the ugly hack I am using now (Ctrl+Alt+Del seems to give SetCursorPos denied access, so Python throws a error and exit the program).
Can I make this program portable? Seems like I can do using Tkinter and generating a invisible Tk window, but I tried to write something without success.
I don't think with win32api you can listen to clicks you can just generate them (not sure though). However, try using pyHook, it's a simple api easy to use and can be found here http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=Main_Page. With pyhook you can create a listener to listen to a mouse event and upon a mouse click you can do whatever you want, the example in the link shows you how. As for key press, you can use the same api for that too, also an example is provided, good luck!
use pynput . It can control mouse, keyboard, etc.
examples:
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position
mouse.move(5, -5)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)

Get global mouse position and use in python program?

As part of a larger project, I am trying to create a snapshot tool that works similar to the Mac OS X snapshot. It should take a first click, a second click, and return an image of the area created by the square.
I have some python functions that take an first point (x, y) and a second point (x, y) and create a snapshot of the square that those points create on the screenshot. The missing piece is getting the mouse locations of the initial click and second click, then passing that data to the python program to create the snapshot.
In other words, the flow of the program should be:
first click (save x, y)
second click (save x2, y2)
run snapshot.py using the saved clicked data to return the screenshot
I've only found solutions that can return the position of the pointer within a frame. If it helps, I'm using "import gtk" and "from Xlib import display"
edit: I have tried to use Tkinter to make an invisible frame that covers the whole screen. The idea was to use that invisible frame to get the exact coordinates of two mouse clicks, and then the invisible frame would disappear, pass the coordinates on to the screenshot function, and it would be done. However, the code I've been writing doesn't keep the frame transparent.
edit 2: This code can create a window, make it transparent, size it to the screen, then return the mouse coordinates on that window. I can use this to simply return the mouse coordinates on two clicks, then remove the window and send those coordinates to the snapshot code. When I run the below code line-by-line in the python shell, it works perfectly. However, whenever I run the code as a whole, it seems to skip the part where it makes the window transparent. Even if I copy and paste a block of code that includes the 'attributes("-alpha", 0.1)' into the python shell, it ignores that line.
from Tkinter import *
root = Tk()
root.attributes('-alpha', 0.1)
maxW = root.winfo_screenwidth()
maxH = root.winfo_screenheight()
root.geometry("{0}x{1}+0+0".format(maxW, maxH))
def callback(event):
print "clicked at: ", event.x, "and: ", event.y
root.bind("<Button-1>", callback)
def Exit(event):
root.destroy()
root.bind("<Escape>", Exit)
# root.overrideredirect(True)
root.mainloop()
I am open to using any c or c++ code, or any language's code, to return the coordinates of the mouse on a click. This guy wrote some code to actually make the computer click at given points, which may be on the same track as my problem.
It's just a indentation problem - you bound the callback in the callback by mistake - try this instead:
root.geometry("{0}x{1}+0+0".format(maxW, maxH))
def callback(event):
print "clicked at: ", event.x, "and: ", event.y
root.bind("<Button-1>", callback)
EDIT
Ok, here's a theory - maybe when you run it from the command line, it takes longer for the root window to appear, for some reason, so you set the alpha before it exists, and the alpha option gets ignored. Give this a try:
root.wait_visibility(root)
root.attributes('-alpha', 0.1)

Categories