主文件
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
from kivy.core.window import Window
Config.set('kivy', 'keybord_mode', 'systemanddock')
Window.size = (360, 640)
class Container(GridLayout):
def convert(self):
try:
number = int(self.text_input.text)
except Exception:
number = 0
self.label_hours.text = str(number * 24)
self.label_minutes.text = str(number * 24 * 60)
self.label_seconds.text = str(number * 24 * 60 * 60)
self.label_m_seconds.text = str(number * 24 * 60 * 60 * 60)
self.label_weeks.text = str('%.2f' % round(number / 7, 2))
def change_something(self):
self.label_text.text = self.text_input.text
class DuckyApp(App):
def build(self):
return Container()
if __name__ == '__main__':
DuckyApp().run()
鸭子.kv
<MyOwnLabel@Label>
font_size: '25sp'
haling: 'left'
valing: 'middle'
text_size: self.size
<Container>:
rows: 3
text_input: text_input
label_hours: label_hours
label_minutes: label_minutes
label_seconds: label_seconds
label_m_seconds: label_m_seconds
label_weeks: label_weeks
AnchorLayout:
anchor_y: 'top'
id: text_input
size_hint: 1, 0.15
input_filter: 'int'
input_type: 'number'
multiline: False
TextInput:
text: ''
font_size: '45sp'
GridLayout:
cols: 2
padding: [40, 0, 0, 0]
BoxLayout:
orientation: 'vertical'
MyOwnLabel:
text: 'Hours'
MyOwnLabel:
text: 'Minutes'
MyOwnLabel:
text: 'Seconds'
MyOwnLabel:
text: 'Milliseconds'
MyOwnLabel:
text: 'Weeks'
BoxLayout:
orientation: 'vertical'
size_hint: 0.5, 1
MyOwnLabel:
text: '0'
id: label_hours
MyOwnLabel:
text: '0'
id: label_minutes
MyOwnLabel:
text: '0'
id: label_seconds
MyOwnLabel:
text: '0'
id: label_m_seconds
MyOwnLabel:
text: '0'
id: label_weeks
BoxLayout:
size_hint: 0.9, 0.15
padding: [30, 20, 30, 20]
Button:
text: "Let's go!"
on_release:
root.convert()