epkeati
Posts: 3
Joined: Tue Dec 04, 2018 9:22 pm

TKinter loop

Wed Dec 05, 2018 6:28 pm

Hi guys,

I just started out using raspberry Pi, and would like some help.

Problem:

I am trying to run tkinter along side code that will make cooling units turn on. I am trying to use a scale to set the temperature and turn on the units until the temperature reached the preset temp.

I been able to run the window using tkinter.

But i have not been able to update the values to turn on the cooling units.

Code:

Code: Select all

import board
import busio
import adafruit_si7021
import time
import RPi.GPIO as gpio
from tkinter import *
import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711
import threading
import queue

# Force Python 3 ###########################################################

if sys.version_info[0] != 3:
    raise Exception("Python 3 is required.")

def usertemp(usertemp):
    ftemp=int(usertemp)
    usertempf=(int(ftemp-32)*5/9)
    print(usertempf)
    #print(temp)
    return usertempf

def turnon():
 gpio.output(17, True)
 gpio.output(22, True)
 gpio.output(4, True)
 gpio.output(27, True)

#Turn off TEC
def turnoff():
 gpio.output(17, False)
 gpio.output(22, False)
 gpio.output(4, False)
 gpio.output(27, False)

    

#Set up
i2c = busio.I2C(board.SCL, board.SDA);
sensor = adafruit_si7021.SI7021(i2c);
hx = HX711(5, 6)

gpio.setmode(gpio.BCM)
gpio.setup(23, gpio.IN, pull_up_down=GPIO.PUD_UP)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(4, gpio.OUT)
gpio.setup(27, gpio.OUT)




#Function Call

    #GUI
root = Tk()

while True:

    boxxle = Label(root, text="Boxxle Wine Chiller", font="times 35", fg="red")
    whiter = Label(root)
    tslide = Label(root, text="Set Temperature", font="times 12", fg="black")
    temperature = Label(root, text="Current Temperature\n73 Degrees F", font="times 12", fg="black")
    left = Label(root, text="Percent Left is XX%", font="times 12", fg="black")

    #slide
    slide1=Scale(root, from_=72, to=35, command=usertemp)
    usertempf = slide1
    # 
    boxxle.grid(row=0, sticky=W)
    tslide.grid(row=2, column=9)
    boxxle.grid(row=0, column=1)
    whiter.grid(row=1, column=8)
    temperature.grid(row=2, column=0)
    slide1.grid(row=2, column=8)
    left.grid(row=7, column=1)

            
    if sensor.temperature+3 > usertempf or sensor.temperature--3 > usertempf:
        turnon()
        print(sensor.temperature)
    else:
        turnoff()
root.mainloop()



scotty101
Posts: 3470
Joined: Fri Jun 08, 2012 6:03 pm

Re: TKinter loop

Wed Dec 05, 2018 10:00 pm

Tkinter and "while True" loops should never mix.

If you want something to happen periodically with tkinter you should use the .after method to schedule a function to be called after a period of time.

For example

Code: Select all

root.after(100, updateGUI)
Will call a function called updateGUI after 100milliseconds.

Basic concept is that you create your GUI, schedule a function with after and then call the mainloop method.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

epkeati
Posts: 3
Joined: Tue Dec 04, 2018 9:22 pm

Re: TKinter loop

Wed Dec 05, 2018 10:22 pm

Okay cool,

But how do i make it call the function over and over again? When I tried this it only called the function once.

scotty101
Posts: 3470
Joined: Fri Jun 08, 2012 6:03 pm

Re: TKinter loop

Wed Dec 05, 2018 11:32 pm

Put a .after call inside the function that you want to be called periodically.
Then it will call itself
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

epkeati
Posts: 3
Joined: Tue Dec 04, 2018 9:22 pm

Re: TKinter loop

Thu Dec 06, 2018 2:03 am

So the code would look like this?

Code: Select all

def function()
	if on
		Do something
	self.after(100, function)
	
root = tk
root.after(100,function)



scotty101
Posts: 3470
Joined: Fri Jun 08, 2012 6:03 pm

Re: TKinter loop

Thu Dec 06, 2018 9:35 am

epkeati wrote:
Thu Dec 06, 2018 2:03 am
So the code would look like this?

Code: Select all

def function()
	if on
		Do something
	self.after(100, function)
	
root = tk
root.after(100,function)


Yeah pretty much. Just don't forget you still need the root.mainloop()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”