简体   繁体   English

简单的kivy标签示例

[英]Simple kivy tab example

I am new to Android development using Kivy. 我是使用Kivy进行Android开发的新手。 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 : 和kv文件是:

#: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? 如何将此代码添加到默认选项卡中?

You can use the following 2 properties: default_tab_text and default_tab_content . 您可以使用以下2个属性: default_tab_textdefault_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 ). 假设代码与main.py(具有TabbedPanelApp代码)位于同一文件夹中。 Then 然后

1) Import the LoginView (I put it upper-case because it is a class) 1)导入LoginView(我把它放在大写,因为它是一个类)

from login import LoginView

2) Modify the build method of TabbedPanelApp as follows 2)修改TabbedPanelAppbuild方法,如下所示

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. 您还可以使用default_tab_cls属性修改整个TabbedPanelHeader ,甚至可以使用do_default_tab属性停用默认选项卡。 So, you have many options. 所以,你有很多选择。 Just take a look to the doc 只需看看文档

Assuming the name of the python file is loginview.py and it is in the same folder of the main.py , adding the following to your code should fix your problems (it works on my computer already): 假设Python文件的名称是loginview.py ,它是在同一文件夹main.py ,添加以下到您的代码应该可以解决你的问题(它的作品在我的电脑上的话):

from loginview import loginView

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

      tb_panel.default_tab_text = "Login Tab"
      tb_panel.default_tab_content = loginView()

But notice that classes should be written in uppercase. 但请注意,类应该用大写字母书写。 This is not mandatory but it is a good practice. 这不是强制性的,但这是一种很好的做法。 Names like loginView are meant for variables in other languages. loginView类的名称适用于其他语言的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM