Search code examples
pythonuser-interfacetabsfiremonkey

How do I set an active tab for TabControl in a Python FMX GUI App?


I've made a Form with a TabControl and Four TabItem tabs on the TabControl. By default, the first tab is always the active tab:

Python GUI App with four tabs

self.TabControl1 = TabControl(self)
self.TabControl1.Parent = self
self.TabControl1.Align = "Client"

self.TabItem1 = TabItem(self.TabControl1)
self.TabItem1.Text = "My first tab"
self.TabItem1.Parent = self.TabControl1

self.TabItem2 = TabItem(self.TabControl1)
self.TabItem2.Text = "My second tab"
self.TabItem2.Parent = self.TabControl1

self.TabItem3 = TabItem(self.TabControl1)
self.TabItem3.Text = "My third tab"
self.TabItem3.Parent = self.TabControl1

self.TabItem4 = TabItem(self.TabControl1)
self.TabItem4.Text = "My fourth tab"
self.TabItem4.Parent = self.TabControl1

How do I set a different tab as the default tab? Like, let's say I want to set the third tab as active via code.


Solution

  • The TabControl component has an ActiveTab property which you can set equal to your TabItem to make it active.

    So if you want to make your third tab as the default active tab, then add the following line of code at the end of your third tab creation code:

    self.TabControl1.ActiveTab = self.TabItem3
    

    Full TabItem creation code:

    self.TabItem3 = TabItem(self.TabControl1)
    self.TabItem3.Text = "My third tab"
    self.TabItem3.Parent = self.TabControl1
    self.TabControl1.ActiveTab = self.TabItem3 # Sets it as ActiveTab
    

    Now if you open the app, your third tab will be set as active by default: Python GUI app with four tabs