简体   繁体   中英

PySimpleGUI currently selected tab and its key

While working on my project I came across a problem regarding tabs and tabgroup in PySimpleGUI. Is there a function that returns currently selected tab? Having many tabs I would like being able to select each of them (but not at the same time) and return the key of the currently selected one/active one. I've tried .Get() and .Select() but seems like these don't do it or maybe I'm doing something wrong? Please keep in mind I'm a total beginner. I also made sure to enable events in every tab. I did something like this: curr_sel_tab= tab_group.find_key_from_tab_name(tab_group.Get()) but it returns the name of the tab instead of the key.

Basically my app is connected to a database. What I'm trying to do is to select a row in a tab(1,2,3 or 4) and delete it. But in order to do it I need a key of the tab(I guess). Below you can find a fragment of code I stuck upon as well as a screenshot of my app.

import PySimpleGUI as sg 
import baza # external file with my databse

sg.theme("GreenTan")

left_col = [sg.Button("Create")],[sg.Button("Read")],[sg.Button("Update")],[sg.Button("Delete")]

data = baza.get_db_obiad()
print(data)
headings2 = ['Id', 'Name', '1', '2', '3']
layout_1 = [[sg.Table(values=data[0:][:], headings=headings2, max_col_width= True,
                    auto_size_columns=False,
                    display_row_numbers=False,
                    enable_events=True,
                    justification='c',
                    alternating_row_color='lightyellow',
                    key='-TAB_1-',
                    row_height=35)]]

data1 = baza.get_db_podkladka()
headings3 = ['Id','Name']
layout_2 = [[sg.Table(values=data1[0:][:], headings=headings3, max_col_width= True,
                    auto_size_columns=False,
                    display_row_numbers=False,
                    enable_events=True,
                    justification='c',
                    alternating_row_color='lightyellow',
                    key='-TAB_2-',
                    row_height=35)]]

data2 = baza.get_db_mieso()
headings4 = ['Id','Name']
layout_3 = [[sg.Table(values=data2[0:][:], headings=headings4, max_col_width= True,
                    auto_size_columns=False,
                    display_row_numbers=False,
                    enable_events=True,
                    justification='c',
                    alternating_row_color='lightyellow',
                    key='-TAB_3-',
                    row_height=35)]]

data3 = baza.get_db_dodatki()
headings5 = ['Id','Name']
layout_4 = [[sg.Table(values=data3[0:][:], headings=headings5, max_col_width= True,
                    auto_size_columns=False,
                    display_row_numbers=False,
                    enable_events=True,
                    justification='c',
                    alternating_row_color='lightyellow',
                    key='-TAB_4-',
                    row_height=35)]]

tab_group = sg.TabGroup([[sg.Tab("Tab 1", layout_1),
                             sg.Tab("Tab 2", layout_2),
                             sg.Tab("Tab 3", layout_3),
                             sg.Tab("Tab 4", layout_4)]], 
                             enable_events=True)
right_col = [[tab_group]]


layout = [[sg.Column(left_col, justification="c"), sg.Column(right_col)]]

window = sg.Window("", layout).Finalize()
window.Maximize()

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == "Exit":
        break
    elif event == "Create":
        pass
    elif event == "Read":
        pass
    elif event == "Update":
        pass
    elif event == "Delete":
        if sg.popup_yes_no("Are you sure you want to delete this record?"):
            curr_sel_tab= tab_group.find_key_from_tab_name(tab_group.Get())
            print(curr_sel_tab)
        else:
            break

window.close()

Screenshot from my app

I had a similar issue. I was able to figure out that the values object contains the key of the tab that has been clicked on. You would just need to figure out what index its at, Before you do that. it would be a good idea to give each tab a key so that you know what tab was clicked on. Made some edits to your code below: Notice how I added the keys to teach tab:

tab_group = sg.TabGroup([[sg.Tab("Tab 1", layout_1, key= "tab1"),
                         sg.Tab("Tab 2", layout_2,  key="tab2"),
                         sg.Tab("Tab 3", layout_3,  key="tab3"),
                         sg.Tab("Tab 4", layout_4,  key="tab4")]], 
                         enable_events=True)

If you print out the values object and "Tab 1" is clicked, then you will see its key, "tab1" in this case, inside the values object. Same applies for the other tabs if they are clicked.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM