Search code examples
pythonuser-interfacetabsfiremonkey

How do I create a tabbed control in a Python FMX GUI App?


I'm making a Python FMX GUI App and I basically want three tabs on it like this: Delphi App with tabs

I tried doing this:

self.TabControl1 = TabControl(self)
self.TabControl1.Parent = self
self.TabControl1.Align = "Client"
self.TabControl1.Margins.Top = 50
self.TabControl1.Margins.Bottom = 50
self.TabControl1.Margins.Left = 50
self.TabControl1.Margins.Right = 50

self.TabControl1.Tabs.Add("Tab1")
self.TabControl1.Tabs.Add("Tab2")
self.TabControl1.Tabs.Add("Tab3")

The self.TabControl1.Tabs.Add() fails and errors with AttributeError: Error in getting property "Tabs". Error: Unknown attribute.

What is the correct way to give tabs to the TabControl component?


Solution

  • You can use the following code to create a tab:

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

    Here's the full code to create 3 tabs on a Form similar to the one you posted:

    from delphifmx import *
    
    class frmMain(Form):
        def __init__(self, owner):
            self.Caption = 'My Application with three tabs'
            self.Width = 1000
            self.Height = 500
    
            self.TabControl1 = TabControl(self)
            self.TabControl1.Parent = self
            self.TabControl1.Align = "Client"
            self.TabControl1.Margins.Top = 50
            self.TabControl1.Margins.Bottom = 50
            self.TabControl1.Margins.Left = 50
            self.TabControl1.Margins.Right = 50
    
            self.TabItem1 = TabItem(self.TabControl1)
            self.TabItem1.Text = "Your first tab"
            self.TabItem1.Parent = self.TabControl1
    
            self.TabItem2 = TabItem(self.TabControl1)
            self.TabItem2.Text = "Your second tab"
            self.TabItem2.Parent = self.TabControl1
    
            self.TabItem3 = TabItem(self.TabControl1)
            self.TabItem3.Text = "Your third tab"
            self.TabItem3.Parent = self.TabControl1
    
    
    def main():
        Application.Initialize()
        Application.Title = "My Application"
        Application.MainForm = frmMain(Application)
        Application.MainForm.Show()
        Application.Run()
        Application.MainForm.Destroy()
    
    if __name__ == '__main__':
        main()
    

    Python GUI App with tabs