python pysimpleGUI changing keep on Top

Question:

I have a couple programs I am trying this on here is a smaller one. I have a right click set up I want to change keep_on_top=True to keep_on_top=False
as you can see I am able to change the Alpha level but not able to figure out how to make this change here is the code of a simple local and Zulu time clock

import PySimpleGUI as sg
import pytz
from datetime import datetime

Cur_Time_Date=''
Cur_Time_DateUTC=''
ALPHA = 0.9  # Initial alpha until user changes
def update_window():
    current_datetime = datetime.now()
    Cur_Time_Date = current_datetime.strftime("%H:%M:%S (L)     %m/%d/%y")
    utc_time = datetime.now(pytz.utc)
    Cur_Time_DateUTC = utc_time.strftime("%H:%M:%S (Z)     %m/%d/%y")
    window['-LCL-'].update(Cur_Time_Date + '(L)')
    window['-UTC-'].update(Cur_Time_DateUTC + '(Z)')

def create_window():
    right_click_menu = [[''],['keep_on_top', 'Alpha', [str(x) for x in range(1, 11)], 'Exit', ]]
    layout = [
        [sg.Text(Cur_Time_Date, key = '-LCL-')],
        [sg.Text(Cur_Time_DateUTC, key = '-UTC-')]
    ]
    return sg.Window('Local/UTC time', layout, alpha_channel=ALPHA, no_titlebar=True, grab_anywhere=True, right_click_menu=right_click_menu, keep_on_top=True)
window = create_window()

while True:
    event, values = window.read(timeout=1000)
    if event in (sg.WIN_CLOSED, '-CLOSE-'):
        break
    elif event in (sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'):
        break
    elif event == 'keep_on_top':
        sg.popup( title = 'Keep On Top', keep_on_top=True)
    elif event in [str(x) for x in range(1, 11)]:
        window.set_alpha(int(event) / 10)

    #window.close
    update_window()
Asked By: Rjtaylor

||

Answers:

I figured it out if anyone else is looking
First, I set the variable and setup my own popup box:

keepOnTop = True
def My_popup():
    layout = [[sg.Text('Keep On Top')],
          [sg.Push(), sg.Yes(),sg.No(), sg.Push()]]
    window = sg.Window('', layout, keep_on_top=True)
    event, values = window.read()
    window.close()
    return (event)

I set the variable to start and used it to set at the beginning.
Then I used this when selecting option from right clicking

elif event == 'keep_on_top':        
        ans=My_popup()
        print(ans)
        if ans == 'Yes':
            keepOnTop=True
        else:
            keepOnTop=False
        print(keepOnTop)
        window.close()
        window = create_window()

I also needed to add, finalize=True to the window

Answered By: Rjtaylor
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.