Using callback function in pyevent - python

I want to detect pressing the "snapshot" button on the top of a webcam in linux. The button has this entry in /dev:
/dev/input/by-id/usb-PixArt_Imaging_Inc._USB2.0_UVC_VGA-event-if00
I am using the "rel" wrapper, at the moment, because it handles exceptions better. Before the following code executes, self.s.cam_btn is assigned the /dev entry for the button.
rel.override()
rel.init()
rel.read(self.s.cam_btn, self.snap)
rel.dispatch()
self.snap() is the callback. It captures a screen shot from mplayer and feeds the image to an OCR program. Everything mostly works until the callback returns. Here is the problem:
If self.snap() returns nothing, the program stops and will not service any more button events. If self.snap() returns 1, the program continues servicing the same button event in an infinite loop, rather than waiting for a new event. Documentation for pyevent is a little sparse so any help gratefully received.
Clinton

Never used pyevent, but would try rescheduling the event at the end of the handler:
def snap(self):
# ... code ...
rel.read(self.s.cam_btn, self.snap)
return False

Related

How to execute a command with a button and delete it before the command ends ? Python / Tkinter

I've got an interface where there is a 'Start' button. It runs a 'main' command where it starts a loop to run some measurements on a powermeter. I want to be able to click on an 'OK' button every time the measure is ready to be done. This button would replace the 'Start' button.
But when I try to destroy the button (buttonStart.destroy()) and then run the command main()
, the command executes but doesn't delete the button until the very end.
I've tried using threads from threading package, but it doesn't really work.
If there is a way to both destroy the button and run a command, I would be very interested !
Thanks a lot
The event loop must be given the opportunity to process events in order for the window to be removed from the screen.
There are a couple of ways to make that happen. The first is to call the update_idletasks method on any widget. That is usually enough to process the event related to the destruction of the widget, though it may be necessary to call update instead. Calling update should be avoided if at all possible. My rule of thumb is to start with update_idletasks. If that doesn't work, switch to update or switch to using after.
def my_custom_function():
startButton.destroy()
root.upddate_idletasks()
main()
The second method is to run your main function via after or after_idle. This will let tkinter naturally process all pending events before starting main. I would argue that this is the best approach. I recommend trying after_idle first, and if that doesn't work, switch to after with a small delay.
def my_custom_function():
startButton.destroy()
root.after_idle(main)

Want Clarification for Program Loop (Python)

So I was wondering if someone would be able to help shed a little light for me on something I am working on in Python.
I am creating a program with a Tkinter GUI interface that interacts with a Serial device, and an ADC chip to measure voltage. I want to make sure I properly understand how I'm building the main program loop to keep everything running smoothly. I'm going to lay out how I think the program should run, if anyone has any corrections please throw them at me.
Program is run, GUI Interface initializes
User presses a button
send signal of button through serial
measure/display voltage levels
periodically update voltage display
if button is pressed, return to step 3
Now I know to run my Tkinter GUI I set up mainloop() as the last line of code. Now my question is simply, is that all I will need? Will mainloop() continually update while it waits for another button press, or will I essentially have to creatre an update method that cycles through everything until another button is pressed?
Will mainloop() continually update while it waits for another button press, or will I essentially have to creatre an update method that cycles through everything until another button is pressed?
Not all. That's why you are using tk.Tk().mainloop(). tkinter does this for you. All you are expected to do is implement the functionality that should happen when your button is pressed. tkinter will listen for the button press.

Functions acting strange when run as thread in Kivy/Python

I am building an app that, when the user hits a 'run' button, generates a table of buttons.
Because this process takes a while, I want to add a popup or progress bar to alert the user that the function is running and not frozen. To do this I decided to create a popup and call my function using threading so that the screen will be updated when the function starts (as opposed to once it is done).
mythread = threading.Thread(target=run_function)
mythread.start()
The trouble is that when I call my function from the above code it works very strangely: the columns of my table are the wrong width, some of my buttons are arbitrarily empty, and others have the wrong fill color. To fix this, all I need to do is to remove the threading operation and simply call run_function()
Any idea why this is happening?
I am new to Python, so it is likely some dumb mistake, but I have no idea. What is different between a process running as a thread and its default operation?
Disclaimer: I haven't worked with Kivy.
Not every framework works well with multithreading.
But most of the GUI frameworks have an event loop which is responsible for managing user events (mouse clicks, keyboard) and queued drawing operations (widgets).
In your case if don't want the UI to be freezed, you should regularly give control to your framework's event loop.
I guess kivy.base.EventLoopBase.dispatch_input is what you need to call to show an added widget or to handle user events.

Preventing a Button Press From Registering TKInter/Python 3.3

I'm in the process of creating a filling program with Python 3.3/Tkinter and have run into an issue that's really stumping me. Basically, I want to prevent button presses from registering while certain processes are taking place. I tried changing the button's state to state='disabled' but this only delays the click from registering until after the function is finished running. In other words, despite the button being "disabled", if it's clicked, the button press will register as soon as it's re-enabled. Please see the example code below.
def Button_Action:
Button.config(state='disabled')
#Activate some hardware that takes a few seconds.
Button.config(state='normal')
So, the question is: How can one selectively ignore button presses in Tkinter/Python 3?
I'm really new to Python and have tried searching for relevant questions to no avail, so please forgive me if this is a stupid question, or has been asked before. Also, I have tested this with both Radiobuttons as well as, standard Buttons (in case that helps).
You can use update method.
def button_action():
b.config(state='disabled')
b.update() # <----------
# Activate some hardware that takes a few seconds.
b.update() # <----------
b.config(state='normal')
The first call to update is to make the button displayed as disabled state.
The second call to update is to handle all pending event (clicks in your case) before enable the button.
BTW, it's normal state, not enabled that make the button back to normal state.

Timer in Python

I am writing a python app using Tkinter for buttons and graphics and having trouble getting a timer working, what I need is a sample app that has three buttons and a label.
[start timer] [stop timer] [quit]
When I press the start button a function allows the label to count up from zero every 5 seconds, the stop button stops the timer and the quit button quits the app.
I need to be able to press stop timer and quit at any time, and the time.sleep(5) function locks everything up so I can't use that.
currently i'm using threading.timer(5,do_count_function) and getting nowhere !
I'm a vb.net programmer, so python is a bit new to me, but hey, i'm trying.
Check the .after method of your Tk() object. This allows you to use Tk's timer to fire events within the gui's own loop by giving it a length of time and a callback method.

Categories

Resources