Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pylint:disable=W0613
- # -*-coding:utf-8-*-
- import kivy
- from kivy.app import App
- from kivy.uix.floatlayout import FloatLayout
- from kivy.uix.button import Button
- from kivy.uix.label import Label
- from kivy.uix.textinput import TextInput#引入输入框
- kivy.resources.resource_add_path('font/')
- font = kivy.resources.resource_find('DroidSansFallback.ttf')
- class RootWidget(FloatLayout):
- def __init__(self, **kwargs):
- super(RootWidget, self).__init__(**kwargs)
- self.button_init =Button(
- text='计算',
- font_name = font,
- size_hint=(None, None),
- size=(200, 144),
- pos_hint={'center_x': .2, 'center_y': .9}
- ) # 按钮控件
- self.label = Label(
- text='',
- font_name = font,
- size_hint=(None, None),
- size=(200, 144),
- pos_hint={'center_x': .5, 'center_y': .9}
- ) # 标签控件,用于显示按钮状态
- self.textinput = TextInput(
- text='',
- font_name = font,
- size_hint=(None, None),
- size=(200, 144),
- pos_hint={'center_x': .3, 'center_y': .6}
- ) # 文本框控件,用于输入字符状态
- self.add_widget(self.button_init) # 添加控件至布局
- self.add_widget(self.label)
- self.add_widget(self.textinput)
- self.button_init.bind(state=self.state_changed) # 绑定按钮状态改变事件
- def state_changed(self, sender, state):
- """处理按钮状态改变事件"""
- c=self.textinput.text
- str=c
- def is_number(s):
- try:
- float(s)
- return True
- except ValueError:
- pass
- try:
- import unicodedata
- unicodedata.numeric(s)
- return True
- except (TypeError, ValueError):
- pass
- return False
- if is_number(str):
- a=float(str)
- b="%.3f" % (a+2)
- self.label.text=b
- else:
- self.textinput.text=''
- self.label.text='输入数字错误'
- class MainApp(App):
- def build(self):
- return RootWidget()
- if __name__ == '__main__':
- MainApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement