Page 1 of 1

root.after or while true ?

Posted: Fri Jan 22, 2016 2:56 pm
by Davies
is it better to use root.after or while true?
for example in this

Code: Select all

def heater():
    global heater_on, heat_on
    if heater_on <= current_temp - 1 and heat_on == 0:
        block8.config(text='Heater On', bg='green')
        heat_on = 1
    if heater_on >= current_temp and heat_on == 1:
        block8.config(text='Heater Off', bg='red')
        heat_on = 0
    root.after(1000, heater)
ive used root. after(1000, heater) but would this not call heater from within its own def and create multiple instances?
would it be better to use a while?

Code: Select all

def heater():
    global heater_on, heat_on
    while True:
        if heater_on <= current_temp - 1 and heat_on == 0:
            block8.config(text='Heater On', bg='green')
            heat_on = 1
        if heater_on >= current_temp and heat_on == 1:
            block8.config(text='Heater Off', bg='red')
            heat_on = 0
    time.sleep(10)
is it better to use a sleep with the while, so it isn't running as quickly as it can but rather on a sleep time?
would both examples need to be run from threads to prevent pausing within root tasks?

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 3:11 pm
by DirkS
Is 'root.after' part of standard python or is it in some module you installed?
What does it do?

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 3:14 pm
by Davies
apologies, I believe root.after is part of tkinter as it would update the labels within a tkinter window after 1sec
here's a full working example

Code: Select all

import Tkinter as tk
from Tkinter import *

root = tk.Tk()
root.geometry("200x200")

heat_on = 1

canvas = Canvas(width=200, height=200, bg='black')
canvas.grid(rowspan=2, columnspan=1, sticky='W,E,N,S')


def button():
    global heat_on
    if button1["text"] == "Off":
        heat_on = 1
    if button1["text"] == "On":
        heat_on = 0


def heater():
    global heat_on
    if heat_on == 0:
        button1.config(text="Off")
        block8.config(text='Heater On', bg='green')
    if heat_on == 1:
        button1.config(text="On")
        block8.config(text='Heater Off', bg='red')
    root.after(500, heater)

block8 = Label(root, text="Heater Off", width=10, font=('aharoni', 18, 'bold'), bg='red', fg='white')
block8.grid(padx=5, pady=10, row=0, column=0, sticky='N')
button1 = tk.Button(root, text="On", width=10, font=('aharoni', 16, 'bold'), bg='black', fg='white', command=button)
button1.grid(padx=5, pady=10, row=1, column=0, sticky='N')

heater()

root.mainloop()

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 3:27 pm
by DirkS
Davies wrote:apologies, I believe root.after is part of tkinter as it would update the labels within a tkinter window after 1sec
You may be right, but how would I, as a potential helper, know that from the small block of code you're posting.

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 3:45 pm
by Davies
DirkS wrote:You may be right, but how would I, as a potential helper, know that from the small block of code you're posting.
as you was typing this I was writing a full example, please review my previous post

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 6:17 pm
by hippy
Davies wrote:is it better to use root.after or while true?
If you use a while true then heater() never exits and you never reach your root.mainloop().

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 6:56 pm
by Davies
thanks for your reply, im aware that with a while loop I would need to use threads. my main concern is that with the first example

Code: Select all

def heater():
    global heater_on, heat_on
    if heater_on <= current_temp - 1 and heat_on == 0:
        block8.config(text='Heater On', bg='green')
        heat_on = 1
    if heater_on >= current_temp and heat_on == 1:
        block8.config(text='Heater Off', bg='red')
        heat_on = 0
    root.after(1000, heater)
will root.after(1000, heater) call a recursive def and fill the stack and should a while be used instead or is that a legitimate use?
im unsure exactly what it calls but if its like saying sleep>update_idletasks then itll be ok, if its sleep>heater then this would cause issues running longterm

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 7:10 pm
by hippy
Davies wrote:will root.after(1000, heater) call a recursive def and fill the stack and should a while be used instead or is that a legitimate use?
As I understand it; that will schedule heater to be executed in the future, then heater ends, it's not a recursive call so stack will only be used while heater is running.

Re: root.after or while true ?

Posted: Fri Jan 22, 2016 7:24 pm
by Davies
hippy wrote:As I understand it; that will schedule heater to be executed in the future, then heater ends, it's not a recursive call so stack will only be used while heater is running.
thank you for your reply,.. so as such it would not need a thread as there is no actual delay? (other than the delay listed within the def)