Giter VIP home page Giter VIP logo

Comments (4)

jason990420 avatar jason990420 commented on May 30, 2024

Basically, there's no method provided to remove any element in PySimpleGUI, but method update with option visible to hide or unhide the element.

import PySimpleGUI as sg

layout = [
    [sg.TabGroup(
        [
            [sg.Tab(
                f'TAB {i}',
                [[sg.Text(f'This is the Tab {i}')]],
                key=f'Tab {i}',
            ) for i in range(5)],
        ],
        key='TabGroup')],
    [sg.Push(), sg.Button("Hide Tab 3"), sg.Button("Show Tab 3")],
]
window = sg.Window('Tab Group', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == "Hide Tab 3":
        window['Tab 3'].update(visible=False)

    elif event == "Show Tab 3":
        window['Tab 3'].update(visible=True)

window.close()

image

from pysimplegui.

yutianlong avatar yutianlong commented on May 30, 2024

Basically, there's no method provided to remove any element in PySimpleGUI, but method update with option visible to hide or unhide the element.

import PySimpleGUI as sg

layout = [
    [sg.TabGroup(
        [
            [sg.Tab(
                f'TAB {i}',
                [[sg.Text(f'This is the Tab {i}')]],
                key=f'Tab {i}',
            ) for i in range(5)],
        ],
        key='TabGroup')],
    [sg.Push(), sg.Button("Hide Tab 3"), sg.Button("Show Tab 3")],
]
window = sg.Window('Tab Group', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == "Hide Tab 3":
        window['Tab 3'].update(visible=False)

    elif event == "Show Tab 3":
        window['Tab 3'].update(visible=True)

window.close()

image

Thanks for your comment,
In fact, I use multithreading in the tabs to initiate video previews. Simply hiding the tabs would waste more resources and connections. Repeatedly initiating them may lead to resource leaks.

from pysimplegui.

jason990420 avatar jason990420 commented on May 30, 2024

Simply hiding the tabs would waste more resources and connections. Repeatedly initiating them may lead to resource leaks.

Following code demo a way to delete a container element. Not sure if it work for all the cases. At least, some information still kept and not destroyed, for example, images, so there will still have some resource leaks.

Most of time, I will reuse the hidden element if new element required.

import PySimpleGUI as sg

def delete(widget):
    for w in widget.pack_slaves():
        if w in all_keys:
            del window.AllKeysDict[all_keys[w]]
        delete(w)
    widget.destroy()

layout = [
    [sg.TabGroup(
        [
            [sg.Tab(
                f'TAB {i}',
                [[sg.Text(f'This is the Tab {i}')]],
                key=f'Tab {i}',
            ) for i in range(5)],
        ],
        key='TabGroup')],
    [sg.Push(), sg.Button("Delete Tab 3")],
]
window = sg.Window('Tab Group', layout, finalize=True)
all_keys = {widget:key for key, widget in window.AllKeysDict.items()}

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == "Delete Tab 3":
        if 'Tab 3' in window.AllKeysDict:
            tab = window['Tab 3'].widget
            delete(tab)

window.close()

from pysimplegui.

PySimpleGUI avatar PySimpleGUI commented on May 30, 2024

You've got valid areas of concern. I don't think we're into problem territory currently. The amount of memory a Tab uses is very small. Hiding and unhiding elements doesn't create nor destroy objects so there should be no resource leak in that operation. If you do some profiling/measuring and find a leak, by all means let us know. I routinely run applications for days and haven't had trouble.

from pysimplegui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.