Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple kivy tab example

I am new to Android development using Kivy. I have created a tab structure like below:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelHeader
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image

class TabbedPanelApp(App):
  def build(self):
      tb_panel= TabbedPanel()

      # Create text tab          
      th_text_head = TabbedPanelHeader(text='Text tab')
      th_text_head.content= Label(text='This is my text content')

      # Create image tab
      th_img_head= TabbedPanelHeader(text='Image tab')
      th_img_head.content= Image(source='sample.jpg',pos=(400, 100), size=(400, 400))

      # Create button tab
      th_btn_head = TabbedPanelHeader(text='Button tab')
      th_btn_head.content= Button(text='This is my button',font_size=20)

      tb_panel.add_widget(th_text_head)
      tb_panel.add_widget(th_img_head)
      tb_panel.add_widget(th_btn_head)          

      return tb_panel

if __name__ == '__main__':
    TabbedPanelApp().run()

I want to add the login widget to the default tab. The code for login widget is:

import kivy 
kivy.require('1.0.5')

from kivy.uix.gridlayout import GridLayout 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.widget import Widget 
from kivy.properties import ObjectProperty, StringProperty 

class loginView(Widget): 
    status=ObjectProperty(None) 
    def validate(self,username,password): 
        print "user - ", username
        print "pwd - ", password
        if username == password: 
            print "in if - ", username,password     
            self.status.text="Login sucess" 
        #mainClass().run() 
        else: 
            self.status.text="Login failed" 

class afterLogin(Widget): 
    def dumb(self): 
        l = BoxLayout(cols="2") 
        btn = Button(text="ad") 
        l.add_widget(btn) 
        print "flag" 

class mainClass(App): 
    def build(self): 
        return loginView()  

if __name__ == '__main__': 
    mainClass().run() 

and the kv file is :

#:kivy 1.0.5 

<loginView>: 
    status:result 
    Label: 
         text:"Contacts Manager" 
         pos:600,600 
         font_size:40 


    Label: 
         text:"Username" 
         pos:450,400 

    Label: 
         text:"Password" 
         pos:450,300 

    TextInput: 
         multiline:False 
         pos:600,425 
         size:200,45 
         font_size:20 
         id:username 

    TextInput: 
         multiline:False 
         pos:600,325 
         password:True 
         size:200,45 
         font_size:20 
         id:password 
    Button: 
         text:"Login" 
         size:100,50 
         pos:600,250 
         on_press:root.validate(username.text,password.text) 
    Label: 
         text:"" 
         pos:600,100 
         id:result 
<afterLogin>: 
    Label: 
         text:"Welcome" 

How can I add this code into the default tab?

like image 262
sam Avatar asked Jun 15 '13 04:06

sam


People also ask

How do I add tabs to Kivy?

To create a tab, you must create a new class that inherits from the MDTabsBase class and the Kivy container, in which you will create content for the tab. class Tab(MDFloatLayout, MDTabsBase): '''Class implementing content for a tab. '''

Which language is used in Kivy?

The KV language, sometimes called kvlang or the kivy language, allows you to create your widget tree in a declarative way and to bind widget properties to each other or to callbacks in a natural manner. It allows for very fast prototypes and agile changes to your UI.

What is a widget in Kivy?

A Widget is the base building block of GUI interfaces in Kivy. It provides a Canvas that can be used to draw on screen. It receives events and reacts to them.

Which among the following class is used for defining layout in Kivy?

PageLayout: Used to create simple multi-page layouts, in a way that allows easy flipping from one page to another using borders.


1 Answers

You can use the following 2 properties: default_tab_text and default_tab_content.

So, assuming your login code is the following one (yours doesn't work):

import kivy 
kivy.require('1.0.5')

from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label 
from kivy.lang import Builder

Builder.load_string("""
<Loginview>:
    cols:2
    padding: 200
    Label: 
        text: "username"
    TextInput:
    Label: 
        text: "password"
    TextInput:
""")

#Name of classes are always uppercase
class LoginView(GridLayout): 
    pass

Assuming that code is in the same folder as the main.py (the code that has the TabbedPanelApp). Then

1) Import the LoginView (I put it upper-case because it is a class)

from login import LoginView

2) Modify the build method of TabbedPanelApp as follows

class TabbedPanelApp(App):
    def build(self):
        tb_panel= TabbedPanel()
        tb_panel.default_tab_text = "Login Tab"
        tb_panel.default_tab_content = LoginView()

You can also modify the whole TabbedPanelHeader with the default_tab_cls property, or even deactivate the default tab with the do_default_tab property. So, you have many options. Just take a look to the doc

like image 58
toto_tico Avatar answered Oct 26 '22 23:10

toto_tico