Python Forum
Pysimplegui - Windows location - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Pysimplegui - Windows location (/thread-34870.html)



Pysimplegui - Windows location - jamesaarr - Sep-09-2021

Hello All,

I'm making a program using PySimpleGUI, and unlike my previous programs, I'm looking to make the windows appear in the same place as the previous window (Once an event is called).

I've tried saving the window.CurrentLocation to a variable and using that as the location in the second window, but that just flags errors. Any ideas?

Thanks,
James


RE: Pysimplegui - Windows location - deanhystad - Sep-09-2021

You should get the current window's location and use that to set the location of the new window.

The idea is sound, so the problem must be in the implementation. Post your code and the error you are getting. You should know the drill by now.


RE: Pysimplegui - Windows location - jamesaarr - Sep-10-2021

Hello there,

Apologies, a mock up, shortened version of my code is below and the error is further down.

import PySimpleGUI as sg
a = ()
def start():
    layout1 = [[sg.Button("Next")]]
    window1 = sg.Window("Test",layout1,size=(300,300),finalize=True)
    event,values = window1.read()
    a = window1.CurrentLocation()
    print(a)
    print(type(a))
    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "Next":
            window1.close()
            nextp()
            break
def nextp():
    global a
    layout2 = [[sg.T("")]]
    window2 = sg.Window("Test2",layout2,location=a,size=(300,300),finalize=True)
    event,values=window1.read()
start()
Error:
ValueError: not enough values to unpack (expected 2, got 0)
Now, as you can see I created an empty tuple to store the location (As it needs to be shared between functions, I've had to use a global one) and I've also printed the contents of 'a' which does display the x,y co-ordinates of the window, and changes depending on where the window is.

However it is saying there are no arguments given, which confuses me as they're there! :')

Thanks,
James


RE: Pysimplegui - Windows location - jamesaarr - Sep-10-2021

Hello,

I've found the issue, it isn't updating the Tuple for the second function so it is only providing an empty tuple as an argument....

This was the fix:
import PySimpleGUI as sg
b = []
def start():
    layout1 = [[sg.Button("Next")]]
    window1 = sg.Window("Test",layout1,size=(300,300),finalize=True)
    event,values = window1.read()
    a = window1.CurrentLocation()
    b.append(a[0])
    b.append(a[1])
 
    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "Next":
            window1.close()
            nextp()
            break
def nextp():
    a = (b[0],b[1])
    
    layout2 = [[sg.T("")]]
    window2 = sg.Window("Test2",layout2,location=(a[0],a[1]),size=(300,300),finalize=True)
    event,values=window2.read()
start()



RE: Pysimplegui - Windows location - FullOfHelp - Sep-11-2021

import PySimpleGUI as sg

b = (None, None)

def start():
    global b
    layout1 = [[sg.Button("Next")]]
    window1 = sg.Window("Test", layout1, size=(300, 300), finalize=True)
    event, values = window1.read()
    b = window1.CurrentLocation()


    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "Next":
            window1.close()
            nextp()
            break


def nextp():

    layout2 = [[sg.T("")]]
    window2 = sg.Window("Test2", layout2, location=b, size=(300, 300), finalize=True)
    event, values = window2.read()

start()
I think the basic problem was a missing global statement.


If you ever wanted to save the location across runs, you can use the user_settings APIs in PySimpleGUI.


import PySimpleGUI as sg

def start():
    layout1 = [[sg.Button("Next")],
               [sg.Sizer(300,300)]]
    window1 = sg.Window("Test", layout1, location=tuple(sg.user_settings_get_entry('-last position-', (None, None))))
    event, values = window1.read()
    sg.user_settings_set_entry('-last position-', window1.current_location())

    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "Next":
            window1.close()
            nextp()
            break

def nextp():
    layout2 = [[sg.T("")]]
    window2 = sg.Window("Test2", layout2, location=tuple(sg.user_settings_get_entry('-last position-', (None, None))), size=(300, 300))
    event, values = window2.read()

start()



RE: Pysimplegui - Windows location - jamesaarr - Sep-13-2021

Hello,

You're absolutely right, the lack of a global statement was the issue. However I am trying to avoid using global variables as much as humanly possible.

Instead I've got it saving to a list that then clears once the second window is called.

Thanks,
James


RE: Pysimplegui - Windows location - FullOfHelp - Sep-13-2021

You can also return the position from the function rather than store it. That way the other window will have it and you don't need a global.


RE: Pysimplegui - Windows location - FullOfHelp - Sep-13-2021

You can also return the position from the function rather than store it. That way the other window will have it and you don't need a global.
(sorry... I posted twice and cannot find the delete button)....