Page 1 of 1

Get ting a spinbox value

Posted: Thu May 25, 2017 11:59 pm
by bserce123
For a goodly time, our Raspi has been driving a SSR to power on and off heaters, using tkinter and GPIO in a python script. We now need to use a spinbox to control the duration (1 to 4 hours) the heater will be switched on for, which seems easy enough, but when it is ON, and from another tk frame, we want to be told what the spinbox setting is. We declare in the first frame . . . global spinset and spinset=w1.get(), where w1 is the name of the spinbox. When we use a label in the second TK frame and use 'text='Heater will be on for ' + spinset + 'hour(s)', this does show, but only the default 1-hour value present at the beginning and not what it was later changed to be. I've tried to find some event to trigger a proper response but am horribly lost. Can anyone please put me in touch with actual code that has a chance of working, as a beginner I get stranded and lost in the complexities of the language.

Re: Get ting a spinbox value

Posted: Fri May 26, 2017 8:49 am
by scotty101
You need to share your code so that we can help.

Please remember to put your code inside code tags.

Code: Select all

print("This is inside code tags")
while True:
    print("Please use them whenever you post code")

Re: Get ting a spinbox value

Posted: Fri May 26, 2017 1:22 pm
by bserce123
[quote="scotty101"]You need to share your code so that we can help.
Thanks very much. This is how far I have got. I'm sure there's a lot of tidying up to be done, but I'm sadly stuck. No need to waste your time if you can just point to existing code and think a bit of directed study on the net or anywhere will teach me.

Code: Select all

#! /usr/bin/ python3 /home/pi/pyscripts/PowerOn_Off.py

#this app is simply to control a solid-state relay remotely, using VNCConnect
#it uses GPIO 24 as the controlling signaller

import sys
import datetime
import glob
import subprocess
import time  #needed for time.sleep()
import RPi.GPIO as GPIO            # import RPi.GPIO module  
from tkinter import *
from tkinter import ttk
from sys import exit


strt=datetime.datetime.now()
today=datetime.date.today()

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)

global offwego
offwego='aaaa'



def TurnPowerON():
    GPIO.output(24,1)
    running()
    return 

def TurnPowerOFF():
    GPIO.output(24,0)
    sayoff()
    return 


def Start():
    global root2
    root2=Tk()
    root2.title('Water pump control')
    root2.geometry('350x150+5+50')
    w=Label(root2, text='Pump will auto-start at 11 am')
    w.config(font=('times',12, 'italic bold'), fg='red', bg='yellow')
    w.pack(pady=5)     
    w=Label(root2, text='Set running time (hours)')
    w.config(font=('times',12, 'italic bold'), fg='red', bg='yellow')    
    w.pack(pady=5)
    w1=Spinbox(root2, from_=1, to=4, width=2)
    w1.pack(ipadx=3,ipady=7)
    w=Button(root2, text='Override: Turn pump ON now)', command=TurnPowerON)
    w.config(font=('Arial',12,'bold'),fg='green')
    w.pack(pady=5)
    global spinsee
    spinsee=w1.get()
    root2.mainloop()
    return
 
def running():
    root2.destroy
    rr=Tk()
    rr.title('Pump running control')
    rr.geometry('550x150+5+50')
    w=Label(rr, text='Pump running for ' + spinsee +' hour(s)') 
    w.config(font=('times',12, 'italic bold'), fg='red', bg='yellow')
    w.pack(pady=5)     
    w=Button(root2, text='Override: Turn OFF & Quit', command=stoppit)
    w.config(font=('Arial',12,'bold'),fg='green')
    w.pack(side=RIGHT, padx=2,pady=5)
    #rr.after(10000,rr.destroy)
    rr.mainloop()
    return

def stoppit():  
    global offwego
    offwego='bbbb'
    return
    
    
while offwego=='aaaa':  # also tried 'while True:' to get Quit button to work 
    if offwego=='bbbb':  
         GPIO.cleanup() #nb need for GPIO.cleanup()
         exit(0)

    Start()
    


Re: Get ting a spinbox value

Posted: Sat May 27, 2017 11:47 pm
by bserce123
Solved!
Instead of calling the Power-ON function directly from the button, I tried using the command= bit first to call a different function - one to get the Spinbox setting, make this result global, and go on to call the PowerON function from that function. Thank you to anyone who tried to fathom out what on earth was needed, I apologise for taking your time.