QPushButton / GPIO on-delay - python

I am trying to create a GUI for touchscreen light controller for RPi using python. So far I have three QPushButtons on my GUI; one for on/off (single click), one for dimming (long press) and one for led mode change (single click). Each of these control corresponding GPIO to control relay.
For the dimming, my LED requires long signal. If this signal is too short, LED driver considers this as a click and it will work as a on/off switch, which I don't want to happen. For this I need to create some type of script that checks that the QPushButton (name: btn_dim) is pressed longer than x amounts of seconds (let's say 0.5 sec) and then perform desired function (turn GPIO on). Icon on the button should also change when the button is pressed even if the press is shorter than 0.5 sec.
Here's what I got so far:
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.btn_close.clicked.connect(self.appClose)
self.btn_dim.pressed.connect(self.dimOn)
self.btn_dim.released.connect(self.dimOff)
self.iconDIM_off = QtGui.QIcon()
self.iconDIM_off.addPixmap(QtGui.QPixmap("icons/led/LED-DIM-OFF.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.iconDIM_on = QtGui.QIcon()
self.iconDIM_on.addPixmap(QtGui.QPixmap("icons/led/LED-DIM-ON.png"))
# App close
def appClose(self):
GPIO.cleanup()
sys.exit()
def dimOn(self):
self.btn_dim.setIcon(self.iconDIM_on)
t_start = time.perf_counter()
print(t_start)
time.sleep(0.5)
if self.btn_dim.isChecked:
t_end = time.perf_counter()
print(t_end)
t_press = (t_end - t_start)
print(t_press)
if t_press > 0.5:
GPIO.output(5, 0)
def dimOff(self):
self.btn_dim.setIcon(self.iconDIM_off)
GPIO.output(5, 1)
Now what happens is:
Icon changes after 0.5 sec, not immediately
Even with a click, GPIO turns on after 0.5 sec and immediately off
How do I modify this, so that the icon changes even with a click/short press, but the GPIO turns on only if the press is longer than 0.5 sec.

Got it work with this:
# Dimmer delay to differentiate from click
def dimmerTimer(self):
time.sleep(0.25)
start=self.pressed
end=self.released
press_time=(end-start)
if press_time < 0:
GPIO.output(13, 0)
else:
GPIO.output(13, 1)
# Dimmer pushbutton pressed function
def dimPressed(self):
self.btn_dim.setIcon(self.iconDIM_on)
self.pressed = time.perf_counter()
timer = Thread(target=self.dimmerTimer)
timer.start()
# Dimmer pushbutton released function
def dimReleased(self):
self.btn_dim.setIcon(self.iconDIM_off)
self.released = time.perf_counter()
time.sleep(0.35)
GPIO.output(13, 1)
There's probably better or right way to do this, but this did the trick for me. Hopefully it's helpful to someone!

Related

I cant update two window at the same time - countdown

I am writing again because of a problem that I cannot solve, I have a main window which has a timer in a label, at the same time it starts a toplevel with another timer in other label, my idea is that the 2 timers start when you click the button on the main window, but I can't figure it out, it just starts the one in the main window.
self.is_running=False
self.tablero_principal = window
self.tablero_principal.title('Tablero')
#SECOND WINDOW TOPLEVEL
self.control_wind = customtkinter.CTkToplevel()
self.control_wind.title('Posesion')
def countdown(self,segundos,posesion):
if self.is_running:
segundos_final=int(segundos)
minutos_final=int(self.text_minutos.get())
posesion_final=int(posesion)
self.text_minutos.set(f"{minutos_final:02d}")
self.text_segundos.set(f"{segundos_final:02d}")
if segundos_final > 0:
self.tablero_principal.after(1000, self.countdown, segundos_final-1,posesion_final-1)
def iniciar(self):
if not self.is_running:
self.countdown(self.text_segundos.get(),self.text_segundos_posesion.get())
def parar(self):
self.is_running = False

Issue synchronizing multiple countdown timers with Python Tkinter

I'll start by saying that I'm completely new to tkinter and I've been trying to wrap my head around a minor issue I'm experiencing - it might be very simple/I'm approaching it incorrectly, but some insight would be very appreciated!
Essentially I have an app with multiple items, each item has a button. When you click a button for an item, it starts a countdown timer by calculating the duration in a function, then using .after(1000, ...) to refresh my label every second.
The issue is, when you click on a button and start a timer, it counts down every second from when the button is clicked. So if you click the next button "off cycle" from the last countdown, the countdown is NOT SYNCED up the way it should be.
Currently it counts down a second from when the button is pressed, instead of updating every "true second", if that makes sense. Any idea how I can approach this?
def handle_growth_time(n, harvest_time):
time_now = datetime.datetime.now()
time_now = time_now.replace(microsecond=0)
crop = allotments[n]
if not crop["reset"]:
if harvest_time >= time_now:
remaining_time = harvest_time - time_now
crop["remaining_label"].configure(text=remaining_time, font='ariel 12', foreground="black")
crop["remaining_label"].after(1000, handle_growth_time, n, harvest_time)
else:
crop["label_name"].configure(background="#86d474")
crop["active"] = False
else:
crop["reset"] = False
crop["active"] = False
I have tried a couple different approaches, such as when I capture time_now I grab time_now.microseconds/1000 and waiting for that before starting the countdown but I think working with time in that way is unreliable because I still can't really capture true :00.00
I also tried resetting all the timers when a new one is started, refreshing their 1 sec cycle, but I did so by going through an array and restarting them - as you can imagine this causes them to be offset still.
I recommend having only a single timekeeper that keeps track of passing seconds. Each timer can have a tick function that is called by this single timer. It is then responsible for updating all timers at the same time so that the update at the same time.
The following example illustrates the point. It's not production quality code, but it illustrates the point: multiple timer objects that all "tick" during a single time loop.
import tkinter as tk
class Timer(tk.Label):
def __init__(self, parent, seconds):
super().__init__(parent, text=str(seconds), width=8)
self.time = seconds
self.running = False
def tick(self):
self.time -= 1
self.configure(text=self.time)
if self.time == 0:
self.running = False
def start(self):
self.running = True
def stop(self):
self.running = False
class Timekeeper:
def __init__(self):
self.timers = []
def new_timer(self, seconds):
t = Timer(root, seconds)
t.pack(side="top", fill="x")
self.timers.append(t)
t.start()
def tick(self):
root.after(1000, self.tick)
# update existing timers
for timer in self.timers:
if timer.running:
timer.tick()
timekeeper = Timekeeper()
root = tk.Tk()
root.geometry("200x600")
button_15 = tk.Button(root, text="New 15 second timer", command=lambda: timekeeper.new_timer(15))
button_30 = tk.Button(root, text="New 30 second timer", command=lambda: timekeeper.new_timer(30))
button_15.pack(side="top")
button_30.pack(side="top")
# add a few timers as an example
timekeeper.new_timer(15)
timekeeper.new_timer(30)
timekeeper.new_timer(45)
timekeeper.new_timer(60)
root.after(1000, timekeeper.tick)
root.mainloop()

Pause the code at some point

I want to pause the code at exact position and wait for the different input plus the start button clicked. However, if it is not achievable, how can I add another button into the start button to make it work?
import wx
import time
import RPi.GPIO as GPIO
global Total_Length
Total_Length = 500
global Input_Length
Input_Length = 0
a = 0
class tyler(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Lyquid Crystal Laser Control',size=(500,200))
panel=wx.Panel(self)
#global Text_1
self.Text_1 = wx.TextCtrl(panel,-1,"0",(350,30),(50,30))
self.Text_1.Bind(wx.EVT_TEXT_ENTER,self.Start)
self.Text_1.Bind(wx.EVT_KILL_FOCUS,self.Start)
self.timer = wx.Timer(self)
#self.Bind(wx.EVT_TIMER, self.timer)
button_1=wx.Button(panel,label="Start",pos=(400,80),size=(80,30))
button_2=wx.Button(panel,label="Stop",pos=(400,120),size=(80,30))
self.Bind(wx.EVT_BUTTON, self.Start, button_1)
#self.Bind(wx.EVT_BUTTON, self.Stop, button_2)
def Start(self,event):
global a
Input_Length=float(self.Text_1.GetValue())
#print(Input_Length)
#a = Input_Length
#print(Input_Length)
dc=float(100*Input_Length/Total_Length)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.setwarnings(False)
p = GPIO.PWM(18,1150)
p.start(0)
p.ChangeDutyCycle(dc)
p.ChangeFrequency(1150)
#I wanted to pause the code at here, until the input changes, and the start button clicked, so I add timer in below, however, the output is only a pulse but the square wave is what I wanted
if a == dc:
self.timer.Start(1000)
else:
a = dc
self.timer.Stop()
#def Stop(self,event):
GPIO.cleanup()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=tyler(parent=None,id=-1)
frame.Show()
app.MainLoop()
"Pause and wait" and "event-driven GUI programming" do not go together. As long as the main GUI thread is blocked waiting for something, then other events can't be processed and the program will appear to be frozen. Your options are to change how you "wait" (by not actually waiting) or to use another thread.
This answer to another question applies just as well here, and will give you more information and pointers.

How can I show/hide toolbar depending on mouse movements and mouse position inside window?

Hi Iam using Python and GTK+. In my GUI I have 2 toolbars I want show first toolbar only if user moves mouse than hide it again after few seconds as for second toolbar I want to show it when user is on particular x,y coordinates.How can I achieve it ?
EDIT:
Iam creating some kind of media player so I want toolbars to disapear while user is not using mouse in case of playerMenu toolbar or if user doesn't move it to specific location in case of ribbonBar toolbar .Iam using GTK+ here is my code for toolbars:
class Player(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
def build_UI(self):
container=Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
ribbonBar=Gtk.Toolbar()
playerMenu=Gtk.Toolbar()
def mouse_moved(self):
#TO-DO here I should check cordinates for example I want to see if mouse.y=window.height-50px and I would like to show ribbonaBar
#after that Gdk.threads_add_timeout(1000,4000,ribbonBar.hide)
#TO-DO here I show playerMenu toolbar if mouse is moved
# smt like playerMenu.show()
#after that I would call Gdk.threads_add_timeout(1000,4000,playerMenu.hide)
# to hide it again after 4 seconds
I should connect my window to some mouse event but I don't know the event name and how can I get mouse.x and mouse.y?
Why do you want to do this? Trying to use widgets that disappear when you're not moving the mouse is rather annoying, IMHO.
But anyway...
To toggle the visibility of a widget use the show() and hide() methods, or map() and unmap() if you don't want the other widgets in your window to move around. To handle timing, use gobject.timeout_add(), and you'll need to connect() your window to "motion_notify_event" and set the appropriate event masks: gtk.gdk.POINTER_MOTION_MASK and probably gtk.gdk.POINTER_MOTION_HINT_MASK. The Event object that your motion_notify callback receives will contain x,y mouse coordinates.
At least, that's how I'd do it in GTK2; I don't know GTK3.
If you want more specific help you need to post some code.
I see that you've posted some code, but it doesn't have a lot of detail... But I understand that GTK can be a bit overwhelming. I haven't used it much in the last 5 years, so I'm a bit rusty, but I just started getting into it again a couple of months ago and thought your question would give me some good practice. :)
I won't claim that the code below is the best way to do this, but it works. And hopefully someone who is a GTK expert will come along with some improvements.
This program builds a simple Toolbar with a few buttons. It puts the Toolbar into a Frame to make it look nicer, and it puts the Frame into an Eventbox so we can receive events for everything in the Frame, i.e., the Toolbar and its ToolItems. The Toolbar only appears when the mouse pointer isn't moving and disappears after a few seconds, unless the pointer is hovering over the Toolbar.
This code also shows you how to get and process mouse x,y coordinates.
#!/usr/bin/env python
''' A framed toolbar that disappears when the pointer isn't moving
or hovering in the toolbar.
A response to the question at
http://stackoverflow.com/questions/26272684/how-can-i-show-hide-toolbar-depending-on-mouse-movements-and-mouse-position-insi
Written by PM 2Ring 2014.10.09
'''
import pygtk
pygtk.require('2.0')
import gtk
import gobject
if gtk.pygtk_version < (2, 4, 0):
print 'pygtk 2.4 or better required, aborting.'
exit(1)
class ToolbarDemo(object):
def button_cb(self, widget, data=None):
#print "Button '%s' %s clicked" % (data, widget)
print "Button '%s' clicked" % data
return True
def show_toolbar(self, show):
if show:
#self.frame.show()
self.frame.map()
else:
#self.frame.hide()
self.frame.unmap()
def timeout_cb(self):
self.show_toolbar(self.in_toolbar)
if not self.in_toolbar:
self.timer = False
return self.in_toolbar
def start_timer(self, interval):
self.timer = True
#Timer will restart if callback returns True
gobject.timeout_add(interval, self.timeout_cb)
def motion_notify_cb(self, widget, event):
if not self.timer:
#print (event.x, event.y)
self.show_toolbar(True)
self.start_timer(self.time_interval)
return True
def eventbox_cb(self, widget, event):
in_toolbar = event.type == gtk.gdk.ENTER_NOTIFY
#print event, in_toolbar
self.in_toolbar = in_toolbar
#### self.show_toolbar(in_toolbar) does BAD things :)
if in_toolbar:
self.show_toolbar(True)
return True
def quit(self, widget): gtk.main_quit()
def __init__(self):
#Is pointer over the toolbar Event box?
self.in_toolbar = False
#Is pointer motion timer running?
self.timer = False
#Time in milliseconds after point stops before toolbar is hidden
self.time_interval = 3000
self.window = win = gtk.Window(gtk.WINDOW_TOPLEVEL)
width = gtk.gdk.screen_width() // 2
height = gtk.gdk.screen_height() // 5
win.set_size_request(width, height)
win.set_title("Magic Toolbar demo")
win.set_border_width(10)
win.connect("destroy", self.quit)
#self.motion_handler = win.connect("motion_notify_event", self.motion_notify_cb)
win.connect("motion_notify_event", self.motion_notify_cb)
win.add_events(gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.POINTER_MOTION_HINT_MASK)
box = gtk.VBox()
box.show()
win.add(box)
#An EventBox to capture events inside Frame,
# i.e., for the Toolbar and its child widgets.
ebox = gtk.EventBox()
ebox.show()
ebox.set_above_child(True)
ebox.connect("enter_notify_event", self.eventbox_cb)
ebox.connect("leave_notify_event", self.eventbox_cb)
box.pack_start(ebox, expand=False)
self.frame = frame = gtk.Frame()
frame.show()
ebox.add(frame)
toolbar = gtk.Toolbar()
#toolbar.set_border_width(5)
toolbar.show()
frame.add(toolbar)
def make_toolbutton(text):
button = gtk.ToolButton(None, label=text)
#button.set_expand(True)
button.connect('clicked', self.button_cb, text)
button.show()
return button
def make_toolsep():
sep = gtk.SeparatorToolItem()
sep.set_expand(True)
#sep.set_draw(False)
sep.show()
return sep
for i in xrange(5):
button = make_toolbutton('ToolButton%s' % (chr(65+i)))
toolbar.insert(button, -1)
#toolbar.insert(make_toolsep(), -1)
for i in xrange(1, 9, 2):
toolbar.insert(make_toolsep(), i)
button = gtk.Button('_Quit')
button.show()
box.pack_end(button, False)
button.connect("clicked", self.quit)
win.show()
frame.unmap()
def main():
ToolbarDemo()
gtk.main()
if __name__ == "__main__":
main()

Get rid of a label inside a thread?

so i have this code:
import thread
from Tkinter import *
import random
import time
Admin=Tk()
def moveit(number):
songas=Label(Admin,text=number,bg='red')
def ji():
plad=0.0
recount=0
times=0
while 1:
plad-=0.1
recount+=1
times+=1
time.sleep(0.5)
pls=0.0
pls+=plad
if recount==4:
pls=0
plad=0.0
recount=0
songas.place(relx=pls,rely=0.7)
thread.start_new_thread(ji,())
za=random.random()
button=Button(Admin,text='Press',command=lambda:moveit(str(za)))
button.place(relx=0.2)
Admin.mainloop()
And it starts to move to the left but if you press the 'press' button again it puts some more numbers on top of the old ones.
does any one know how to erase the old numbers to make it so there are only the knew ones?
Tkinter isn't thread safe -- you can't manipulate widgets in any thread except the main one or you'll get undefined results.
You don't need threads for this. Your code adds an infinite loop, but the application already has an infinite loop (the event loop) that you can take advantage of.
If you want to move some item create a function that does two things. First, it does whatever it is you want, such as move the item. Second, it uses the standard after method to call itself again in a short amount of time (for example, half a second or 500ms). This way you let your event loop drive the animation, you don't need threads, and your UI stays responsive.
Here's an example. I doubt it does exactly what you want because I'm not certain of exactly what you want.
import Tkinter as tk
import random
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
self._running = False
self._relx = None
tk.Tk.__init__(self, *args, **kwargs)
self.pack_propagate(False)
self.configure(width=400, height=400)
self.label = tk.Label(self, text="hello, world", background="red")
self.button = tk.Button(self, text="Start", command=self.toggle)
self.button.pack(side="top")
def toggle(self):
'''toggle animation on or off'''
self._running = not self._running
if self._running:
self.button.configure(text="Stop")
self.moveit()
else:
self.button.configure(text="Start")
def moveit(self):
'''Animate the label'''
if not self._running:
# animation has been stopped
# hide the label from view.
self.label.place_forget()
if self._running:
if not self.label.winfo_viewable():
# not visible; establish future locations
self._relx = [.5, .4, .3, .2, .1, 0]
relx = self._relx.pop(0)
self._relx.append(relx)
self.label.place(relx=relx, rely=0.7)
self.after(1000, self.moveit)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
You must signal the old thread to exit somehow. It is probably easiest to perform with locks - you create a lock when creating a new thread and acquire it. And you release it when the thread is no longer needed. The thread then only needs to check in the main loop whether its lock is still locked - if it isn't it will remove the label and exit. Here the modified version of your code (replace "Remove label here" comment by suitable code):
import thread
from Tkinter import *
import random
import time
Admin=Tk()
lock = None
def moveit(number):
global lock
songas=Label(Admin,text=number,bg='red')
def ji(lock):
plad=0.0
recount=0
times=0
while 1:
plad-=0.1
recount+=1
times+=1
time.sleep(0.5)
pls=0.0
pls+=plad
if recount==4:
pls=0
plad=0.0
recount=0
songas.place(relx=pls,rely=0.7)
if not lock.locked():
# Remove label here
break
if lock:
# Signal old thread to exit
lock.release()
lock = thread.allocate_lock()
lock.acquire()
thread.start_new_thread(ji,(lock,))
za=random.random()
button=Button(Admin,text='Press',command=lambda:moveit(str(za)))
button.place(relx=0.2)
Admin.mainloop()

Categories