Creating Tabs and Tab Views with PySimpleGUI

Posted by


PySimpleGUI is a simple yet powerful Python GUI framework that makes it easy to create graphical user interfaces for your Python applications. In this tutorial, we’ll be focusing on how to use tabs and tab views in PySimpleGUI.

Tabs are a great way to organize your GUI applications and provide a clean and intuitive user experience. Tab views, on the other hand, allow you to switch between different views within a tab, similar to how tabs work in a web browser.

First, you’ll need to install PySimpleGUI if you haven’t already. You can do this using pip:

pip install PySimpleGUI

Once you have PySimpleGUI installed, you can start creating your GUI application. Let’s start by creating a simple tabbed interface with two tabs:

import PySimpleGUI as sg

# Define the layout for the tabbed interface
tab1_layout = [[sg.Text('This is tab 1')]]
tab2_layout = [[sg.Text('This is tab 2')]]

layout = [[sg.TabGroup([[sg.Tab('Tab 1', tab1_layout), sg.Tab('Tab 2', tab2_layout)]])],
          [sg.Button('Exit')]]

# Create the window
window = sg.Window('Tabbed Interface', layout)

# Event loop
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break

window.close()

In this example, we define two tabs (tab1_layout and tab2_layout) with some simple content, and then use sg.TabGroup to group these tabs together. We then create a window with the tabbed layout and run an event loop to handle user input.

Now let’s extend this example by adding tab views to one of the tabs. This will allow us to switch between different views within a tab:

import PySimpleGUI as sg

# Define the layouts for the tab views
tab2_view1_layout = [[sg.Text('View 1')]]
tab2_view2_layout = [[sg.Text('View 2')]]

# Define the layout for tab 2 with tab views
tab2_layout = [[sg.Tab('View 1', tab2_view1_layout, key='view1'),
                sg.Tab('View 2', tab2_view2_layout, visible=False, key='view2')]]

layout = [[sg.TabGroup([[sg.Tab('Tab 1', [[sg.Text('This is tab 1')]]),
                         sg.Tab('Tab 2', tab2_layout)]])],
          [sg.Button('Toggle View'), sg.Button('Exit')]]

# Create the window
window = sg.Window('Tabbed Interface with Tab Views', layout)

# Event loop
while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == 'Toggle View':
        tab = window['view1'] if window['view1'].Visible else window['view2']
        tab.update(visible=not tab.Visible)

window.close()

In this example, we create two different tab views within the second tab. We then use the visible property to show and hide these tab views based on user input. When the "Toggle View" button is clicked, it toggles between showing tab2_view1_layout and tab2_view2_layout.

This is just a basic example to get you started with using tabs and tab views in PySimpleGUI. You can customize and extend this code to create more complex tabbed interfaces for your Python applications. Try experimenting with different layouts, widgets, and event handlers to create the perfect GUI for your needs.

0 0 votes
Article Rating
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@vladimirkostov8182
2 months ago

Hello, thank you for the video, very very useful and helpful! I have one question, can I for example from contact information window table, select a row and then edit it somehow? If you can make tutorial with this will be super! 🙂

@kurtauerbach5883
2 months ago

thank you this. This should be very useful. Ive been trying to put a table into a tab all day! thanks for this tutorial.

@laeciofarias3747
2 months ago

Hi;;; Can we Print the Table Contents?

@Wallee580
2 months ago

I guess I'm the first commenter, please get your bracket indentation right xD