Hi,
I have this

def update_labelain0_val(self):
        val=get_ain0()
        self.labelain0_val.set(val)
        self.labelain0.config(bg="#32CD32")
        #0-12.9v -> 0-6000N
        valain0=float(val)
        valain0_calc=valain0*6000/12.9
        valain0_calc=int(valain0_calc)
        self.labelain0_calc.set(valain0_calc)
        self.labelain0calc.config(bg="#FF8C00")

        threading.Timer(0.1, self.update_labelain0_val).start()
        return

This is part of a class which update a label in a window. It is working fine. My problem is :
how can i give a variable name o this thread and have a function that stop the thread before close the window.

Thank you

You could collect the threads in some container and iterate through it and close each thread when you close the window.

If i don t ask too much can you point me to an example for this?
Thanks

I would tend to use a Condition object and a wait() with timeout instead of a timer. Here is an example

#!/usr/bin/env python
# -*-coding: utf8-*-
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

__doc__ = '''
'''
import threading

def hello_world():
    print('hello world')

class PeriodicCall(object):
    def __init__(self, seconds, func, *args, **kwd):
        self._thread = threading.Thread(target = self._periodic_call,
            args = (float(seconds), func, args, kwd))
        self._cond = threading.Condition()
        self._interrupted = False

    def start(self):
        self._interrupted = False
        self._thread.start()

    def _periodic_call(self, seconds, func, args, kwd):
        with self._cond:
            while True:
                if self._interrupted:
                    break
                self._cond.wait(seconds)
                if not self._interrupted:
                    func(*args, **kwd)

    def cancel(self):
        with self._cond:
            self._interrupted = True
            self._cond.notify()
        self._thread.join()

if __name__ == '__main__':
    p = PeriodicCall(0.8, hello_world)
    import time
    p.start()
    for i in range(5):
        time.sleep(0.377689)
        print('main')
    p.cancel()
    print('canceled')
    time.sleep(2)
    print('bye')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.