Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Application launch position / location

riker86

Coder
Hi everyone,

I have been given a project in python and I'm not too familiar with it but I understand the basics due other coding work I've done.
The task is to basically make the application launch in last location it was closed.
There is no save button and no config (xml / ini etc.) currently used so I would assume it would need to create one of these, if not existing, or update an existing one upon closure of the window.

Everyone has different size monitors / monitor layouts and resolutions so is there a module that can assess this and save the X,Y co-ords per user configuration?
Or is there a better way to do this using the modules listed below? Or do I need to import an additional module?

Application is built for and runs on Linux. (CentOS mainly)

These are the current modules imported:
import os
import sys
import pygtk
pygtk.require('2.0')
import gtk
from gtk import glade
import subprocess
import datetime

Any help would be greatly appreciated.
 
Last edited:
Would something like this work?

Probably have to use gtk.get_position save the X,Y position of the window upon closing the application and save it to a file in the user's profile directory.
Then upon launch this file would be read by the application launches to those co-ords using gtk.Window.move.

I could be wrong as I have never programmed in python, gtk and have no idea how friendly it would be with MATE.
 
Hey there.

I myself have never worked with GTK and Python most certainly isn't my forte, but what exactly do you mean by "location the program was last closed in"?

Do you happen to be referring to the directory the program stays in(E.g. /Documents/b])?

Specifying what you mean by "location", will help.
 
Sorry I should have explained better.
By location I mean the position of the window displayed on the screen.

Say a user has a specific configuration of windows from applications displayed across 4 monitors (custom to each user) and there is only a small area that has nothing displayed on it (just the desktop background) then they would like to be able to drag said application to that area and have it reopen there every time as opposed to resetting on every open and dragging it to the available area on their desktop.

Does that make more sense?

Basically they want the application (single window app written in python) to open on their desktop in the last position they closed it.
 
Hey.

Thanks for responding to my question. Again, Python isn't my forte so I can't really help you with this. I simply asked the question as a way to get additional information that others can work from, as you didn't specify what you meant by "location". Try to be refer to what you mean in future questions, as it can help with getting responses a lot faster.

Anyway, I don't understand why one would want to write code that has the window be re-displayed from where it last was. Where the window was last located, is such a small detail that it doesn't really matter that much. Did the contractor/person who gave you the project, say why they wanted this feature in?
 
It's an inhouse application that was written years ago by our own devs, but it's been passed onto me.
I am of the same thoughts as you, why bother?
But the nature of the industry I am in, it's been asked by the end users (who bring in big money) for this to be implemented.
What they want they end up getting.
 
It's an inhouse application that was written years ago by our own devs, but it's been passed onto me.
I am of the same thoughts as you, why bother?
But the nature of the industry I am in, it's been asked by the end users (who bring in big money) for this to be implemented.
What they want they end up getting.
Well, seeing as it was written years ago, I also take it that it was written in Python back then too, correct? If so, then good luck porting it over to Python 3(If it was written prior to the release of 3 anyway).

As you said, what the end users want, is what they end up getting. But, the way I see it, the users are essentially asking for feature-creep to happen. Create and build unnecessary features that aren't important, that end up bloating the program to the point it takes forever to load-up. Python from what I've heard, is a slow language, so by installing more features into this Python program, you're asking for a program that can barely run at all.
 
I've actually managed to figure out A method and got demo code working now for this task, just need to somehow integrate it into the existing code without breaking it.

If you get bored enough it would be interesting to compare to how you would have done it.
 
This is more of a general logic suggestion rather than specific code, but I think it's worth mentioning.
When you store the user's state, you should figure out the width & height of the window, as well as the position - obviously. So you can do this by getting the X,Y of the top left and the X,Y of the bottom right of the window. This assumes the window is a square or a rectangle. That is all pretty standard, but the important part is this... When the user re-opens the program, use relative values. For example, if a user opens the program on Monitor A, it could be 960 pixels wide by 540 pixels tall, or in other words 50% width and 50% height of a 1920x1080 display. If the user then opens the program on Monitor B and it's 1280x720, it might be worth considering opening the program up at 640x360 which is 50% width and 50% height. You could use similar logic for how far away the program is from the bottom/top of the screen and the left/right sides of the screen. Using a % value you can apply relative distances based on the close position and the program opening (ex: 10% of screen space between the program & the right side on a 1920x1080 would be a program that is 192 pixels away from the right side).

I only think it's worth mentioning because of how many times I do end up opening a program on one of my monitors and later unplugging the laptop from the hub. If you don't use relative dimensions and positioning you will end up with a program that doesn't always look good or fit on the screen when the screen size is changed.
 
I should elaborate a bit further.
Everyone uses multiple monitors in different layouts and sometimes different sizes but the resolution is always uniform per user.
e.g. User1 - 3x 27" monitors horizontal all in 1440p | User 2 - 2x vertical 34" monitors in 4k resolution.
No user has individual resolutions per monitor.

Thanks for the suggestion of the size of the window but this wasn't requested and isn't probably an issue as long as it is in the location they want.
 
I should elaborate a bit further.
Everyone uses multiple monitors in different layouts and sometimes different sizes but the resolution is always uniform per user.
e.g. User1 - 3x 27" monitors horizontal all in 1440p | User 2 - 2x vertical 34" monitors in 4k resolution.
No user has individual resolutions per monitor.

Thanks for the suggestion of the size of the window but this wasn't requested and isn't probably an issue as long as it is in the location they want.
Well, millions of users have different resolutions, different zoom levels, different sizing, etc for their monitors. I'm not sure why you are so adamant that everyone uses identical resolutions on every monitor. The other part worth mentioning is the hundreds of times per week I and many others unplug our laptops from monitors and hook them up elsewhere - for example, when I do a presentation on a projector, or hook up my laptop to a client's rig, or connect with my TV, etc etc. The problem you are saying never happens actually happens on a daily basis for many, many developers, traveling business people (or other travelers), and even stay at home workers who have multiple displays around the house they can connect to.

A lot of people use different monitors with different sizes, and different resolutions, and may open up your program on a different monitor than it was originally closed on.
If you don't want to program for it, that's fine, but not everyone is using identical resolution monitors. For example, I have 1 vertical monitor on my desk specifically set up for testing mobile, so the resolution and settings for that monitor are wildly different than anything else because I use it that way. When I don't want to use my TV or want something from my computer to watch, I hook my laptop up to my bedroom TV. All of this requires programs to adjust and remember their positioning and display settings.

-------------
The OP has solved the issue already, so I definitely wanted to make sure anyone that stumbles across this in the future understands where I'm coming from, as well as has some additional resources to explore for Gtk window position in Python:
^These pages make some great points, such as the window position not always being reliably detected/set depending on platform specifications, but show you good code examples that can be used for window position when the platform allows it.

However, even those links are more for general positioning like centering. The gtk move() on the other hand is for exact positioning.
window.move(X, Y)

As for saving a closing position, you would just need to go with whatever local storage you have set up for app configuration, states, etc.
 

Buy us a coffee!

Back
Top Bottom