Kivy使用篇13

Kivy使用篇之记事本小程序os.path中exists join @property 的使用Property使用ScreenManager Screen SlideTransition使用RecycleView的使用根据kivy示例程序,编写一个可以运行在Android的记事本小程序。可变的文本输入框当双击时,文本框获取焦点,且可输入内容。失去焦点时,采用标签显示输入的文本。...
摘要由CSDN通过智能技术生成

Kivy使用篇之记事本小程序

  1. os.pathexists join @property 的使用
  2. Property使用
  3. ScreenManager Screen SlideTransition使用
  4. RecycleView的使用

根据kivy示例程序,编写一个可以运行在Android的记事本小程序。
可变的文本输入框
当双击时,文本框获取焦点,且可输入内容。失去焦点时,采用标签显示输入的文本。
源码如下:

class MutableTextInput(FloatLayout):
    """可变文本输入框"""
    text = StringProperty()
    multiline = BooleanProperty(True)

    def __init__(self, **kwargs):
        """初始化"""
        super(MutableTextInput, self).__init__(**kwargs)
        Clock.schedule_once(self.prepare, 0)

    def prepare(self, *args):
        """设置显示和输入"""
        self.w_textinput = self.ids.w_textinput.__self__
        self.w_label = self.ids.w_label.__self__
        self.view()

    def on_touch_down(self, touch):
        """双击标签编辑"""
        if self.collide_point(*touch.pos) and touch.is_double_tap:
            self.edit()
        return super(MutableTextInput, self).on_touch_down(touch)

    def edit(self):
        """编辑内容"""
        self.clear_widgets()
        self.add_widget(self.w_textinput)
        self.w_textinput.focus = True

    def view(self):
        self.clear_widgets()
        if not self.text:
            self.w_label.text = "Double tap/click to edit"
        self.add_widget(self.w_label)

    def check_focus_and_view(self, textinput):
        """当输入框失去焦点时显示内容"""
        if not textinput.focus:
            self.text = textinput.text
            self.view()

初始时,设置输入框和显示框。

	Clock.schedule_once(self.prepare, 0)

    def prepare(self, *args):
        """设置显示和输入"""
        self.w_textinput = self.ids.w_textinput.__self__
        self.w_label = self.ids.w_label.__self__
        self.view()

def on_touch_down(self, touch):
当双击控件时,进入编辑状态。self.add_widget(self.w_textinput)显示输入框
def check_fo

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值