from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from time import strftime
kv = """
<Home@Screen>:
BoxLayout:
orientation: 'vertical'
MyClock:
font_size: 50
text: self.current_time
Button:
background_color: (128,0,0,.6)
background_down: '(0, 0, .3, 1)'
text: 'Set Alarm'
on_press: app.root.ids.sm.current = 'alarm_page'
<AlarmPage@Screen>:
GridLayout:
rows: 4
Button:
text: 'Alarm 1'
on_release: app.root.ids.sm.current = 'edit1_page'
Button:
text: 'Alarm 2'
Button:
text: 'Put alarms on this page'
on_release: app.root.ids.sm.current = 'home_page'
<Alarm1Edit@Screen>:
SetAlarm:
cols: 4
Label:
text: 'Set Time (00:00):'
TextInput:
id: alarmtime1
ToggleButton:
text: 'AM'
group:'am/pm'
ToggleButton:
text: 'PM'
group:'am/pm'
Button:
text: "Set Alarm"
on_press: set_button
BoxLayout:
ScreenManager:
id: sm
Home:
name: 'home_page'
AlarmPage:
name: 'alarm_page'
Alarm1Edit:
name: 'edit1_page'
"""
class MyClock(Label):
current_time = StringProperty(strftime("%I:%M:%S %p"))
def __init__(self, **kwargs):
Clock.schedule_interval(self.update_time, 1)
super().__init__(**kwargs)
def update_time(self, dt):
self.current_time = strftime("%I:%M:%S %p")
class SetAlarm(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def set_button(self, instance):
alarmtime = self.alarmtime1.text
class MainApp(App):
def build(self):
return Builder.load_string(kv)
if __name__ == "__main__":
MainApp().run()
Create a StringProperties in app for the data you want to share across screens.
Write the property in the place you are setting the alarm.
Assign it the text field (or state field for the toggle buttons)
Something like:
class MainApp342(App):
alarm_1_time = StringProperty()
alarm_1_am_state = StringProperty()
alarm_1_pm_state = StringProperty()
Access in kv:
app.alarm_1_time
Access in Python (outside of App) as:
app = App.get_running_app()
app.alarm_1_time
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/26c92d3a-09f8-43a5-a7d9-42f621847a7eo%40googlegroups.com.
text: alarm_1_time
ToggleButton:
text: 'AM'
group:'am/pm'
state: alarm_1_am_state
ToggleButton:
text: 'PM'
group:'am/pm'
state: alarm_1_pm_state
Button:
text: "Set Alarm"
on_press: set_button
BoxLayout:
ScreenManager:
id: sm
Home:
name: 'home_page'
AlarmPage:
name: 'alarm_page'
Alarm1Edit:
name: 'edit1_page'
"""
class MyClock(Label):
current_time = StringProperty(strftime("%I:%M:%S %p"))
def __init__(self, **kwargs):
Clock.schedule_interval(self.update_time, 1)
super().__init__(**kwargs)
def update_time(self, dt):
self.current_time = strftime("%I:%M:%S %p")
class SetAlarm(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def set_button(self, instance):
alarmtime = self.alarmtime1.text
alarm_1_time = StringProperty()
alarm_1_am_state = StringProperty()
alarm_1_pm_state = StringProperty()
class MainApp(App):
def build(self):
return Builder.load_string(kv)
app = App.get_running_app()
app.alarm_1_time
if __name__ == "__main__":
MainApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f072834.1c69fb81.29fb0.9ad4SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Close… more like this. We are creating the string propery as a member of app to make it easy to access anywhere in the program.
class SetAlarm(GridLayout):
def set_button(self, instance):
app = App.get_running_app()
app.alarm_1_time = self.alarmtime1.text
class MainApp(App):
alarm_1_time = StringProperty() #
alarm_1_am_state = StringProperty()
alarm_1_pm_state = StringProperty()
def build(self):
return Builder.load_string(kv)
…. And anywhere in kv as: app.alarm_1_time
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV441hibF6EbX%2BseL-%3Dx25GWgrviZFodekwVjxLKNzY0%3DZw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f078770.1c69fb81.16d0f.54b1SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here is a working version. I changed a number of things in the code, given what you were trying to achieve. Notice I created on AlarmEdit screen. I put the properties in the screen. The screen is instanced twice to support your 2 alarms.
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen
from time import strftime
kv =
"""
<Home@Screen>:
BoxLayout:
orientation: 'vertical'
MyClock:
font_size: 50
text: self.current_time
Button:
background_color: (128,0,0,.6)
background_down: '(0, 0, .3, 1)'
text: 'Set Alarm'
on_press: root.manager.current = 'alarm_page'
<AlarmPage@Screen>:
GridLayout:
rows: 4
Button:
text: 'Alarm 1'
on_release: root.manager.current = 'edit1_page'
Button:
text: 'Alarm 2'
on_release: root.manager.current = 'edit2_page'
Button:
text: 'Put alarms on this page'
on_release: root.manager.current = 'home_page'
<AlarmEdit>:
GridLayout:
cols: 4
Label:
text: 'Set Time (00:00):'
TextInput:
id: alarmtime
text: root.alarm_time
ToggleButton:
text: 'AM'
group:'am/pm'
state: root.alarm_am_state
on_state: root.alarm_am_state = self.state
ToggleButton:
text: 'PM'
group:'am/pm'
state: root.alarm_pm_state
on_state: root.alarm_pm_state = self.state
Button:
text: "Set Alarm"
on_press:
root.set_alarm()
root.manager.current = 'home_page'
BoxLayout:
ScreenManager:
id: sm
Home:
name: 'home_page'
AlarmPage:
name: 'alarm_page'
AlarmEdit:
name: 'edit1_page'
AlarmEdit:
name: 'edit2_page'
"""
class MyClock(Label):
current_time = StringProperty(strftime("%I:%M:%S %p"))
def __init__(self, **kwargs):
Clock.schedule_interval(self.update_time, 1)
super().__init__(**kwargs)
def update_time(self, dt):
self.current_time = strftime("%I:%M:%S %p")
class AlarmEdit(Screen): # Moved to the screen
alarm_time = StringProperty()
alarm_am_state = StringProperty('down') # set default values
alarm_pm_state = StringProperty('normal')
def set_alarm(self):
am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
self.alarm_time = self.ids.alarmtime.text + ' ' + am_pm
print(f'Alarm time set: {self.alarm_time}')
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV46fGqrDS0zDL_M8rJv_BRK3rF5RO4%3DvQaHPS2cFNHmZiA%40mail.gmail.com.
am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
Like that bracketed code and why specifically the am_state.
Thank you buddy.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f0de575.1c69fb81.87abb.9a53SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
That line is used to generate the AM or PM at the end of the time.
Breaking it down:
self.alarm_am_state is set to the value of the toggle button state in the kv code.
ToggleButton:
text: 'AM'
group:'am/pm'
state: root.alarm_am_state
on_state: root.alarm_am_state = self.state
This value will be ‘down’ if it is AM, and ‘normal’ if the time is PM. This is how state works in the ToggleButton.
This is a dictionary {'down': 'AM', 'normal': 'PM'}
So:
am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
This code uses self.alarm_am_state to index either ‘AM’ or ‘PM’.
That one line is equivalent to:
If self.alarm_am_state == ‘down’
am_pm = ‘AM’
else:
am_pm = ‘PM’
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV45b9pYiMTyi0jPGu08xjEWZnJ4WVauG%2B1Pw8Jqx9jQf2Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f110eff.1c69fb81.e10f7.6b58SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
If you want to have a toggle button that has AM on it and a checkbox on it. Open an image editor and create 2 images.
One with the AM in the button down color and the checkbox selected, The other with AM the button normal color and the checkbox unselected. And then another 2 images for PM. Then create MyButton(ToggleBehavior, Image)
The images in the kivy default themes are located here: https://github.com/kivy/kivy/tree/master/kivy/tools/theming/defaulttheme
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV46AQqDS6daxPdKvY51o1cKtUWQXqhjBQiZ1DU_NnP-G0A%40mail.gmail.com.
<AlarmEdit>:
GridLayout:
cols: 4
Label:
text: 'Set Time (00:00):'
TextInput:
id: alarmtime
text: root.alarm_time
ToggleButton:
text: 'AM'
group:'am/pm'
state: root.alarm_am_state
on_state: root.alarm_am_state = self.state
ToggleButton:
text: 'PM'
group:'am/pm'
state: root.alarm_pm_state
on_state: root.alarm_pm_state = self.state
Button:
text: "Set Alarm"
on_press:
root.set_alarm()
root.manager.current = 'home_page'
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f18a04c.1c69fb81.aadd6.1b05SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To have the bottom button take the full width of the screen, you want it outside of the GridLayout. Your could do this by nesting the GridLayout in a BoxLayout. Something like:
<AlarmEdit>:
BoxLayout:
orientation: 'vertical'
GridLayout:
cols: 4
Label:
text: 'Set Time (00:00):'
TextInput:
id: alarmtime
text: root.alarm_time
ToggleButton:
text: 'AM'
group:'am/pm'
state: root.alarm_am_state
on_state: root.alarm_am_state = self.state
ToggleButton:
text: 'PM'
group:'am/pm'
state: root.alarm_pm_state
on_state: root.alarm_pm_state = self.state
Button:
size_hint_y: None
height: 48
text: "Set Alarm"
on_press:
root.set_alarm()
root.manager.current = 'home_page'
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV442%2B%2Bhpa-gf%3DS2og%2Bu9wzn5piWuCAL31b9eTH%2BFg%3D7ayQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f19df7e.1c69fb81.41b67.86e5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here are a few changes:
To remove the trailing AM/PM the set_alarm method has been simplified, so it no longer appends the AM/PM to the time.
I have also change the way you were declaring the string properties. Kivy properties get declared like class properties and used as instance properties.
I also put the code that was in your alarm edit class, into a __init__ method. You need a self pointer to properly address the string properties.
You may want to change your alarm_ properties to be a list properties and always load/store your 2 alarms to the file. The way your code works now, the last alarm set will be the alarm written. You are overwriting what exists in the file.
# Moved to the screen
alarm_time = StringProperty()
alarm_am_state = StringProperty('down') # set default values
alarm_pm_state = StringProperty('normal')
def __init__(self, **kwargs):
super().__init__(**kwargs)
if os.path.isfile("alarm1_details.txt"):
with open("alarm1_details.txt", "r") as f:
d = f.read().split(",")
self.alarm_time = d[0]
self.alarm_am_state = d[1] # set default values
self.alarm_pm_state = d[2]
def set_alarm(self):
self.alarm_time = self.ids.alarmtime.text
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV44DB7ZwJnD%2BePqn%2Bqze_cOVpqUg49c92MjAK08GWFa9tA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f1cadac.1c69fb81.4b4c0.3fd8SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
The code works for me without error.
Did you change something? Perhaps you need to delete the text file created with the previous version.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV44KQBZ2fK6wKxk96R0OPPhrYfc_eDe28JrJmHS9eF%3DeDQ%40mail.gmail.com.