Python Forum
Pysimplegui - Windows location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pysimplegui - Windows location
#1
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
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#2
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.
Reply
#3
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
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#4
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()
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#5
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()
jamesaarr likes this post
Reply
#6
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
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#7
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.
Reply
#8
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)....
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020