Kivy python create a tab bar

575 views
Skip to first unread message

huit...@gmail.com

unread,
Dec 23, 2017, 4:35:09 AM12/23/17
to Kivy users support

I started with kivy, I have generally understood the main concepts (it's quite similar to tkinter)

but there is something that I can not do, I would like to create a tab bar at the top of my window (as there is in the web browser)

The only tutorial I find does not look like my code: https://kivy.org/docs/api-kivy.uix.tabbedpanel.html

my code:


from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
class MyApp(App):
# layout
    def build(self):
        layout = BoxLayout(orientation='vertical')
        # use a (r, g, b, a) tuple
        blue = (0, 0, 1.5, 2.5)
        red = (2.5, 0, 0, 1.5)

        btn =  Button(text='Touch me!', background_color=blue, font_size=120)
        btn.bind(on_press=self.callback)
        btn2 =  Button(text='btn2', background_color=red, font_size=120)
        layout.add_widget(btn)
        layout.add_widget(btn2)

        self.label = Label(text="------------", font_size='50sp')
        layout.add_widget(self.label)

        btn1 = Button(text="OK")
        btn1.bind(on_press=self.buttonClicked)
        layout.add_widget(btn1)
        self.lbl1 = Label(text="test")
        layout.add_widget(self.lbl1)
        self.txt1 = TextInput(text='', multiline=False)
        layout.add_widget(self.txt1)
        return layout

    def callback(self, event):
        print("button touched")  # test
        self.label.text = "button touched"

# button click function
    def buttonClicked(self,btn):
        self.lbl1.text = "You wrote " + self.txt1.text

# run app
if __name__ == "__main__":
    MyApp().run()


I try to add tabpannel but i have two question :

1) how can i change name of tab ? and remove "defaulttab"

2) why my widget not in my tab ?

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelItem

class MyApp(App):
# layout
    def build(self):
        layout = BoxLayout(orientation='vertical')
        # use a (r, g, b, a) tuple
        blue = (0, 0, 1.5, 2.5)
        red = (2.5, 0, 0, 1.5)


        tab=TabbedPanel()
        tabitem=TabbedPanelItem()
        tab.add_widget(tabitem)
        layout.add_widget(tab)

        btn =  Button(text='Touch me!', background_color=blue, font_size=120)
        btn.bind(on_press=self.callback)
        btn2 =  Button(text='btn2', background_color=red, font_size=120)
        tabitem.add_widget(btn)
        tabitem.add_widget(btn2)

        self.label = Label(text="------------", font_size='50sp')
        tabitem.add_widget(self.label)

        btn1 = Button(text="OK")
        btn1.bind(on_press=self.buttonClicked)
        tabitem.add_widget(btn1)
        self.lbl1 = Label(text="test")
        tabitem.add_widget(self.lbl1)
        self.txt1 = TextInput(text='', multiline=False)
        tabitem.add_widget(self.txt1)

        return layout

    def callback(self, event):
        print("button touched")  # test
        self.label.text = "button touched"

# button click function
    def buttonClicked(self,btn):
        self.lbl1.text = "You wrote " + self.txt1.text

# run app
if __name__ == "__main__":
    MyApp().run()



Dipanshu Juneja

unread,
Dec 23, 2017, 10:52:56 AM12/23/17
to Kivy users support
You have to set this to make default tab go away do_default_tab: False 

huit...@gmail.com

unread,
Dec 23, 2017, 12:52:49 PM12/23/17
to Kivy users support
Yes but i don't want use kt text, how for example can i transform do_default_tab: False
to my code ?

Dipanshu Juneja

unread,
Dec 25, 2017, 8:45:42 PM12/25/17
to Kivy users support
tab = TabbedPanel()
tab
.do_default_tab = False

should work

On Saturday, December 23, 2017 at 3:05:09 PM UTC+5:30, huit...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages