5

我是使用 Kivy 进行 Android 开发的新手。我创建了一个选项卡结构,如下所示:

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()

我想将登录小部件添加到默认选项卡。登录小部件的代码是:

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() 

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" 

如何将此代码添加到默认选项卡中?

4

2 回答 2

4

您可以使用以下 2 个属性:default_tab_textdefault_tab_content.

因此,假设您的登录代码如下(您的无效):

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

假设该代码与 main.py(具有 的代码)位于同一文件夹中TabbedPanelApp。然后

1)导入LoginView(我把它大写,因为它是一个类)

from login import LoginView

2)修改build方法TabbedPanelApp如下

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

您还可以TabbedPanelHeader使用该default_tab_cls属性修改整体,甚至可以使用该属性停用默认选项卡do_default_tab。所以,你有很多选择。看看文档

于 2013-06-16T04:09:08.803 回答
2

假设 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()

但是请注意,类应该用大写字母书写。这不是强制性的,但它是一个很好的做法。类似的名称loginView用于其他语言中的变量。

于 2013-06-16T09:16:13.700 回答