hi all i am new to raspberry. pls help me with coding timer (100 msec) , switch input interrupt
how can i display 2 numbers on the hdmi screen 1'' high and 4'' high ( example 54 on 2 different size)
any help is appreciated
harry P
Code: Select all
# python3 and tk
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
myfont = ('times', 132, 'bold')
self.hi_there["text"] = "54 Hello World\n(click me)"
self.hi_there.config(font = myfont)
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
newfont = ('Helvetica', 64, 'bold')
self.quit = tk.Button(self, text="QUIT", fg="red",command=root.destroy)
self.quit.config(font = newfont)
self.quit.pack(side="bottom")
def say_hi(self):
self.hi_there["text"] = "54 Hello World\n(clicked)"
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Code: Select all
nano big.py
Code: Select all
python3 big.py
Code: Select all
import RPi.GPIO as GPIO
import pygame, sys
import time
from pygame.locals import *
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
switch = 11 # pin 11, switch to +3.3v
GPIO.setup(switch,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
redColor = pygame.Color(255,0,0)
greyColor = pygame.Color(50,50,50)
pygame.init()
font_size = 200
width = font_size * 6
height = font_size
windowSurfaceObj = pygame.display.set_mode((width,height),1,24)
pygame.display.set_caption('STOPWATCH')
fontObj = pygame.font.Font('freesansbold.ttf',font_size)
msg = "00:00:00:000"
msgSurfaceObj = fontObj.render(msg, False,redColor)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(2,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update()
try:
while GPIO.input(switch)==0:
down = 0
print (" Ready")
down = 0
while True :
if GPIO.input(switch)==1:
if down == 0:
start = time.time()
now = time.time() - start
m,s = divmod(now,60)
h,m = divmod(m,60)
msg= "%02d:%02d:%02d" % (h,m,s)
psec = str(now-int(now))
pstr = psec[1:5]
msg = msg + str(pstr)
pygame.draw.rect(windowSurfaceObj,greyColor,Rect(0,0,width,height))
msgSurfaceObj = fontObj.render(msg, False,redColor)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(2,0)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update()
down = 1
if GPIO.input(switch)==1:
down = 0
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
except KeyboardInterrupt:
print (" Quit")
GPIO.cleanup()
Code: Select all
# python3 and tk
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
switch = 17 # pin 11, switch to +3.3v
GPIO.setup(switch,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
global running
running = 0
self.master.title('Stopwatch')
self.hi_there = tk.Button(self)
myfont = ('times', 132, 'bold')
self.hi_there["text"] = "00:00:00:00"
self.hi_there.config(font = myfont)
self.hi_there["command"] = self.start_clock
self.hi_there.pack(side="top")
newfont = ('Helvetica', 64, 'bold')
self.quit = tk.Button(self, text="QUIT", fg="red",command=root.destroy)
self.quit.config(font = newfont)
self.quit.pack(side="bottom")
L1 = tk.Label(self.hi_there, text="Click on time, or toggle GPIO #17, to start / stop " , font = 30)
L1.place(x=20, y=180)
self.check_switches()
def start_clock(self):
global start,running
start = time.time()
if running == 0:
running = 1
elif running == 1:
running = 0
self.update_clock()
def update_clock(self):
if running == 1:
now = time.time() - start
m,s = divmod(now,60)
h,m = divmod(m,60)
psec = str(now-int(now))
pstr = psec[1:4]
msg= "%02d:%02d:%02d" % (h,m,s)
msg = msg + str(pstr)
self.hi_there["text"] = msg
self.after(10, self.update_clock)
def check_switches(self):
global switch, start, running
if GPIO.input(switch) == 1 and running == 0:
start = time.time()
running = 1
self.update_clock()
elif GPIO.input(switch) == 1 and running == 1:
running = 0
self.after(100, self.check_switches)
root = tk.Tk()
app = Application(master=root)
app.mainloop()