Having trouble with implementing a timer in KivyDoes Python have a ternary conditional operator?Does Python have a string 'contains' substring method?Correct way to implement 2d tiled map on KivyHow do I have multiple windows in Kivy?Kivy with PythonKivy widgets behaving erraticallyPython/Kivy : add a calendar in kivyKivy - Timer Integration in FloatLayouti have some trouble with Kivy, omxplayer + screenmanager, imagebuttonImplementing VRAM using Kivy
The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?
Are Warlocks Arcane or Divine?
Resetting two CD4017 counters simultaneously, only one resets
Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?
How can I successfully establish a nationwide combat training program for a large country?
Should my PhD thesis be submitted under my legal name?
Why are on-board computers allowed to change controls without notifying the pilots?
My boss asked me to take a one-day class, then signs it up as a day off
Superhero words!
Stereotypical names
Indicating multiple different modes of speech (fantasy language or telepathy)
What is Sitecore Managed Cloud?
I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?
Can I Retrieve Email Addresses from BCC?
Simple recursive Sudoku solver
Partial sums of primes
Female=gender counterpart?
How did Monica know how to operate Carol's "designer"?
How do I repair my stair bannister?
Can a Bard use an arcane focus?
How to check participants in at events?
Is a naturally all "male" species possible?
Freedom of speech and where it applies
word describing multiple paths to the same abstract outcome
Having trouble with implementing a timer in Kivy
Does Python have a ternary conditional operator?Does Python have a string 'contains' substring method?Correct way to implement 2d tiled map on KivyHow do I have multiple windows in Kivy?Kivy with PythonKivy widgets behaving erraticallyPython/Kivy : add a calendar in kivyKivy - Timer Integration in FloatLayouti have some trouble with Kivy, omxplayer + screenmanager, imagebuttonImplementing VRAM using Kivy
I'm trying to create a kivy application.
The Application works as following:
- I send the amount of seconds I want my timer to last via MQTT over the topic 'tafelxuren' (x is a number between 1 and 8).
- After sending the amounts of seconds I want it to last, I send a second MQTT command to the topic 'tafelxstart' (x is a number between 1 and 8) with the command 'start'.
- After sending the start command, my timer starts running.

Under my timer I have 3 buttons, pause, resume and stop.
While starting and pausing the timer works using MQTT commands, I cannot seem to get my resume button to function properly, if there's 26 seconds left after clicking on the pause button, I want my timer to resume from that point. Instead, it starts again from the beginning
My main.py:
import os
if os.name == 'posix':
os.environ['KIVY_GL_BACKEND'] = 'gl'
import kivy, time,threading
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.clock import Clock
from kivy.config import Config
import paho.mqtt.client as mqtt
Config.set('graphics', 'fullscreen', 'auto')
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import config
# lege MQTT-client instantie
client = None
# De topics die gebruikt worden
ingesteldetijd = [config.timeTopics['tafel1'],config.timeTopics['tafel2'],config.timeTopics['tafel3'],config.timeTopics['tafel4'],config.timeTopics['tafel5'],config.timeTopics['tafel6'],config.timeTopics['tafel7'],config.timeTopics['tafel8']]
ingesteldetijdTopics = [config.tim1, config.tim2, config.tim3, config.tim4, config.tim5, config.tim6, config.tim7, config.tim8]
starttopics = [config.startTopics['tafel1'],config.startTopics['tafel2'],config.startTopics['tafel3'],config.startTopics['tafel4'],config.startTopics['tafel5'],config.startTopics['tafel6'],config.startTopics['tafel7'],config.startTopics['tafel8']]
def on_connect(client,userdata,flags,rc):
'''
This function gets triggered when MQTT is connected succesfully
'''
if(rc == 0):
print("[INFO ] [MQTT ] MQTT connected to broker "+config.settings['broker']+".")
client.subscribe('#')
##################################### ingestelde tijd subscriptions #####################################
for x in range(0,8):
client.subscribe(ingesteldetijd[x])
print('[INFO ] [MQTT ] Subscribed to '+ingesteldetijd[x])
##################################### ingestelde tijd subscriptions #####################################
##################################### start topic subscriptions #####################################
for x in range(0,8):
client.subscribe(starttopics[x])
print('[INFO ] [MQTT ] Subscribed to '+starttopics[x])
##################################### start topic subscriptions #####################################
else:
print("MQTT connection to broker "+config.settings['broker']+"failed.")
def on_message(client,userdata,msg):
'''
If there's a message received on one of the topics, the messages gets handled here.
'''
################################## tijd instellen topic ##################################
if msg.topic == 'tafel1uren':
config.tim1 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel2uren':
config.tim2 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel3uren':
config.tim3 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel4uren':
config.tim4 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel5uren':
config.tim5 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel6uren':
config.tim6 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel7uren':
config.tim7 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel8uren':
config.tim8 = int(msg.payload.decode('utf-8'))
################################## tijd instellen topic ##################################
if msg.topic == config.startTopics['tafel1']:
if msg.payload.decode('utf-8') == 'start':
config.tb1start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb1start = False
if msg.topic == config.startTopics['tafel2']:
if msg.payload.decode('utf-8') == 'start':
config.tb2start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb2start = False
if msg.topic == config.startTopics['tafel3']:
if msg.payload.decode('utf-8') == 'start':
config.tb3start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb3start = False
if msg.topic == config.startTopics['tafel4']:
if msg.payload.decode('utf-8') == 'start':
config.tb4start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb4start = False
if msg.topic == config.startTopics['tafel5']:
if msg.payload.decode('utf-8') == 'start':
config.tb5start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb5start = False
if msg.topic == config.startTopics['tafel6']:
if msg.payload.decode('utf-8') == 'start':
config.tb6start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb6start = False
if msg.topic == config.startTopics['tafel7']:
if msg.payload.decode('utf-8') == 'start':
config.tb7start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb7start = False
if msg.topic == config.startTopics['tafel8']:
if msg.payload.decode('utf-8') == 'start':
config.tb8start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb8start = False
class CrudeTimerGrid(GridLayout):
_python_access = ObjectProperty(None)
time = NumericProperty(0)
def __init__(self,**kwargs):
super(CrudeTimerGrid,self).__init__(**kwargs)
self.runningTimer = 0
Clock.schedule_interval(self.load_times,1)
Clock.schedule_interval(self.start,1)
def load_times(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
self.time = config.tim1
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
self.time = config.tim2
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
self.time = config.tim3
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
self.time = config.tim4
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
self.time = config.tim5
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
self.time = config.tim6
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
self.time = config.tim7
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
self.time = config.tim8
def start(self, *_):
tafelobjecten = self.parent.parent.ids
self.runningTimer = self.time
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
#self.time = config.tim1
if config.tb1start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel2':
#self.time = config.tim2
if config.tb2start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel3':
#self.time = config.tim3
if config.tb3start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel4':
#self.time = config.tim4
if config.tb4start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel5':
#self.time = config.tim5
if config.tb5start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel6':
#self.time = config.tim6
if config.tb6start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel7':
#self.time = config.tim7
if config.tb7start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel8':
#self.time = config.tim8
if config.tb8start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
def pause(self):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = False
Clock.unschedule(self.tick)
def resume(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = True
Clock.schedule_interval(self.tick,1) #pass
def stop(self, *_):
#TODO: implement stop button
pass
def tick(self, *_):
tafelobjecten = self.parent.parent.ids
if self.runningTimer > 0:
self.runningTimer -= 1
# publish de juiste tafel topic met de waarde van de restrerende tijd
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
client.publish(topic = str(list(tafelobjecten.keys())[x]), payload = str(self.runningTimer))
self.ids.Changelabel.text = str(time.strftime('%H:%M:%S',time.gmtime(self.runningTimer)))
else:
pass
class Main(GridLayout):
pass
class CrudeTimerApp(App):
pass
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(config.settings['username'], config.settings['password'])
client.connect(config.settings['broker'])
t = threading.Thread(target=client.loop_start())
t.daemon = True
t.start()
CrudeTimerApp().run()
My .kv file:
#:kivy 1.10.1
################################### Widget template ##########################################
<CrudeTimerGrid>:
_python_access: Changelabel
id: timer
rows: 4
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Restrerende tijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
id: Changelabel
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Pauzetijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: '00'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.2
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Button:
text: "Pauze"
on_press: timer.pause()
Button:
text: "Hervatten"
on_press: timer.resume()
Button:
text: "Stoppen"
#on_press: timer.reset()
Label:
text: ''
################################### Widget template ##########################################
<Main@Widget>:
rows: 2 # 2 rijen
cols: 4 # 4 colums
padding: 10
spacing: 10
################################### Tafel 1 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 1'
CrudeTimerGrid:
id: tafel1
################################### Tafel 1 ##########################################
################################### Tafel 2 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 2'
CrudeTimerGrid:
id: tafel2
################################### Tafel 2 ##########################################
################################### Tafel 3 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 3'
CrudeTimerGrid:
id: tafel3
################################### Tafel 3 ##########################################
################################### Tafel 4 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 4'
CrudeTimerGrid:
id: tafel4
################################### Tafel 4 ##########################################
################################### Tafel 5 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 5'
CrudeTimerGrid:
id: tafel5
################################### Tafel 5 ##########################################
################################### Tafel 6 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 6'
CrudeTimerGrid:
id: tafel6
################################### Tafel 6 ##########################################
################################### Tafel 7 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 7'
CrudeTimerGrid:
id: tafel7
################################### Tafel 7 ##########################################
################################### Tafel 8 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 8'
CrudeTimerGrid:
id: tafel8
################################### Tafel 8 ##########################################
Main:
my config.py:
# MQTT broker settings.
settings = dict(
broker = '172.16.24.128',
username = 'pi',
password = 'Piaservice123'
)
# start topics.
startTopics = dict(
tafel1 = 'tafel1start',
tafel2 = 'tafel2start',
tafel3 = 'tafel3start',
tafel4 = 'tafel4start',
tafel5 = 'tafel5start',
tafel6 = 'tafel6start',
tafel7 = 'tafel7start',
tafel8 = 'tafel8start'
)
# time in seconds topics.
timeTopics = dict(
tafel1 = 'tafel1uren',
tafel2 = 'tafel2uren',
tafel3 = 'tafel3uren',
tafel4 = 'tafel4uren',
tafel5 = 'tafel5uren',
tafel6 = 'tafel6uren',
tafel7 = 'tafel7uren',
tafel8 = 'tafel8uren'
)
# Currenttime topics.
currentTime = dict(
tafel1 = 'tafel1',
tafel2 = 'tafel2',
tafel3 = 'tafel3',
tafel4 = 'tafel4',
tafel5 = 'tafel5',
tafel6 = 'tafel6',
tafel7 = 'tafel7',
tafel8 = 'tafel8'
)
# Global timer vars
tim1 = 0
tim2 = 0
tim3 = 0
tim4 = 0
tim5 = 0
tim6 = 0
tim7 = 0
tim8 = 0
# startbooleans
tb1start = False
tb2start = False
tb3start = False
tb4start = False
tb5start = False
tb6start = False
tb7start = False
tb8start = False
The class where all the magic happens is the class CrudeTimerGrid().
How can I make it so my timer won't reset everytime I click on my Resume button?
Edit: managed to fix my problem!
I made an event(self.event = Clock.schedule_interval(self.tick,1)) and have 2 buttons to pause and resume.
I use the pause button (Clock.unschedule(self.event)) to unschedule the event and the resume button (Clock.schedule_once(self.event)) to reschedule my timer.
python kivy kivy-language
add a comment |
I'm trying to create a kivy application.
The Application works as following:
- I send the amount of seconds I want my timer to last via MQTT over the topic 'tafelxuren' (x is a number between 1 and 8).
- After sending the amounts of seconds I want it to last, I send a second MQTT command to the topic 'tafelxstart' (x is a number between 1 and 8) with the command 'start'.
- After sending the start command, my timer starts running.

Under my timer I have 3 buttons, pause, resume and stop.
While starting and pausing the timer works using MQTT commands, I cannot seem to get my resume button to function properly, if there's 26 seconds left after clicking on the pause button, I want my timer to resume from that point. Instead, it starts again from the beginning
My main.py:
import os
if os.name == 'posix':
os.environ['KIVY_GL_BACKEND'] = 'gl'
import kivy, time,threading
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.clock import Clock
from kivy.config import Config
import paho.mqtt.client as mqtt
Config.set('graphics', 'fullscreen', 'auto')
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import config
# lege MQTT-client instantie
client = None
# De topics die gebruikt worden
ingesteldetijd = [config.timeTopics['tafel1'],config.timeTopics['tafel2'],config.timeTopics['tafel3'],config.timeTopics['tafel4'],config.timeTopics['tafel5'],config.timeTopics['tafel6'],config.timeTopics['tafel7'],config.timeTopics['tafel8']]
ingesteldetijdTopics = [config.tim1, config.tim2, config.tim3, config.tim4, config.tim5, config.tim6, config.tim7, config.tim8]
starttopics = [config.startTopics['tafel1'],config.startTopics['tafel2'],config.startTopics['tafel3'],config.startTopics['tafel4'],config.startTopics['tafel5'],config.startTopics['tafel6'],config.startTopics['tafel7'],config.startTopics['tafel8']]
def on_connect(client,userdata,flags,rc):
'''
This function gets triggered when MQTT is connected succesfully
'''
if(rc == 0):
print("[INFO ] [MQTT ] MQTT connected to broker "+config.settings['broker']+".")
client.subscribe('#')
##################################### ingestelde tijd subscriptions #####################################
for x in range(0,8):
client.subscribe(ingesteldetijd[x])
print('[INFO ] [MQTT ] Subscribed to '+ingesteldetijd[x])
##################################### ingestelde tijd subscriptions #####################################
##################################### start topic subscriptions #####################################
for x in range(0,8):
client.subscribe(starttopics[x])
print('[INFO ] [MQTT ] Subscribed to '+starttopics[x])
##################################### start topic subscriptions #####################################
else:
print("MQTT connection to broker "+config.settings['broker']+"failed.")
def on_message(client,userdata,msg):
'''
If there's a message received on one of the topics, the messages gets handled here.
'''
################################## tijd instellen topic ##################################
if msg.topic == 'tafel1uren':
config.tim1 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel2uren':
config.tim2 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel3uren':
config.tim3 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel4uren':
config.tim4 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel5uren':
config.tim5 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel6uren':
config.tim6 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel7uren':
config.tim7 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel8uren':
config.tim8 = int(msg.payload.decode('utf-8'))
################################## tijd instellen topic ##################################
if msg.topic == config.startTopics['tafel1']:
if msg.payload.decode('utf-8') == 'start':
config.tb1start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb1start = False
if msg.topic == config.startTopics['tafel2']:
if msg.payload.decode('utf-8') == 'start':
config.tb2start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb2start = False
if msg.topic == config.startTopics['tafel3']:
if msg.payload.decode('utf-8') == 'start':
config.tb3start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb3start = False
if msg.topic == config.startTopics['tafel4']:
if msg.payload.decode('utf-8') == 'start':
config.tb4start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb4start = False
if msg.topic == config.startTopics['tafel5']:
if msg.payload.decode('utf-8') == 'start':
config.tb5start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb5start = False
if msg.topic == config.startTopics['tafel6']:
if msg.payload.decode('utf-8') == 'start':
config.tb6start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb6start = False
if msg.topic == config.startTopics['tafel7']:
if msg.payload.decode('utf-8') == 'start':
config.tb7start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb7start = False
if msg.topic == config.startTopics['tafel8']:
if msg.payload.decode('utf-8') == 'start':
config.tb8start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb8start = False
class CrudeTimerGrid(GridLayout):
_python_access = ObjectProperty(None)
time = NumericProperty(0)
def __init__(self,**kwargs):
super(CrudeTimerGrid,self).__init__(**kwargs)
self.runningTimer = 0
Clock.schedule_interval(self.load_times,1)
Clock.schedule_interval(self.start,1)
def load_times(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
self.time = config.tim1
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
self.time = config.tim2
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
self.time = config.tim3
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
self.time = config.tim4
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
self.time = config.tim5
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
self.time = config.tim6
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
self.time = config.tim7
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
self.time = config.tim8
def start(self, *_):
tafelobjecten = self.parent.parent.ids
self.runningTimer = self.time
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
#self.time = config.tim1
if config.tb1start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel2':
#self.time = config.tim2
if config.tb2start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel3':
#self.time = config.tim3
if config.tb3start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel4':
#self.time = config.tim4
if config.tb4start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel5':
#self.time = config.tim5
if config.tb5start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel6':
#self.time = config.tim6
if config.tb6start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel7':
#self.time = config.tim7
if config.tb7start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel8':
#self.time = config.tim8
if config.tb8start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
def pause(self):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = False
Clock.unschedule(self.tick)
def resume(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = True
Clock.schedule_interval(self.tick,1) #pass
def stop(self, *_):
#TODO: implement stop button
pass
def tick(self, *_):
tafelobjecten = self.parent.parent.ids
if self.runningTimer > 0:
self.runningTimer -= 1
# publish de juiste tafel topic met de waarde van de restrerende tijd
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
client.publish(topic = str(list(tafelobjecten.keys())[x]), payload = str(self.runningTimer))
self.ids.Changelabel.text = str(time.strftime('%H:%M:%S',time.gmtime(self.runningTimer)))
else:
pass
class Main(GridLayout):
pass
class CrudeTimerApp(App):
pass
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(config.settings['username'], config.settings['password'])
client.connect(config.settings['broker'])
t = threading.Thread(target=client.loop_start())
t.daemon = True
t.start()
CrudeTimerApp().run()
My .kv file:
#:kivy 1.10.1
################################### Widget template ##########################################
<CrudeTimerGrid>:
_python_access: Changelabel
id: timer
rows: 4
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Restrerende tijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
id: Changelabel
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Pauzetijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: '00'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.2
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Button:
text: "Pauze"
on_press: timer.pause()
Button:
text: "Hervatten"
on_press: timer.resume()
Button:
text: "Stoppen"
#on_press: timer.reset()
Label:
text: ''
################################### Widget template ##########################################
<Main@Widget>:
rows: 2 # 2 rijen
cols: 4 # 4 colums
padding: 10
spacing: 10
################################### Tafel 1 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 1'
CrudeTimerGrid:
id: tafel1
################################### Tafel 1 ##########################################
################################### Tafel 2 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 2'
CrudeTimerGrid:
id: tafel2
################################### Tafel 2 ##########################################
################################### Tafel 3 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 3'
CrudeTimerGrid:
id: tafel3
################################### Tafel 3 ##########################################
################################### Tafel 4 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 4'
CrudeTimerGrid:
id: tafel4
################################### Tafel 4 ##########################################
################################### Tafel 5 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 5'
CrudeTimerGrid:
id: tafel5
################################### Tafel 5 ##########################################
################################### Tafel 6 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 6'
CrudeTimerGrid:
id: tafel6
################################### Tafel 6 ##########################################
################################### Tafel 7 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 7'
CrudeTimerGrid:
id: tafel7
################################### Tafel 7 ##########################################
################################### Tafel 8 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 8'
CrudeTimerGrid:
id: tafel8
################################### Tafel 8 ##########################################
Main:
my config.py:
# MQTT broker settings.
settings = dict(
broker = '172.16.24.128',
username = 'pi',
password = 'Piaservice123'
)
# start topics.
startTopics = dict(
tafel1 = 'tafel1start',
tafel2 = 'tafel2start',
tafel3 = 'tafel3start',
tafel4 = 'tafel4start',
tafel5 = 'tafel5start',
tafel6 = 'tafel6start',
tafel7 = 'tafel7start',
tafel8 = 'tafel8start'
)
# time in seconds topics.
timeTopics = dict(
tafel1 = 'tafel1uren',
tafel2 = 'tafel2uren',
tafel3 = 'tafel3uren',
tafel4 = 'tafel4uren',
tafel5 = 'tafel5uren',
tafel6 = 'tafel6uren',
tafel7 = 'tafel7uren',
tafel8 = 'tafel8uren'
)
# Currenttime topics.
currentTime = dict(
tafel1 = 'tafel1',
tafel2 = 'tafel2',
tafel3 = 'tafel3',
tafel4 = 'tafel4',
tafel5 = 'tafel5',
tafel6 = 'tafel6',
tafel7 = 'tafel7',
tafel8 = 'tafel8'
)
# Global timer vars
tim1 = 0
tim2 = 0
tim3 = 0
tim4 = 0
tim5 = 0
tim6 = 0
tim7 = 0
tim8 = 0
# startbooleans
tb1start = False
tb2start = False
tb3start = False
tb4start = False
tb5start = False
tb6start = False
tb7start = False
tb8start = False
The class where all the magic happens is the class CrudeTimerGrid().
How can I make it so my timer won't reset everytime I click on my Resume button?
Edit: managed to fix my problem!
I made an event(self.event = Clock.schedule_interval(self.tick,1)) and have 2 buttons to pause and resume.
I use the pause button (Clock.unschedule(self.event)) to unschedule the event and the resume button (Clock.schedule_once(self.event)) to reschedule my timer.
python kivy kivy-language
add a comment |
I'm trying to create a kivy application.
The Application works as following:
- I send the amount of seconds I want my timer to last via MQTT over the topic 'tafelxuren' (x is a number between 1 and 8).
- After sending the amounts of seconds I want it to last, I send a second MQTT command to the topic 'tafelxstart' (x is a number between 1 and 8) with the command 'start'.
- After sending the start command, my timer starts running.

Under my timer I have 3 buttons, pause, resume and stop.
While starting and pausing the timer works using MQTT commands, I cannot seem to get my resume button to function properly, if there's 26 seconds left after clicking on the pause button, I want my timer to resume from that point. Instead, it starts again from the beginning
My main.py:
import os
if os.name == 'posix':
os.environ['KIVY_GL_BACKEND'] = 'gl'
import kivy, time,threading
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.clock import Clock
from kivy.config import Config
import paho.mqtt.client as mqtt
Config.set('graphics', 'fullscreen', 'auto')
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import config
# lege MQTT-client instantie
client = None
# De topics die gebruikt worden
ingesteldetijd = [config.timeTopics['tafel1'],config.timeTopics['tafel2'],config.timeTopics['tafel3'],config.timeTopics['tafel4'],config.timeTopics['tafel5'],config.timeTopics['tafel6'],config.timeTopics['tafel7'],config.timeTopics['tafel8']]
ingesteldetijdTopics = [config.tim1, config.tim2, config.tim3, config.tim4, config.tim5, config.tim6, config.tim7, config.tim8]
starttopics = [config.startTopics['tafel1'],config.startTopics['tafel2'],config.startTopics['tafel3'],config.startTopics['tafel4'],config.startTopics['tafel5'],config.startTopics['tafel6'],config.startTopics['tafel7'],config.startTopics['tafel8']]
def on_connect(client,userdata,flags,rc):
'''
This function gets triggered when MQTT is connected succesfully
'''
if(rc == 0):
print("[INFO ] [MQTT ] MQTT connected to broker "+config.settings['broker']+".")
client.subscribe('#')
##################################### ingestelde tijd subscriptions #####################################
for x in range(0,8):
client.subscribe(ingesteldetijd[x])
print('[INFO ] [MQTT ] Subscribed to '+ingesteldetijd[x])
##################################### ingestelde tijd subscriptions #####################################
##################################### start topic subscriptions #####################################
for x in range(0,8):
client.subscribe(starttopics[x])
print('[INFO ] [MQTT ] Subscribed to '+starttopics[x])
##################################### start topic subscriptions #####################################
else:
print("MQTT connection to broker "+config.settings['broker']+"failed.")
def on_message(client,userdata,msg):
'''
If there's a message received on one of the topics, the messages gets handled here.
'''
################################## tijd instellen topic ##################################
if msg.topic == 'tafel1uren':
config.tim1 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel2uren':
config.tim2 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel3uren':
config.tim3 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel4uren':
config.tim4 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel5uren':
config.tim5 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel6uren':
config.tim6 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel7uren':
config.tim7 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel8uren':
config.tim8 = int(msg.payload.decode('utf-8'))
################################## tijd instellen topic ##################################
if msg.topic == config.startTopics['tafel1']:
if msg.payload.decode('utf-8') == 'start':
config.tb1start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb1start = False
if msg.topic == config.startTopics['tafel2']:
if msg.payload.decode('utf-8') == 'start':
config.tb2start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb2start = False
if msg.topic == config.startTopics['tafel3']:
if msg.payload.decode('utf-8') == 'start':
config.tb3start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb3start = False
if msg.topic == config.startTopics['tafel4']:
if msg.payload.decode('utf-8') == 'start':
config.tb4start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb4start = False
if msg.topic == config.startTopics['tafel5']:
if msg.payload.decode('utf-8') == 'start':
config.tb5start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb5start = False
if msg.topic == config.startTopics['tafel6']:
if msg.payload.decode('utf-8') == 'start':
config.tb6start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb6start = False
if msg.topic == config.startTopics['tafel7']:
if msg.payload.decode('utf-8') == 'start':
config.tb7start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb7start = False
if msg.topic == config.startTopics['tafel8']:
if msg.payload.decode('utf-8') == 'start':
config.tb8start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb8start = False
class CrudeTimerGrid(GridLayout):
_python_access = ObjectProperty(None)
time = NumericProperty(0)
def __init__(self,**kwargs):
super(CrudeTimerGrid,self).__init__(**kwargs)
self.runningTimer = 0
Clock.schedule_interval(self.load_times,1)
Clock.schedule_interval(self.start,1)
def load_times(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
self.time = config.tim1
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
self.time = config.tim2
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
self.time = config.tim3
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
self.time = config.tim4
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
self.time = config.tim5
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
self.time = config.tim6
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
self.time = config.tim7
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
self.time = config.tim8
def start(self, *_):
tafelobjecten = self.parent.parent.ids
self.runningTimer = self.time
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
#self.time = config.tim1
if config.tb1start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel2':
#self.time = config.tim2
if config.tb2start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel3':
#self.time = config.tim3
if config.tb3start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel4':
#self.time = config.tim4
if config.tb4start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel5':
#self.time = config.tim5
if config.tb5start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel6':
#self.time = config.tim6
if config.tb6start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel7':
#self.time = config.tim7
if config.tb7start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel8':
#self.time = config.tim8
if config.tb8start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
def pause(self):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = False
Clock.unschedule(self.tick)
def resume(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = True
Clock.schedule_interval(self.tick,1) #pass
def stop(self, *_):
#TODO: implement stop button
pass
def tick(self, *_):
tafelobjecten = self.parent.parent.ids
if self.runningTimer > 0:
self.runningTimer -= 1
# publish de juiste tafel topic met de waarde van de restrerende tijd
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
client.publish(topic = str(list(tafelobjecten.keys())[x]), payload = str(self.runningTimer))
self.ids.Changelabel.text = str(time.strftime('%H:%M:%S',time.gmtime(self.runningTimer)))
else:
pass
class Main(GridLayout):
pass
class CrudeTimerApp(App):
pass
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(config.settings['username'], config.settings['password'])
client.connect(config.settings['broker'])
t = threading.Thread(target=client.loop_start())
t.daemon = True
t.start()
CrudeTimerApp().run()
My .kv file:
#:kivy 1.10.1
################################### Widget template ##########################################
<CrudeTimerGrid>:
_python_access: Changelabel
id: timer
rows: 4
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Restrerende tijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
id: Changelabel
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Pauzetijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: '00'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.2
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Button:
text: "Pauze"
on_press: timer.pause()
Button:
text: "Hervatten"
on_press: timer.resume()
Button:
text: "Stoppen"
#on_press: timer.reset()
Label:
text: ''
################################### Widget template ##########################################
<Main@Widget>:
rows: 2 # 2 rijen
cols: 4 # 4 colums
padding: 10
spacing: 10
################################### Tafel 1 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 1'
CrudeTimerGrid:
id: tafel1
################################### Tafel 1 ##########################################
################################### Tafel 2 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 2'
CrudeTimerGrid:
id: tafel2
################################### Tafel 2 ##########################################
################################### Tafel 3 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 3'
CrudeTimerGrid:
id: tafel3
################################### Tafel 3 ##########################################
################################### Tafel 4 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 4'
CrudeTimerGrid:
id: tafel4
################################### Tafel 4 ##########################################
################################### Tafel 5 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 5'
CrudeTimerGrid:
id: tafel5
################################### Tafel 5 ##########################################
################################### Tafel 6 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 6'
CrudeTimerGrid:
id: tafel6
################################### Tafel 6 ##########################################
################################### Tafel 7 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 7'
CrudeTimerGrid:
id: tafel7
################################### Tafel 7 ##########################################
################################### Tafel 8 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 8'
CrudeTimerGrid:
id: tafel8
################################### Tafel 8 ##########################################
Main:
my config.py:
# MQTT broker settings.
settings = dict(
broker = '172.16.24.128',
username = 'pi',
password = 'Piaservice123'
)
# start topics.
startTopics = dict(
tafel1 = 'tafel1start',
tafel2 = 'tafel2start',
tafel3 = 'tafel3start',
tafel4 = 'tafel4start',
tafel5 = 'tafel5start',
tafel6 = 'tafel6start',
tafel7 = 'tafel7start',
tafel8 = 'tafel8start'
)
# time in seconds topics.
timeTopics = dict(
tafel1 = 'tafel1uren',
tafel2 = 'tafel2uren',
tafel3 = 'tafel3uren',
tafel4 = 'tafel4uren',
tafel5 = 'tafel5uren',
tafel6 = 'tafel6uren',
tafel7 = 'tafel7uren',
tafel8 = 'tafel8uren'
)
# Currenttime topics.
currentTime = dict(
tafel1 = 'tafel1',
tafel2 = 'tafel2',
tafel3 = 'tafel3',
tafel4 = 'tafel4',
tafel5 = 'tafel5',
tafel6 = 'tafel6',
tafel7 = 'tafel7',
tafel8 = 'tafel8'
)
# Global timer vars
tim1 = 0
tim2 = 0
tim3 = 0
tim4 = 0
tim5 = 0
tim6 = 0
tim7 = 0
tim8 = 0
# startbooleans
tb1start = False
tb2start = False
tb3start = False
tb4start = False
tb5start = False
tb6start = False
tb7start = False
tb8start = False
The class where all the magic happens is the class CrudeTimerGrid().
How can I make it so my timer won't reset everytime I click on my Resume button?
Edit: managed to fix my problem!
I made an event(self.event = Clock.schedule_interval(self.tick,1)) and have 2 buttons to pause and resume.
I use the pause button (Clock.unschedule(self.event)) to unschedule the event and the resume button (Clock.schedule_once(self.event)) to reschedule my timer.
python kivy kivy-language
I'm trying to create a kivy application.
The Application works as following:
- I send the amount of seconds I want my timer to last via MQTT over the topic 'tafelxuren' (x is a number between 1 and 8).
- After sending the amounts of seconds I want it to last, I send a second MQTT command to the topic 'tafelxstart' (x is a number between 1 and 8) with the command 'start'.
- After sending the start command, my timer starts running.

Under my timer I have 3 buttons, pause, resume and stop.
While starting and pausing the timer works using MQTT commands, I cannot seem to get my resume button to function properly, if there's 26 seconds left after clicking on the pause button, I want my timer to resume from that point. Instead, it starts again from the beginning
My main.py:
import os
if os.name == 'posix':
os.environ['KIVY_GL_BACKEND'] = 'gl'
import kivy, time,threading
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.clock import Clock
from kivy.config import Config
import paho.mqtt.client as mqtt
Config.set('graphics', 'fullscreen', 'auto')
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import config
# lege MQTT-client instantie
client = None
# De topics die gebruikt worden
ingesteldetijd = [config.timeTopics['tafel1'],config.timeTopics['tafel2'],config.timeTopics['tafel3'],config.timeTopics['tafel4'],config.timeTopics['tafel5'],config.timeTopics['tafel6'],config.timeTopics['tafel7'],config.timeTopics['tafel8']]
ingesteldetijdTopics = [config.tim1, config.tim2, config.tim3, config.tim4, config.tim5, config.tim6, config.tim7, config.tim8]
starttopics = [config.startTopics['tafel1'],config.startTopics['tafel2'],config.startTopics['tafel3'],config.startTopics['tafel4'],config.startTopics['tafel5'],config.startTopics['tafel6'],config.startTopics['tafel7'],config.startTopics['tafel8']]
def on_connect(client,userdata,flags,rc):
'''
This function gets triggered when MQTT is connected succesfully
'''
if(rc == 0):
print("[INFO ] [MQTT ] MQTT connected to broker "+config.settings['broker']+".")
client.subscribe('#')
##################################### ingestelde tijd subscriptions #####################################
for x in range(0,8):
client.subscribe(ingesteldetijd[x])
print('[INFO ] [MQTT ] Subscribed to '+ingesteldetijd[x])
##################################### ingestelde tijd subscriptions #####################################
##################################### start topic subscriptions #####################################
for x in range(0,8):
client.subscribe(starttopics[x])
print('[INFO ] [MQTT ] Subscribed to '+starttopics[x])
##################################### start topic subscriptions #####################################
else:
print("MQTT connection to broker "+config.settings['broker']+"failed.")
def on_message(client,userdata,msg):
'''
If there's a message received on one of the topics, the messages gets handled here.
'''
################################## tijd instellen topic ##################################
if msg.topic == 'tafel1uren':
config.tim1 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel2uren':
config.tim2 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel3uren':
config.tim3 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel4uren':
config.tim4 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel5uren':
config.tim5 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel6uren':
config.tim6 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel7uren':
config.tim7 = int(msg.payload.decode('utf-8'))
if msg.topic == 'tafel8uren':
config.tim8 = int(msg.payload.decode('utf-8'))
################################## tijd instellen topic ##################################
if msg.topic == config.startTopics['tafel1']:
if msg.payload.decode('utf-8') == 'start':
config.tb1start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb1start = False
if msg.topic == config.startTopics['tafel2']:
if msg.payload.decode('utf-8') == 'start':
config.tb2start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb2start = False
if msg.topic == config.startTopics['tafel3']:
if msg.payload.decode('utf-8') == 'start':
config.tb3start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb3start = False
if msg.topic == config.startTopics['tafel4']:
if msg.payload.decode('utf-8') == 'start':
config.tb4start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb4start = False
if msg.topic == config.startTopics['tafel5']:
if msg.payload.decode('utf-8') == 'start':
config.tb5start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb5start = False
if msg.topic == config.startTopics['tafel6']:
if msg.payload.decode('utf-8') == 'start':
config.tb6start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb6start = False
if msg.topic == config.startTopics['tafel7']:
if msg.payload.decode('utf-8') == 'start':
config.tb7start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb7start = False
if msg.topic == config.startTopics['tafel8']:
if msg.payload.decode('utf-8') == 'start':
config.tb8start = True
if msg.payload.decode('utf-8') == 'stop':
config.tb8start = False
class CrudeTimerGrid(GridLayout):
_python_access = ObjectProperty(None)
time = NumericProperty(0)
def __init__(self,**kwargs):
super(CrudeTimerGrid,self).__init__(**kwargs)
self.runningTimer = 0
Clock.schedule_interval(self.load_times,1)
Clock.schedule_interval(self.start,1)
def load_times(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
self.time = config.tim1
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
self.time = config.tim2
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
self.time = config.tim3
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
self.time = config.tim4
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
self.time = config.tim5
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
self.time = config.tim6
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
self.time = config.tim7
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
self.time = config.tim8
def start(self, *_):
tafelobjecten = self.parent.parent.ids
self.runningTimer = self.time
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
#self.time = config.tim1
if config.tb1start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel2':
#self.time = config.tim2
if config.tb2start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel3':
#self.time = config.tim3
if config.tb3start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel4':
#self.time = config.tim4
if config.tb4start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel5':
#self.time = config.tim5
if config.tb5start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel6':
#self.time = config.tim6
if config.tb6start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel7':
#self.time = config.tim7
if config.tb7start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
elif str(list(tafelobjecten.keys())[x]) == 'tafel8':
#self.time = config.tim8
if config.tb8start == True:
if(self.runningTimer > 0):
Clock.schedule_interval(self.tick,1)
def pause(self):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = False
Clock.unschedule(self.tick)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = False
Clock.unschedule(self.tick)
def resume(self, *_):
tafelobjecten = self.parent.parent.ids
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
if str(list(tafelobjecten.keys())[x]) == 'tafel1':
config.tb1start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel2':
config.tb2start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel3':
config.tb3start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel4':
config.tb4start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel5':
config.tb5start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel6':
config.tb6start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel7':
config.tb7start = True
Clock.schedule_interval(self.tick,1)
if str(list(tafelobjecten.keys())[x]) == 'tafel8':
config.tb8start = True
Clock.schedule_interval(self.tick,1) #pass
def stop(self, *_):
#TODO: implement stop button
pass
def tick(self, *_):
tafelobjecten = self.parent.parent.ids
if self.runningTimer > 0:
self.runningTimer -= 1
# publish de juiste tafel topic met de waarde van de restrerende tijd
for x in range(0,8):
if list(tafelobjecten.values())[x] == self:
client.publish(topic = str(list(tafelobjecten.keys())[x]), payload = str(self.runningTimer))
self.ids.Changelabel.text = str(time.strftime('%H:%M:%S',time.gmtime(self.runningTimer)))
else:
pass
class Main(GridLayout):
pass
class CrudeTimerApp(App):
pass
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(config.settings['username'], config.settings['password'])
client.connect(config.settings['broker'])
t = threading.Thread(target=client.loop_start())
t.daemon = True
t.start()
CrudeTimerApp().run()
My .kv file:
#:kivy 1.10.1
################################### Widget template ##########################################
<CrudeTimerGrid>:
_python_access: Changelabel
id: timer
rows: 4
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Restrerende tijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
id: Changelabel
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: 'Pauzetijd:'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Label:
font_size: 20
text: '00'
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
BoxLayout:
size_hint_y: 0.2
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
Button:
text: "Pauze"
on_press: timer.pause()
Button:
text: "Hervatten"
on_press: timer.resume()
Button:
text: "Stoppen"
#on_press: timer.reset()
Label:
text: ''
################################### Widget template ##########################################
<Main@Widget>:
rows: 2 # 2 rijen
cols: 4 # 4 colums
padding: 10
spacing: 10
################################### Tafel 1 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 1'
CrudeTimerGrid:
id: tafel1
################################### Tafel 1 ##########################################
################################### Tafel 2 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 2'
CrudeTimerGrid:
id: tafel2
################################### Tafel 2 ##########################################
################################### Tafel 3 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 3'
CrudeTimerGrid:
id: tafel3
################################### Tafel 3 ##########################################
################################### Tafel 4 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 4'
CrudeTimerGrid:
id: tafel4
################################### Tafel 4 ##########################################
################################### Tafel 5 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 5'
CrudeTimerGrid:
id: tafel5
################################### Tafel 5 ##########################################
################################### Tafel 6 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 6'
CrudeTimerGrid:
id: tafel6
################################### Tafel 6 ##########################################
################################### Tafel 7 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 7'
CrudeTimerGrid:
id: tafel7
################################### Tafel 7 ##########################################
################################### Tafel 8 ##########################################
BoxLayout:
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
orientation: 'vertical'
Label:
size_hint_y: 0.15
font_size: 25
canvas.before:
Color:
rgba: .5, .5, .5, 1
Rectangle:
pos: self.pos
size: self.size
text: 'Tafel 8'
CrudeTimerGrid:
id: tafel8
################################### Tafel 8 ##########################################
Main:
my config.py:
# MQTT broker settings.
settings = dict(
broker = '172.16.24.128',
username = 'pi',
password = 'Piaservice123'
)
# start topics.
startTopics = dict(
tafel1 = 'tafel1start',
tafel2 = 'tafel2start',
tafel3 = 'tafel3start',
tafel4 = 'tafel4start',
tafel5 = 'tafel5start',
tafel6 = 'tafel6start',
tafel7 = 'tafel7start',
tafel8 = 'tafel8start'
)
# time in seconds topics.
timeTopics = dict(
tafel1 = 'tafel1uren',
tafel2 = 'tafel2uren',
tafel3 = 'tafel3uren',
tafel4 = 'tafel4uren',
tafel5 = 'tafel5uren',
tafel6 = 'tafel6uren',
tafel7 = 'tafel7uren',
tafel8 = 'tafel8uren'
)
# Currenttime topics.
currentTime = dict(
tafel1 = 'tafel1',
tafel2 = 'tafel2',
tafel3 = 'tafel3',
tafel4 = 'tafel4',
tafel5 = 'tafel5',
tafel6 = 'tafel6',
tafel7 = 'tafel7',
tafel8 = 'tafel8'
)
# Global timer vars
tim1 = 0
tim2 = 0
tim3 = 0
tim4 = 0
tim5 = 0
tim6 = 0
tim7 = 0
tim8 = 0
# startbooleans
tb1start = False
tb2start = False
tb3start = False
tb4start = False
tb5start = False
tb6start = False
tb7start = False
tb8start = False
The class where all the magic happens is the class CrudeTimerGrid().
How can I make it so my timer won't reset everytime I click on my Resume button?
Edit: managed to fix my problem!
I made an event(self.event = Clock.schedule_interval(self.tick,1)) and have 2 buttons to pause and resume.
I use the pause button (Clock.unschedule(self.event)) to unschedule the event and the resume button (Clock.schedule_once(self.event)) to reschedule my timer.
python kivy kivy-language
python kivy kivy-language
edited Mar 11 at 10:12
hoek rand
asked Mar 7 at 9:59
hoek randhoek rand
899
899
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You won't be able to unschedule a Kivy Clock event by using Clock.unschedule(self.tick). Please refer to the example below.
Kivy Clock » Unscheduling
Schedule
Replace
Clock.schedule_interval(self.tick, 1)
with
self.event = Clock.schedule_interval(self.tick, 1)
Unschedule
so that you can unschedule using
either
self.event.cancel()
or
Clock.unschedule(self.event)
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
Since you have 8 tables/windows, you should have 8 start events i.e.self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e.Clock.unschedule(self.tafel?)where ? = 1 to 8.
– ikolim
Mar 8 at 16:59
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040883%2fhaving-trouble-with-implementing-a-timer-in-kivy%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You won't be able to unschedule a Kivy Clock event by using Clock.unschedule(self.tick). Please refer to the example below.
Kivy Clock » Unscheduling
Schedule
Replace
Clock.schedule_interval(self.tick, 1)
with
self.event = Clock.schedule_interval(self.tick, 1)
Unschedule
so that you can unschedule using
either
self.event.cancel()
or
Clock.unschedule(self.event)
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
Since you have 8 tables/windows, you should have 8 start events i.e.self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e.Clock.unschedule(self.tafel?)where ? = 1 to 8.
– ikolim
Mar 8 at 16:59
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
add a comment |
You won't be able to unschedule a Kivy Clock event by using Clock.unschedule(self.tick). Please refer to the example below.
Kivy Clock » Unscheduling
Schedule
Replace
Clock.schedule_interval(self.tick, 1)
with
self.event = Clock.schedule_interval(self.tick, 1)
Unschedule
so that you can unschedule using
either
self.event.cancel()
or
Clock.unschedule(self.event)
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
Since you have 8 tables/windows, you should have 8 start events i.e.self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e.Clock.unschedule(self.tafel?)where ? = 1 to 8.
– ikolim
Mar 8 at 16:59
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
add a comment |
You won't be able to unschedule a Kivy Clock event by using Clock.unschedule(self.tick). Please refer to the example below.
Kivy Clock » Unscheduling
Schedule
Replace
Clock.schedule_interval(self.tick, 1)
with
self.event = Clock.schedule_interval(self.tick, 1)
Unschedule
so that you can unschedule using
either
self.event.cancel()
or
Clock.unschedule(self.event)
You won't be able to unschedule a Kivy Clock event by using Clock.unschedule(self.tick). Please refer to the example below.
Kivy Clock » Unscheduling
Schedule
Replace
Clock.schedule_interval(self.tick, 1)
with
self.event = Clock.schedule_interval(self.tick, 1)
Unschedule
so that you can unschedule using
either
self.event.cancel()
or
Clock.unschedule(self.event)
edited Mar 7 at 16:44
answered Mar 7 at 16:15
ikolimikolim
7,5091824
7,5091824
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
Since you have 8 tables/windows, you should have 8 start events i.e.self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e.Clock.unschedule(self.tafel?)where ? = 1 to 8.
– ikolim
Mar 8 at 16:59
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
add a comment |
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
Since you have 8 tables/windows, you should have 8 start events i.e.self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e.Clock.unschedule(self.tafel?)where ? = 1 to 8.
– ikolim
Mar 8 at 16:59
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
I tried your suggestion, and the resetting of time is fixed, but I've still got the problem that when I click my resume button twice, that I get 2 timers running simultaneously which both change the label, you see the time changing between the two timers continously
– hoek rand
Mar 8 at 10:55
Since you have 8 tables/windows, you should have 8 start events i.e.
self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e. Clock.unschedule(self.tafel?) where ? = 1 to 8.– ikolim
Mar 8 at 16:59
Since you have 8 tables/windows, you should have 8 start events i.e.
self.tafel? = Clock.schedule_interval(self.tick, 1), and 8 cancel / unschedule events i.e. Clock.unschedule(self.tafel?) where ? = 1 to 8.– ikolim
Mar 8 at 16:59
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
Did manage to fix my problem as per your suggestion, the only thing different is a schedule_once to spin up my event again. Thanks for the suggestion, it helped alot!
– hoek rand
Mar 11 at 10:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040883%2fhaving-trouble-with-implementing-a-timer-in-kivy%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown