kivy学习笔记-转到新界面之二

上篇博文中的转换界面方法,当切换到新界面后,无法返回原界面,本文通过改进,实现了界面任意切换。

代码如下:

<MyForm>: #界面1代码
    orientation:"vertical"
    text_input:text_box 

    BoxLayout: 
        height:"40dp" 
        size_hint_y:None 

        TextInput:
            id:text_box
            size_hint_x:50

        Button:
            text:"Button A"
            size_hint_x:20
            on_press:root.buttona_act() 

        Button:
            text:"Button B"
            size_hint_x:20
            on_press:root.chg_widget()

        Button:
            text:"Button C"
            size_hint_x:10
            on_press:root.chg_widget2()

<MyForm2>:#界面2代码
       Button:
        text:"Button D"
        on_press:root.chg_widget3()
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.factory import Factory

class MyForm(BoxLayout): 
    text_input = ObjectProperty()
    def buttona_act(self):
        print(self.text_input.text) 

    def chg_widget(self):
        self.clear_widgets()
        self.add_widget(Label(text='location')) 

    def chg_widget2(self):  #进入子界面调用的方法
        self.clear_widgets()
        cur_wdgt = Factory.MyForm2()
        self.add_widget(cur_wdgt) 

class MyForm2(BoxLayout):

    def chg_widget3(self):  #返回主界面调用的方法
        self.clear_widgets()
        cur_wdgt = Factory.MyForm()
        self.add_widget(cur_wdgt)


class MychgApp(App):
    def build(self):
        return MyForm() #显示主界面

MychgApp().run()
运行结果截图这里就不放了。

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值