4

I have run into several problems wanting to customize windows in Pysimplegui using use_custom_titlebar = True.

My system:

  • Windows 10
  • Python 3.9.1
  • PySimpleGUI 4.38.0

Problems:

  1. The icon does not appear on the taskbar, it only appears when I minimize the window. Consequently, if my window is hidden under another window of another application, I will not be able to access it or know that it is open until I have removed the window above it by covering it. I also can't access doing alt+tab
  2. I can't disable the maximize window option. The argument resizable = False does not seem to affect the behavior. It also doesn't work if I set a fixed window size with the size argument

I appreciate in advance any contribution. Thank you

Example code:

import PySimpleGUI as sg

layout = [[sg.B('Exit', size = (12,3))]]
window = sg.Window('My window',
                     layout,
                     use_custom_titlebar = True,
                     size = (None, None),
                     resizable = False)
while True:
    event, values = window.read()
    if event in (None,'Exit'): 
        break
window.close()
ArielMdp
  • 43
  • 1
  • 5

1 Answers1

3

use_custom_titlebar=True sets no_titlebar=True for your window. Under Windows, this sets window.TKroot.wm_overrideredirect(True), causing the (system) window manager to ignore the window. As one of the consequences, there is no taskbar icon.

Tcl/Tk documentation: wm overrideredirect

There are some tricks to deal with it when working directly with Tkinter (see, e.g., Making Tkinter windows show up in the taskbar) but we cannot do this directly via PySimpleGUI so far. Currently, I'm exploring how to make a dummy window to appear in the taskbar and to couple it with my window, but I don't have a working solution yet.

To keep my main window accessible even if it's behind another window, I'm using a system tray that always stays on top. (You may want to consider setting keep_on_top=True for your window, but sometimes it's simply not convenient.)

To prevent maximisation, you can either tweak the function Titlebar() in PySimpleGUI and comment out its part where the maximise-symbol is added like this:

    return Column([[Column([icon_and_text_portion], pad=(0, 0), background_color=bc),
                    Column([[T(SYMBOL_TITLEBAR_MINIMIZE, text_color=tc, background_color=bc, enable_events=True, font=font, key=TITLEBAR_MINIMIZE_KEY),
                             # Text(SYMBOL_TITLEBAR_MAXIMIZE, text_color=tc, background_color=bc, enable_events=True, font=font,key=TITLEBAR_MAXIMIZE_KEY),
                             Text(SYMBOL_TITLEBAR_CLOSE, text_color=tc, background_color=bc, font=font, enable_events=True, key=TITLEBAR_CLOSE_KEY),
                             ]],
                           element_justification='r', expand_x=True, grab=True, pad=(0, 0), background_color=bc)
                    ]],
                  expand_x=True, grab=True,  background_color=bc, pad=(0,0),metadata=TITLEBAR_METADATA_MARKER, key=key)

or you can remove the titlebar completely using no_titlebar=True in your window and add your own titlebar at the beginning of your layout, for example with this function (tweaked from https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Window_Background_Image.py):

titlebar = [sg.Col([[sg.Text(your_titlebar_text, text_color=tc, background_color=bc, font=font, grab=True)]],
                   pad=(0, 0), background_color=bc),
            sg.Col([[sg.Text(sg.SYMBOL_X, background_color=bc, enable_events=enable_events)]],  # '❎'
                   element_justification='r', key='-X-', grab=True, pad=(0, 0), background_color=bc)]

(you may want to add sg.HorizontalSeparator() as an extra row in the titlebar).

As an example:

import PySimpleGUI as sg

titlebar = [[sg.Col([[sg.Text('My window', grab=True)]], pad=(0, 0)),
             sg.Col([[sg.Text(sg.SYMBOL_X, enable_events=True, key='-X-')]],  # '❎'
                    element_justification='r', grab=True, pad=(0, 0), expand_x=True)],
            [sg.HorizontalSeparator()]]

layout = titlebar + [[sg.B('Exit', size=(12, 3))]]
window = sg.Window('My window', layout, no_titlebar=True, finalize=True)
while True:
    event, values = window.read()
    print(event)
    if event in (None, 'Exit', '-X-'):
        break
window.close()

enter image description here

  • 1
    Excellent lenka_cizkova !! I tried commenting on the line you indicated in pysimplegui.py and it worked. Then I decided to uncomment it and put the value enable_events = False to the text object, so that it continues to appear visible but without functionality – ArielMdp May 22 '21 at 01:11
  • I believe it can be confusing for the user to have GUI elements that are visible and seem to be enabled - but not executing their usual action. But if you think it's a good solution in your case: why not... :) – Lenka Čížková May 23 '21 at 12:07