Making windows not appear over each other - python

Is there a way to check if part or an entire window is over/under another window in python?
I have two windows and I'd like to make them not appear over each other. This is in Windows, using Tkinter.

You can use the methods winfo_rootx and winfo_rooty to get the x/y in the upper left corner. You can use winfo_width and winfo_height to get the width and height of the window. From that it's just a little math to figure out if two windows overlap. You can then use the geometry method to position the windows anywhere on the screen.

Related

Adding a vertical and horizontal scroll bar to the display surface in Pygame

I'm using pygame and I've been working so far in a large monitor, but now I need to work on a small laptop, whose screen is much smaller than the mentioned monitor. The dimensions I gave to the display surface and the items blitted to it work well on the previous monitor but it is too large on the laptop and therefore I cannot reach all buttons. Since I can't seem to find a way to resize everything proportionaly, is there an easy way to add a vertical and horizontal scroll bar in order to navigate around the display surface?
Thank you
I don't think that you can add a scroll bar. What you can do is shift the x or y of everything when you press the corresponding arrow keys. But as said by Ted Klein Bergman, it is better to simply resize everything.

Overlay all screens and draw rectangle with a mouse

I am working on tiny program to capture screen print, I want to do it in a similar fashion that Win Snipping Tool is working. First I need to overlay all screens with a 50% opacity layer and then, using the mouse, draw a rectangle and read vertices coordinates. Honestly, I have no idea how to bite this. I tried with win32api / gui and it is great to get mouse coordinates, but still was unable to draw a rectangle. My idea (one of many) is to (using PIL / ImageGrab) take shots of both displays, put an overlay and print them as a full screen on all windows, but I failed while doing this. Other idea is to take img grab and create two new windows using BeeWare / Toga (that is GUI framework I am using) in full screen, but I was unable to find any method to open window on second display. Any ideas and hints will be greatly appreciated, I am really counting on you, as I feel I reached dead end.
Well,It is very easy to use tkinter.
Ok,It is the principle when I make my screenshot application:
User presses the button to start.
Make a new window whose width and height should full cover all the screens,and hide the title bar(If it is had to achieve,maybe use width=9999 and height=9999).
Take a screenshot of all the desktop(You can use ImageGrab.grab((),all_screens=True)) to do that.
Make the screenshot showed in a Canvas(I know that toga have this widget).
Start your mouse listener thread and save the position of pressed.
When user moves his mouse,create a rectangle(toga's Canvas have a function rect()).Maybe use this rect(pressed_x,pressed_y,move_x,move_y).And delete the last rectangle(Then it will always show only one rectangle).
When user released his mouse,save the position of released.And use ImageGrab.grab((pressed_x,pressed_y,released_x,released_y),all_screens=True) to crop the selected area.
If you want to show it in application interface.toga has a widget called ImageView.You can put the image in it.

pyautogui not moving window to correct location

I am using pyautogui to move a window to the top left corner of the screen. For some applications such as Excel and Skype, this works correctly. For other applications such as Chrome and Notepad, the window is instead moved to 10 pixels to the right of the upper left hand corner. Why is this happening?
Note that I am using python 3.6 and Windows 10.
See code example below.
import pyautogui
window = pyautogui.getWindow('MyChromeWindow')
window.move(0,0)
#window instead moves to (10, 0).
I can't recreate the problem , but you can overcome this issue wiht move to picture as i suppose (0,0) is standardbackroud rgb.

Python: Resizing widgets when window is resized (Using PLACE manager)

After lots of reading, I could not find solution for my problem.
I have made a quiz program using Tkinter and Python. I also used pack geometry manager everywhere. Window is not resizable, it's set to 960x540, and all widgets are precisely set in window using X and Y coordinates. Now, I'd like to make full screen option. But, when I turn it full screen, all widgets are moved in upper left corner (because they are set to X and Y coordinates using place manager). Any idea how could widgets 'stretch' when I turn window into full screen?
I know this could be accomplished using grid managed, but I would like to use pack manager instead.
I didn't post any code, because I don't think it would help. Please correct me if I'm wrong!
PS: Sorry for my weird English, and thank you a lot!
If you don't want to use grid you're going to need to use the place manager. A lot of people recommend against it because it's more complex, but I like the control it gives you over your GUI.
For example you can have a label that always stays in a relative position and has a relative width and height (in relation to the size of the screen)
newLabel = tk.Label(root)
newLabel.place(relwidth = 0.5, relheight = 0.2, relx = 0.25, rely = 0.4)
This creates a label that is always half the width of the root size, 20% of the root height, and is always centered in the screen.
These are two excellent tutorials on pack and place, and more importantly they are a great reference for the options that pack and place offer (scroll to the bottom of the page to see all the options and their descriptions). You may be able to get pack to do what you want, but I stick with place.
http://effbot.org/tkinterbook/pack.htm
http://effbot.org/tkinterbook/place.htm

Getting windows (client coordinates) for python window

I have loaded an image using Tkinter in Python. It opens up in a python window. If I try to get coordinates of a part of the image, it gives me coordinates relative to the python window that opened up. However, I would like to actually be able to find the client coordinates of that part of the image i.e. coordinates in relation to the actual computer screen.
I looked into using win32gui and somehow trying to get coordinates from the device context, however I could not figure out a way.
Any help is much appreciated!
You can use root.winfo_rootx() and root.winfo_rooty() to get the coordinates of the top left corner of the tkinter window. You can add those to the event.x and event.yto get the screen coordinates.

Categories