picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

Tkinter and I/O control

Tue Sep 18, 2018 8:33 am

I'm using Tkinter with some buttons, sliders and such to make adjustment of the hardware to set fwd, stop, reverse, and adjust rpm of my motors. That seems ok, a callback function can access and manipulate the io. However, I'm a bit befudddled on how the rest of the system is to be situated. For example, two other I/O lines---as soon as both go high twice, another output to control a relay should activate, until either pin goes low (then the relay goes off). Another pin should read a limit switch and kill motor power & light an led as soon as it is detected (within a millisecond or hopefully sooner). I'm not sure where this code goes, since it has little or nothing to do with the gui itself. Ideally the gui's user's touches will not slow down these responses (so scrolling a slider won't delay the limit switch response)...but even some delay is ok for starters.

Are there any ideas or tutorials showing how to interface the gui with the hardware to do these kinds of things?

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

Re: Tkinter and I/O control

Tue Sep 18, 2018 9:54 am


scotty101
Posts: 3649
Joined: Fri Jun 08, 2012 6:03 pm

Re: Tkinter and I/O control

Tue Sep 18, 2018 10:11 am

Basic rule with Tkinter (and other GUI frameworks) is "No infinite loops unless it is the GUI framework one". So no while loops to read GPIO pins, no blocking I/O reads.

The simplest way to perform "background tasks" with tkinter is to use the .after method to schedule a function periodically.

Another method is to have your control inside a separate thread.

If you are already using callbacks linked to the GPIO pins, then you can continue to use these but keep what every happens in the callback brief and without any long waits/sleeps/delays.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

Re: Tkinter and I/O control

Tue Sep 18, 2018 4:35 pm

The simplest way to perform "background tasks" with tkinter is to use the .after method to schedule a function periodically.
Another method is to have your control inside a separate thread.
Thanks, a small step in the right direction. Do you know of any good project tutorials or articles that use this technique? There must be some projectsd that have non-gui things happening (not waiting on the gui).

Would the entire main program (non-gui) be the background? Since the gui is only a small part of the program (just to make adjustments of some of the parameters) , I was thinking the gui would be the background. I suppose that is why I was wanting to take a look at some completed examples. None of the tkinter tutorials I saw even mentioned any of this

picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

Re: Tkinter and I/O control

Sat Sep 22, 2018 6:33 pm

Are there some project articles that use both the tkinter graphics as well as also handling ongoing io operations? I'd like to take a look at one or two for ideas & read about the pitfalls and things to watch out for. It seems like this would be a pretty common situation for the rpi.

Return to “General discussion”