PySimpleGUI/DemoPrograms/Demo_Invisible_Elements_Pin...

45 lines
1.8 KiB
Python
Raw Normal View History

import PySimpleGUI as sg
"""
Demo - "Pinning" an element into a location in a layout
2020-08-03 19:59:10 +00:00
Requires PySimpleGUI version 4.28.0 and greater
When using the tkinter port of PySimpleGUI, if you make an element invisible and then visible again,
rather than the element appearing where it was originally located, it will be moved to the bottom
of whatever it was contained within (a window or a container element (column, frame, tab))
2020-08-03 19:59:10 +00:00
This demo uses a new "pin" function which will place the element inside of a Column element. This will
reserve a location for the element to be returned.
2020-08-03 19:59:10 +00:00
Additionally, there will be a 1 pixel Canvas element inside the "pin".
This will cause the area to shrink when the element is made invisible. It's a weird tkinter thing. Not sure
exactly why it works, but it works.
For other ports of PySimpleGUI such as the Qt port, the position is remembered by Qt and as a
2020-08-03 20:01:05 +00:00
result this technique using "pin" is not needed.
Copyright 2020, 2022 PySimpleGUI.org
"""
2023-06-09 10:31:15 +00:00
layout = [ [sg.Text('Hide Button or Multiline. Buttons 1 & 2 hide Button 2')],
[sg.pin(sg.Multiline(size=(60, 10), key='-MLINE-'))],
2023-06-09 10:31:15 +00:00
[sg.pin(sg.Button('Button1')), sg.pin(sg.Button('Button2'), shrink=False), sg.B('Toggle Multiline')],
]
2020-08-03 19:59:10 +00:00
window = sg.Window('Visible / Invisible Element Demo', layout)
2020-08-03 19:59:10 +00:00
toggle = toggle_in = False
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
2020-08-03 19:59:10 +00:00
if event in ('Button1', 'Button2'):
window['Button2'].update(visible=toggle)
toggle = not toggle
elif event == 'Toggle Multiline':
window['-MLINE-'].update(visible=not window['-MLINE-'].visible)
window.close()