In this video I’ll show you how to use the KivyMD Time Picker for Kivy and Python.
In this video we’ll look at the KivyMD TimePicker that allows you to pull up a clock and pick a time, and then return that time to the screen. We can set a default time as well. We’ll look at all those options in this video.
#kivy #codemy
Python Code: time.py
GitHub Code: time.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.picker import MDTimePicker
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "BlueGray"
return Builder.load_file('time.kv')
# Get Time
def get_time(self, instance, time):
self.root.ids.time_label.text = str(time)
# Cancel
def on_cancel(self, instance, time):
self.root.ids.time_label.text = "You Clicked Cancel!"
def show_time_picker(self):
from datetime import datetime
# Define default time
default_time = datetime.strptime("4:20:00", '%H:%M:%S').time()
time_dialog = MDTimePicker()
# Set default Time
time_dialog.set_time(default_time)
time_dialog.bind(on_cancel=self.on_cancel, time=self.get_time)
time_dialog.open()
MainApp().run()
Kivy Design Code: time.kv
GitHub Code: time.kv
MDFloatLayout:
MDRaisedButton:
text: "Open Time Picker"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_time_picker()
MDLabel:
id: time_label
text: "Some Stuff!"
pos_hint: {'center_x': .95, 'center_y': .4}

Add comment