0

I'm trying to open a tkinter window using PySimpleGUI towards the end of a lengthy program.

def raise_above_all(window):
 window.attributes('-topmost', 1)
 window.attributes('-topmost', 0)
window = sg.Window('File Selector', layout)
while True: # Event Loop
 event, values = window.read()
 raise_above_all(window.TKroot)
 break
window.close()

When I start a new console the window opens in the backgroud. If I restart the whole program it then opens in the front as it should. If I start a fresh console it again opens in the back. If I just run that section it opens in front.

How can this be? I even added a required input just before the window is supposed to open, thinking that maybe the program running for so long is causing some idle status which is causing the issue, but that didn't help.

Background thread

asked Aug 9, 2024 at 8:43
2
  • 4
    What is the idea of having a while true loop with an unguarded break? Commented Aug 9, 2024 at 8:45
  • since I don't have a timeout on the window.read it waits until I either click a button or close the window, but that answers my question I think Commented Aug 9, 2024 at 9:10

2 Answers 2

1

Here is the code I was mentioning in the comments.


window = sg.Window('FIRST window', layout, finalize=True, print_event_values=True)
window.bring_to_front()

I've not been able to duplicate the problem, but then again, I've got no idea of the environment, the operating system the version numbers, etc. Many variables at play so it's the best I can do is give you the parm to finalize the window so you can make the bring to front call.

You could also schedule a timer to delay a little bit before trying to bring it to the front. Here's a version with a timer designed in:

import PySimpleGUI as sg
layout = [ [sg.Text('The First Window', font='_ 20')],
 [sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('FIRST window', layout, print_event_values=True, repeating_timer_ms=100)
while True:
 event, values = window.read()
 if event == sg.WIN_CLOSED or event == 'Exit':
 break
 elif event == sg.TIMER_KEY:
 window.timer_stop_all()
 window.bring_to_front()
window.close()

Maybe consider opening an issue so more data can be provided.

answered Aug 15, 2024 at 0:41
Sign up to request clarification or add additional context in comments.

Comments

0

The problem was the code never got called on a new console. Rerunning it on the same console it still remembers to put the window on top. So my fix based on the question that showed my error from khelwood:

while True: # Event Loop
 event, values = window.read(timeout=100)
 # window.TKroot.focus_force() # this wasn't even necessary nor the sleep
 if event == sg.WIN_CLOSED or event == 'Submit'or event == 'Doesn\'t exist':
 break
 raise_above_all(window.TKroot)
window.close()
Jason Yang
13.1k2 gold badges11 silver badges29 bronze badges
answered Aug 9, 2024 at 9:19

4 Comments

window.bring_to_front() do the samething as your function rasie_above_all.
What's the purpose of the timeout? It's a serious use of CPU time. Finalize the Window when it's made, then call window.bring_to_front() before entering event loop (as Jason pointed out, it's what you're doing manually. Also, it may not work depending on the OS if you do it manually).
if I put any window.bring_to_front() or similar before the window.read I get an error. If I put them after the window.read without having a timeout, they never get executed
Note the instructions to "Finalize the window". Set the parameter finalize=True when you create the window.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.