Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impressive! A "pin" checkbox in your titlebar? #1

Open
PySimpleGUI opened this issue Aug 22, 2021 · 5 comments
Open

Impressive! A "pin" checkbox in your titlebar? #1

PySimpleGUI opened this issue Aug 22, 2021 · 5 comments

Comments

@PySimpleGUI
Copy link

This right here:

image

is why I love to look at the things users are making.

I never know what I'm going to see. It's always something interesting.

Nice job of not just making something interesting, but posting a screenshot of it too. I'm impressed!
image

@PySimpleGUI
Copy link
Author

TIP - The User Settings API may work for your settings file since you're using a JSON file. It may save a lot of code while providing the same functionality you have.

It's documented here:
https://pysimplegui.readthedocs.io/en/latest/#user-settings-api

@ackness
Copy link
Owner

ackness commented Aug 23, 2021

yes, I use checkbox in titlebar because that when I set no_titlebar=True the program icon in task bar will disappear. So I use the 'Pin' checkbox to keep the window on the top.

@PySimpleGUI
Copy link
Author

PySimpleGUI commented Aug 23, 2021

Yea, at the moment I don't have an option to create a taskbar icon for no-titlebar windows or windows that have a custom titlebar. It's great for "Rainmenter" type of windows where you don't want an icon on the taskbar. But, will be nice to add the option late to include one.

I noticed that there is no call to change the keep_on_top setting so I just added 2 calls, one to set and one to clear. Not supposed to be doing features right now (so don't tell anyone) but this is a good one to quickly add. They're only on GitHub at the moment.

I made this little demo to show how to use them and your clever feature:

import PySimpleGUI as sg

"""
    Demo - Pin a window on top

    Note that the PIN used requires Python 3.7+ due to a tkinter problem
    This demo uses a Window call only recently added to GitHub in Aug 2021

    4.46.0.7 of PySimpleGUI provides the methods:
        Window.keep_on_top_set
        Window.keep_on_top_clear
        
    A temporary implementation is included in case you don't have that version

    Copyright 2021 PySimpleGUI
"""

def main():
    sg.theme('dark green 7')

    PIN = '📌'

    # This custom titlebar inveses the normal text/background colors. Uses a little bigger font
    my_titlebar = [[sg.Text('Window title', expand_x=True, grab=True,
                            text_color=sg.theme_background_color(), background_color=sg.theme_text_color(), font='_ 12', pad=(0,0)),
                    sg.Text(PIN, enable_events=True, k='-PIN-',  font='_ 12', pad=(0,0),  metadata=False,
                            text_color=sg.theme_background_color(), background_color=sg.theme_text_color())]]

    layout = my_titlebar + \
             [  [sg.Text('This is my window layout')],
                [sg.Input(key='-IN-')],
                [sg.Button('Go'), sg.Button('Exit')]  ]

    window = sg.Window('Window Title', layout, no_titlebar=True, resizable=True, margins=(0,0))

    while True:
        event, values = window.read()
        print(event, values)
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event == '-PIN-':
            window['-PIN-'].metadata = not window['-PIN-'].metadata     # use metadata to store current state of pin
            if window['-PIN-'].metadata:
                window['-PIN-'].update(text_color='red')
                window.keep_on_top_set()
            else:
                window['-PIN-'].update(text_color=sg.theme_background_color())
                window.keep_on_top_clear()

    window.close()

# Temp definitions of the Window methods added to 4.46.0.7 of PySimpleGUI
def keep_on_top_set(window):
    """
    Sets keep_on_top after a window has been created.  Effect is the same
    as if the window was created with this set.  The Window is also brought
    to the front
    """
    window.KeepOnTop = True
    window.bring_to_front()
    window.TKroot.wm_attributes("-topmost", 1)


def keep_on_top_clear(window):
    """
    Clears keep_on_top after a window has been created.  Effect is the same
    as if the window was created with this set.
    """
    window.KeepOnTop = False
    window.TKroot.wm_attributes("-topmost", 0)


if __name__ == '__main__':
    if 'keep_on_top_set' not in dir(sg.Window):
        print('You do not have a PySimpleGUI version with required methods. Using the temp ones from this file.')
        sg.Window.keep_on_top_set = keep_on_top_set
        sg.Window.keep_on_top_clear = keep_on_top_clear
    main()

Unfortunately, early versions of tkinter can't handle some UNICODE chars so you'll need Python 3.7+

pinned window

@ackness
Copy link
Owner

ackness commented Aug 24, 2021

Thanks for your nice demo!
I will update my codes on next version to use this nice feature!

@PySimpleGUI
Copy link
Author

Thank you for the idea.

I not only added a new capability to PySimpleGUI, but I also figured out a nice way to "patch" when an older version of PySimpleGUI is being used. I searched through all of the demos and this is the first one that I've used this technique.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants