John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Help needed to add inputs and outputs to Tkinter Code

Thu Aug 16, 2018 8:32 pm

Hi ,
Im looking for some help with the following code , Im new to programming and need to add inputs and outputs to control a tkinter gui project . Ive build a Secondary Current Injection Tester and looking to add a GUI interface from the raspberry pi to time the operation of over current relays. Im looking to control the stopwatch via the GPIO pins , but measure the time in takes between inputs .

Im wanting to add the following

input 1 = start the timer
input 2 N/O = stop the timer when it goes N/C
input 3 N/C = stop the timer when it goes N/O
input 4 = reset
Output 1 = turn on relay from gpio pin , when input 2 changes state , turn off relay when the reset is pushed
Output 2 = turn on relay from gpio pin , when input 3 changes state , turn off relay when the reset is pushed

Any help would be great to finally give my project a finishing touch by adding this feature

John

Code: Select all

from Tkinter import *
import time
import RPi.GPIO as GPIO

class StopWatch(Frame):  
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()      

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr)
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self): 
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)
    
    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1        
         
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
    
        
def exist():
     root.destroy()
            
def main():
    root = Tk()
    sw = StopWatch(root)
    sw.pack(side=TOP)
    
    Button(root, text='Start', command=sw.Start).pack(side=LEFT)
    Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
    Button(root, text='Quit', command=root.quit).pack(side=LEFT)
    
    root.mainloop()

if __name__ == '__main__':
    main()
/code]

User avatar
neilgl
Posts: 204
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Help needed to add inputs and outputs to Tkinter Code

Fri Aug 17, 2018 11:21 am



gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sat Aug 18, 2018 3:33 pm

Try this... Note the pin numbers used (easiest for me to test on my system)

Comes with no guarantees, try it on your system at your own risk.

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import time
import RPi.GPIO as GPIO
import sys

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 10
input_3 = 8
input_4 = 12

output_1 = 16
output_2 = 18

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( input_1,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 1 START (connect to gnd to START)
GPIO.setup( input_2,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 2 STOP  (connect to gnd to STOP)
GPIO.setup( input_3,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 3 STOP  (DISconnect from gnd to STOP)
GPIO.setup( input_4,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 4 RESET (connect to gnd to RESET)

GPIO.setup(output_1,GPIO.OUT)   # output 1
GPIO.output(output_1, GPIO.LOW) # ensures output 1 OFF
GPIO.setup(output_2,GPIO.OUT)   # output 2
GPIO.output(output_2, GPIO.LOW) # ensures output 2 OFF

class StopWatch(Frame):
    
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr)
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self):
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            GPIO.output(output_1, GPIO.LOW)
            GPIO.output(output_2, GPIO.LOW)
        
        
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
        GPIO.output(output_1, GPIO.LOW)
        GPIO.output(output_2, GPIO.LOW)
        
    def Start_button(self,pin):
        self.Start()
        
    def Stop1_button(self,pin):
        if self._running:
            GPIO.output(output_1, GPIO.HIGH)
        self.Stop()
        
    def Stop2_button(self,pin):
        if self._running:
            GPIO.output(output_2, GPIO.HIGH)
        self.Stop()
        
    def Reset_button(self,pin):
        self.Reset()
        
def exist():
    sys.exit()
            
def main():
    root = Tk()
    sw = StopWatch(root)
    sw.pack(side=TOP)
    Button(root, text='Start', command=sw.Start).pack(side=LEFT)
    Button(root, text='Stop',  command=sw.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
    Button(root, text='Quit',  command=exist).pack(side=LEFT)

    GPIO.add_event_detect(input_1, GPIO.FALLING, callback=sw.Start_button, bouncetime=500)
    GPIO.add_event_detect(input_2, GPIO.FALLING, callback=sw.Stop1_button, bouncetime=500)
    GPIO.add_event_detect(input_3, GPIO.RISING,  callback=sw.Stop2_button, bouncetime=500)
    GPIO.add_event_detect(input_4, GPIO.FALLING, callback=sw.Reset_button, bouncetime=500)
    root.mainloop()

if __name__ == '__main__':
    main()

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sat Aug 18, 2018 5:27 pm

Gordon ,

It works thank you :D much appreciated , one last question ... how do i change the name of the window from tk to "Secondary injection Timer"

Kind Regards

John

gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 9:00 am

Had to modify the sizes etc to fit title...

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import time
import RPi.GPIO as GPIO
import sys

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 10
input_3 = 8
input_4 = 12

output_1 = 16
output_2 = 18

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( input_1,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 1 START (connect to gnd to START)
GPIO.setup( input_2,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 2 STOP  (connect to gnd to STOP)
GPIO.setup( input_3,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 3 STOP  (DISconnect from gnd to STOP)
GPIO.setup( input_4,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 4 RESET (connect to gnd to RESET)

GPIO.setup(output_1,GPIO.OUT)   # output 1
GPIO.output(output_1, GPIO.LOW) # ensures output 1 OFF
GPIO.setup(output_2,GPIO.OUT)   # output 2
GPIO.output(output_2, GPIO.LOW) # ensures output 2 OFF

class StopWatch(Frame):
    
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr, font=("Helvetica", 40))
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self):
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            GPIO.output(output_1, GPIO.LOW)
            GPIO.output(output_2, GPIO.LOW)
        
        
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
        GPIO.output(output_1, GPIO.LOW)
        GPIO.output(output_2, GPIO.LOW)
        
    def Start_button(self,pin):
        self.Start()
        
    def Stop1_button(self,pin):
        if self._running:
            GPIO.output(output_1, GPIO.HIGH)
        self.Stop()
        
    def Stop2_button(self,pin):
        if self._running:
            GPIO.output(output_2, GPIO.HIGH)
        self.Stop()
        
    def Reset_button(self,pin):
        self.Reset()
            
def main():
    root = Tk()
    root.title("Secondary injection Timer")
    root.geometry("350x145")
    sw = StopWatch(root)
    sw.pack(side=TOP)
    Button(root, text='Start', command=sw.Start,    width = 8 , height = 3).pack(side=LEFT)
    Button(root, text='Stop',  command=sw.Stop,     width = 8 , height = 3).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset,    width = 8 , height = 3).pack(side=LEFT)
    Button(root, text='Quit',  command=root.destroy,width = 8 , height = 3).pack(side=LEFT)

    GPIO.add_event_detect(input_1, GPIO.FALLING, callback=sw.Start_button, bouncetime=500)
    GPIO.add_event_detect(input_2, GPIO.FALLING, callback=sw.Stop1_button, bouncetime=500)
    GPIO.add_event_detect(input_3, GPIO.RISING,  callback=sw.Stop2_button, bouncetime=500)
    GPIO.add_event_detect(input_4, GPIO.FALLING, callback=sw.Reset_button, bouncetime=500)
    root.mainloop()

if __name__ == '__main__':
    main()




John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 1:40 pm

Hi Again :)

Im now trying to run this script using a superscript from when the pi boots up and im getting ./superscript PERMISSION DENIED .

I've setup a superscript using " sudo nano superscript".......
Then copied and pasted the timer script into this and saved it ......
Then opened .bashrc and added ./superscript at the bottom........

What am i missing here ? Also is it possible to make the script fit the full screen when opened automatically . I'm looking to use a 3.5 or 4inch touchscreen and mount this in my tester with a bezel and would prefer nothing else showing on the screen apart from the timer application

Kind Regards
John

gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 2:09 pm

Do you know the resolution of the screen?. My 3.5" is 480x320.

We could resize to suit.

I've never used superscript, are you booting to the GUI? If so you could add a line item to /home/pi/.config/lxsession/LXDE-pi/autostart
Eg

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@/usr/bin/python3 /home/pi/stopwatch.py
Last edited by gordon77 on Sun Aug 19, 2018 2:14 pm, edited 1 time in total.

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 2:13 pm

Heres the tester , im looking at replacing the existing timer for a 3.5" screen , which ill be buying today
tester.jpg
tester.jpg (109.94 KiB) Viewed 477 times
The attachment tester.jpg is no longer available

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 2:14 pm

tester2.jpg
tester2.jpg (150.5 KiB) Viewed 475 times

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 2:21 pm

Hi ,

Yes ill be booting straight to gui on the screen , when the power is applied im using the attached board to sense 230v and generate an input to the pi to start the timer , when the rotary switch is turned on
board.png
board.png (135.61 KiB) Viewed 471 times

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 2:27 pm

Not sure of the resolution as of yet , ill buy one today and look at the spec and update you

gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 3:01 pm

this fits my 3.5" TFT plus from adafruit (480x320) https://www.adafruit.com/product/2441

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import time
import RPi.GPIO as GPIO
import sys

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 10
input_3 = 8
input_4 = 12

output_1 = 16
output_2 = 18

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( input_1,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 1 START (connect to gnd to START)
GPIO.setup( input_2,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 2 STOP  (connect to gnd to STOP)
GPIO.setup( input_3,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 3 STOP  (DISconnect from gnd to STOP)
GPIO.setup( input_4,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 4 RESET (connect to gnd to RESET)

GPIO.setup(output_1,GPIO.OUT)   # output 1
GPIO.output(output_1, GPIO.LOW) # ensures output 1 OFF
GPIO.setup(output_2,GPIO.OUT)   # output 2
GPIO.output(output_2, GPIO.LOW) # ensures output 2 OFF

class StopWatch(Frame):
    
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr, font=("Helvetica", 80))
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self):
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            GPIO.output(output_1, GPIO.LOW)
            GPIO.output(output_2, GPIO.LOW)
        
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        #if self._running:
        self.after_cancel(self._timer)            
        self._elapsedtime = time.time() - self._start    
        if self._running:
            self._setTime(self._elapsedtime)
        self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
        GPIO.output(output_1, GPIO.LOW)
        GPIO.output(output_2, GPIO.LOW)
        
    def Start_button(self,pin):
        self.Start()
        
    def Stop1_button(self,pin):
        if self._running:
            GPIO.output(output_1, GPIO.HIGH)
        self.Stop()
        
    def Stop2_button(self,pin):
        if self._running:
            GPIO.output(output_2, GPIO.HIGH)
        self.Stop()
        
    def Reset_button(self,pin):
        self.Reset()

def main():
    root = Tk()
    root.title("Secondary injection Timer")
    root.geometry("480x288")
    sw = StopWatch(root)
    sw.pack(side=TOP)
    Button(root, text='Start', command=sw.Start,    width = 12 , height = 3).pack(side=LEFT)
    Button(root, text='Stop',  command=sw.Stop,     width = 12 , height = 3).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset,    width = 12 , height = 3).pack(side=LEFT)
    Button(root, text='Quit',  command=root.destroy,width = 12 , height = 3).pack(side=LEFT)

    GPIO.add_event_detect(input_1, GPIO.FALLING, callback=sw.Start_button, bouncetime=500)
    GPIO.add_event_detect(input_2, GPIO.FALLING, callback=sw.Stop1_button, bouncetime=500)
    GPIO.add_event_detect(input_3, GPIO.RISING,  callback=sw.Stop2_button, bouncetime=500)
    GPIO.add_event_detect(input_4, GPIO.FALLING, callback=sw.Reset_button, bouncetime=500)
    root.mainloop()

if __name__ == '__main__':
    main()



gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 4:19 pm

I have noticed on my setup that it will occasionally go into a mode where you can't STOP the stopwatch, I believe this maybe to do with my switches.

If this happens try this version.. Note it also uses the fullscreen attribute instead of setting the size.

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import time
import RPi.GPIO as GPIO
import sys

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 10
input_3 = 8
input_4 = 12

output_1 = 16
output_2 = 18

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( input_1,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 1 START (connect to gnd to START)
GPIO.setup( input_2,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 2 STOP  (connect to gnd to STOP)
GPIO.setup( input_3,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 3 STOP  (DISconnect from gnd to STOP)
GPIO.setup( input_4,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 4 RESET (connect to gnd to RESET)

GPIO.setup(output_1,GPIO.OUT)   # output 1
GPIO.output(output_1, GPIO.LOW) # ensures output 1 OFF
GPIO.setup(output_2,GPIO.OUT)   # output 2
GPIO.output(output_2, GPIO.LOW) # ensures output 2 OFF

class StopWatch(Frame):
    
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr, font=("Helvetica", 80))
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self):
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            GPIO.output(output_1, GPIO.LOW)
            GPIO.output(output_2, GPIO.LOW)
        
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        #if self._running:
        self.after_cancel(self._timer)            
        #self._elapsedtime = time.time() - self._start    
        self._setTime(self._elapsedtime)
        self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
        GPIO.output(output_1, GPIO.LOW)
        GPIO.output(output_2, GPIO.LOW)
        
    def Start_button(self,pin):
        self.Start()
        
    def Stop1_button(self,pin):
        if self._running:
            GPIO.output(output_1, GPIO.HIGH)
        self.Stop()
        
    def Stop2_button(self,pin):
        if self._running:
            GPIO.output(output_2, GPIO.HIGH)
        self.Stop()
        
    def Reset_button(self,pin):
        self.Reset()

def main():
    root = Tk()
    root.title("Secondary injection Timer")
    #root.geometry("480x288")
    root.attributes("-fullscreen", True)
    sw = StopWatch(root)
    sw.pack(side=TOP)
    Button(root, text='Start', command=sw.Start,    width = 12 , height = 3).pack(side=LEFT)
    Button(root, text='Stop',  command=sw.Stop,     width = 12 , height = 3).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset,    width = 12 , height = 3).pack(side=LEFT)
    Button(root, text='Quit',  command=root.destroy,width = 12 , height = 3).pack(side=LEFT)

    GPIO.add_event_detect(input_1, GPIO.FALLING, callback=sw.Start_button, bouncetime=500)
    GPIO.add_event_detect(input_2, GPIO.FALLING, callback=sw.Stop1_button, bouncetime=500)
    GPIO.add_event_detect(input_3, GPIO.RISING,  callback=sw.Stop2_button, bouncetime=500)
    GPIO.add_event_detect(input_4, GPIO.FALLING, callback=sw.Reset_button, bouncetime=500)
    root.mainloop()

if __name__ == '__main__':
    main()



John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Sun Aug 19, 2018 6:37 pm

I did notice that , but thought the pi had locked up . I've tried the latest and works a treat . just adjusted the size of the buttons to fit the size and just wondering is the an easier way from what i've done . Tried to expand the buttons to fit the width on the screen and its slightly out (on the right hand side , is there some way of centralizing them ?

I've ordered the screen from the link so , should all be ready to go when it arrives :D

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import time
import RPi.GPIO as GPIO
import sys

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 10
input_3 = 8
input_4 = 12

output_1 = 16
output_2 = 18

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( input_1,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 1 START (connect to gnd to START)
GPIO.setup( input_2,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 2 STOP  (connect to gnd to STOP)
GPIO.setup( input_3,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 3 STOP  (DISconnect from gnd to STOP)
GPIO.setup( input_4,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 4 RESET (connect to gnd to RESET)

GPIO.setup(output_1,GPIO.OUT)   # output 1
GPIO.output(output_1, GPIO.LOW) # ensures output 1 OFF
GPIO.setup(output_2,GPIO.OUT)   # output 2
GPIO.output(output_2, GPIO.LOW) # ensures output 2 OFF

class StopWatch(Frame):
    
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr, font=("Helvetica", 150))
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self):
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            GPIO.output(output_1, GPIO.LOW)
            GPIO.output(output_2, GPIO.LOW)
        
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        #if self._running:
        self.after_cancel(self._timer)            
        #self._elapsedtime = time.time() - self._start    
        self._setTime(self._elapsedtime)
        self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
        GPIO.output(output_1, GPIO.LOW)
        GPIO.output(output_2, GPIO.LOW)
        
    def Start_button(self,pin):
        self.Start()
        
    def Stop1_button(self,pin):
        if self._running:
            GPIO.output(output_1, GPIO.HIGH)
        self.Stop()
        
    def Stop2_button(self,pin):
        if self._running:
            GPIO.output(output_2, GPIO.HIGH)
        self.Stop()
        
    def Reset_button(self,pin):
        self.Reset()

def main():
    root = Tk()
    root.title("Secondary Injection Timer")
    #root.geometry("480x288")
    root.attributes("-fullscreen", True)
    sw = StopWatch(root)
    sw.pack(side=TOP)
    Button(root, text='Start', command=sw.Start,    width = 21 , height = 3).pack(side=LEFT)
    Button(root, text='Stop',  command=sw.Stop,     width = 21 , height = 3).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset,    width = 21 , height = 3).pack(side=LEFT)
    Button(root, text='Quit',  command=root.destroy,width = 21 , height = 3).pack(side=LEFT)

    GPIO.add_event_detect(input_1, GPIO.FALLING, callback=sw.Start_button, bouncetime=500)
    GPIO.add_event_detect(input_2, GPIO.FALLING, callback=sw.Stop1_button, bouncetime=500)
    GPIO.add_event_detect(input_3, GPIO.RISING,  callback=sw.Stop2_button, bouncetime=500)
    GPIO.add_event_detect(input_4, GPIO.FALLING, callback=sw.Reset_button, bouncetime=500)
    root.mainloop()

if __name__ == '__main__':
    main()


gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Mon Aug 20, 2018 10:42 am

Code: Select all

#!/usr/bin/env python3
from tkinter import *
import tkinter as tk
import time
import RPi.GPIO as GPIO
import sys

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 10
input_3 = 8
input_4 = 12

output_1 = 16
output_2 = 18

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( input_1,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 1 START (connect to gnd to START)
GPIO.setup( input_2,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 2 STOP  (connect to gnd to STOP)
GPIO.setup( input_3,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 3 STOP  (DISconnect from gnd to STOP)
GPIO.setup( input_4,GPIO.IN,pull_up_down = GPIO.PUD_UP)   # input 4 RESET (connect to gnd to RESET)

GPIO.setup(output_1,GPIO.OUT)   # output 1
GPIO.output(output_1, GPIO.LOW) # ensures output 1 OFF
GPIO.setup(output_2,GPIO.OUT)   # output 2
GPIO.output(output_2, GPIO.LOW) # ensures output 2 OFF

class StopWatch(Frame):
    
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr, font=("Helvetica", 80))
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=10, padx=10)                      
    
    def _update(self):
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(10, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
        
    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1
            GPIO.output(output_1, GPIO.LOW)
            GPIO.output(output_2, GPIO.LOW)
        
    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        #if self._running:
        self.after_cancel(self._timer)            
        #self._elapsedtime = time.time() - self._start    
        self._setTime(self._elapsedtime)
        self._running = 0
    
    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)
        GPIO.output(output_1, GPIO.LOW)
        GPIO.output(output_2, GPIO.LOW)
        
    def Start_button(self,pin):
        self.Start()
        
    def Stop1_button(self,pin):
        if self._running:
            GPIO.output(output_1, GPIO.HIGH)
        self.Stop()
        
    def Stop2_button(self,pin):
        if self._running:
            GPIO.output(output_2, GPIO.HIGH)
        self.Stop()
        
    def Reset_button(self,pin):
        self.Reset()

def main():
    root = Tk()
    root.title("Secondary injection Timer")
    #root.geometry("480x288")
    root.attributes("-fullscreen", True)
    sw = StopWatch(root)
    sw.pack(side=TOP)
    back= tk.Frame(master=root, width=480, height=56, bg='grey')
    back.pack_propagate(0)
    back.pack(fill="none", expand=True)
    Button(master = back, text='Start', command=sw.Start,    width = 12 , height = 3,bg="green").pack(side=LEFT)
    Button(master = back, text='Stop',  command=sw.Stop,     width = 12 , height = 3,bg="red").pack(side=LEFT)
    Button(master = back, text='Reset', command=sw.Reset,    width = 12 , height = 3,bg="yellow").pack(side=LEFT)
    Button(master = back, text='Quit',  command=root.destroy,width = 12 , height = 3,bg="grey").pack(side=LEFT)

    GPIO.add_event_detect(input_1, GPIO.FALLING, callback=sw.Start_button, bouncetime=500)
    GPIO.add_event_detect(input_2, GPIO.FALLING, callback=sw.Stop1_button, bouncetime=500)
    GPIO.add_event_detect(input_3, GPIO.RISING,  callback=sw.Stop2_button, bouncetime=500)
    GPIO.add_event_detect(input_4, GPIO.FALLING, callback=sw.Reset_button, bouncetime=500)
    root.mainloop()

if __name__ == '__main__':
    main()




gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 9:00 am

I tried the setup with the adfruit 3.5" tft to prove the GPIOs still work. They do.

I used

# GPIO BOARD PIN numbers
input_1 = 7
input_2 = 11
input_3 = 12
input_4 = 13

output_1 = 15
output_2 = 16
Attachments
stopwatch3s.jpg
stopwatch3s.jpg (39.3 KiB) Viewed 374 times

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 1:32 pm

Hi Gordon

Thanks for the Image , I've purchased a 2.8" display , due to limited space on my tester , what do i need to change to fit it all on the 2.8" display .

John

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 4:02 pm

2.8 screen.jpg
2.8 screen.jpg (65.33 KiB) Viewed 353 times

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 4:22 pm

screen1.jpg
screen1.jpg (23.89 KiB) Viewed 350 times
Managed to sort it however , im having issues now after loading all the kernals to make the screen work. My desktop now has a password , which is causing issues with the auto boot script and my touch screen doesn't seem to work any ideas .

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 4:26 pm

guiboot.jpg
guiboot.jpg (25.17 KiB) Viewed 344 times
This is the new screen i seem to be getting when the pi boots

gordon77
Posts: 3413
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 5:53 pm

I haven't used that kernel. Can you choose auto login in the pi preferences menu?

John2809
Posts: 32
Joined: Thu Aug 02, 2018 9:49 pm

Re: Help needed to add inputs and outputs to Tkinter Code

Tue Aug 21, 2018 6:30 pm

I’m going to try downloading the latest img and hope for the best , let’s hope it fixes the touch screen issue also . Tried the menu options and doesn’t do anything .

I have a 7” touch screen and it worked fine before I replaced it with the 2.8” Adafruit display .

Return to “Beginners”