0

I've written a python program that uses gphoto2 to control my camera - so I can create a photobooth. Instead of offering the user a keyboard or mouse for input I've bought an xin-mo controller so I can provide a simple arcade style button interface (take photo, reject photo, accept photo etc).

The python program is written using Tkinter and I'm using evdev to read the input events from the xin-mo. The code I'm using for the xin-mo events is like so:-

while True:

  for ev in device.read():
    print("Take photo")

It works and I can do something when the buttons are pressed.

What I can't figure out is how do I get evdev and Tkinter to work together. From what I've found I need to call mainloop to render the GUI... but once in the mainloop how do I read input events from the xin-mo controller??

I'm very new to python, so I may well be missing something obvious.

Thanks, Steve.

Steve
  • 1
  • 1

1 Answers1

0

As long as the number of events on evdev isn't very big, you can poll it every few milliseconds.

Roughly, the solution would look like this, which uses the main event loop in place of your infinite loop:

root = tk.Tk()
...
def poll_evdev():
    for ev in device.read():
        print("Take photo")
    # schedule the polling to occur every 100 ms
    root.after(100, poll_evdev)
...
# start the polling process
poll_evdev()

# start the GUI event loop
root.mainloop()

Note: if the polling takes more than a few hundred milliseconds it will cause your GUI to lag. If that's the case you may need to do the polling in a separate thread.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for the code, I tried to used it in my program. Unfortunately it only worked for a couple of events then reported that the device wasn't ready. I ended up stripping down an old keyboard and soldering the wires from the arcade buttons to the PCB. This means that I now have keyboard letters as input... so I can trap them. Thanks again for the code. – Steve Apr 12 '15 at 20:11