简体   繁体   English

PySimpleGUI 当前选择的选项卡及其键

[英]PySimpleGUI currently selected tab and its key

While working on my project I came across a problem regarding tabs and tabgroup in PySimpleGUI.在处理我的项目时,我遇到了关于 PySimpleGUI 中的选项卡和选项卡组的问题。 Is there a function that returns currently selected tab?是否有返回当前选定选项卡的 function? 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.有很多选项卡我希望能够 select 每个选项卡(但不是同时)并返回当前选择的一个/活动的密钥。 I've tried .Get() and .Select() but seems like these don't do it or maybe I'm doing something wrong?我已经尝试过.Get().Select()但似乎这些都没有做到,或者我做错了什么? 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.我做了这样的事情: curr_sel_tab= tab_group.find_key_from_tab_name(tab_group.Get())但它返回选项卡的名称而不是键。

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.我想要做的是 select 在选项卡(1,2,3 或 4)中的一行并将其删除。 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.我能够弄清楚值 object 包含已单击的选项卡的键。 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.如果您打印出值 object 并单击“Tab 1”,那么您将在值 object 中看到其键,在本例中为“tab1”。 Same applies for the other tabs if they are clicked.如果单击其他选项卡,则同样适用。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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