Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:05:33+00:00 2026-06-15T15:05:33+00:00

I made some hardware for my Raspberry Pi’s GPIO that I’d like to test.

  • 0

I made some hardware for my Raspberry Pi’s GPIO that I’d like to test. I found some neat python code that makes 8 buttons for the 8 outputs, and lets you toggle their state. I have almost no knowledge of python, but I’d like to be able to toggle the 8 outputs via keyboard (ex. numbers 1-8). I don’t know how to ask for a keyboard input without pausing the flow of the program then continuing after response.

How can I make the numbers 1-8 “interrupt” my program and jump to 1 of the 8 corresponding functions?

My code:

from Tkinter import *
import RPi.GPIO as GPIO
import time

GPIO.setmode( GPIO.BCM )
GPIO.setup( 4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

class App:
    io4=0
    io17=0
    io18=0
    io21=0
    io22=0
    io23=0
    io24=0
    io25=0

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.p1 = Button(frame, text="GPIO 25",fg="green", command=self.gpio25)
        self.p1.pack(side=LEFT)
        self.p1.grid(row=0,column=0)

        self.p2 = Button(frame, text="GPIO 24",fg="red", command=self.gpio24)
        self.p2.pack(side=LEFT)
        self.p2.grid(row=0,column=1)

        self.p3 = Button(frame, text="GPIO 23",fg="red", command=self.gpio23)
        self.p3.pack(side=LEFT)
        self.p3.grid(row=0,column=2)

        self.p4 = Button(frame, text="GPIO 22",fg="red", command=self.gpio22)
        self.p4.pack(side=LEFT)
        self.p4.grid(row=0,column=3)

        self.p5 = Button(frame, text="GPIO 21",fg="red", command=self.gpio21)
        self.p5.pack(side=LEFT)
        self.p5.grid(row=0,column=4)

        self.p6 = Button(frame, text="GPIO 18",fg="red", command=self.gpio18)
        self.p6.pack(side=LEFT)
        self.p6.grid(row=0,column=5)

        self.p7 = Button(frame, text="GPIO 17",fg="red", command=self.gpio17)
        self.p7.pack(side=LEFT)
        self.p7.grid(row=0,column=6)


        self.p8 = Button(frame, text="GPIO 4", fg="red",command=self.gpio4)
        self.p8.pack(side=LEFT)
        self.p8.grid(row=0,column=7)

    def gpio4(self):
        if self.io4==0:
          GPIO.output(4, GPIO.HIGH)
          self.io4=1
        else:
          GPIO.output(4, GPIO.LOW)
          self.io4=0
        return

    def gpio17(self):
        if self.io17==0:
          GPIO.output(17, GPIO.HIGH)
          self.io17=1
        else:
          GPIO.output(17, GPIO.LOW)
          self.io17=0
        return

    def gpio18(self):
        if self.io18==0:
          GPIO.output(18, GPIO.HIGH)
          self.io18=1
        else:
          GPIO.output(18, GPIO.LOW)
          self.io18=0
        return

    def gpio21(self):
        if self.io21==0:
          GPIO.output(21, GPIO.HIGH)
          self.io21=1
        else:
          GPIO.output(21, GPIO.LOW)
          self.io21=0
        return

    def gpio22(self):
        if self.io22==0:
          GPIO.output(22, GPIO.HIGH)
          self.io22=1
        else:
          GPIO.output(22, GPIO.LOW)
          self.io22=0
        return

    def gpio23(self):
        if self.io23==0:
          GPIO.output(23, GPIO.HIGH)
          self.io23=1
        else:
          GPIO.output(23, GPIO.LOW)
          self.io23=0
        return

    def gpio24(self):
        if self.io24==0:
          GPIO.output(24, GPIO.HIGH)
          self.io24=1
        else:
          GPIO.output(24, GPIO.LOW)
          self.io24=0
        return

    def gpio25(self):
        if self.io25==0:
          GPIO.output(25, GPIO.HIGH)
          self.io25=1
        else:
          GPIO.output(25, GPIO.LOW)
          self.io25=0
        return

    def reserved(self):
        return

root = Tk()
app = App(root)
root.mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:05:35+00:00Added an answer on June 15, 2026 at 3:05 pm

    Add master.bind(...) commands to the __init__ method:

    def __init__(self, master):
    
        frame = Frame(master)
        frame.pack()
        master.bind('1', self.gpio25)
        master.bind('2', self.gpio24)
        master.bind('3', self.gpio23)
        ...
    

    master.bind('1', self.gpio25) binds the keypress 1 event to the method call self.gpio25(event).
    You will need 7 more master.bind calls — one for each key.

    Next, modify the definition of the callback functions. You will need to add a second argument, event, to each of them. Change, for example,

    def gpio25(self):
    

    to

    def gpio25(self, event = None):
    

    Explanation:

    When you use the bind method, you are binding an Event to a callback function (e.g. self.gpio25). Pressing a key is a KeyPress Event. Information about the event is sent to the callback in an Event object. Thus the callback function must take one argument.
    See Tkinter events and bindings for more information.

    Buttons work differently. Their callback functions are called with zero arguments.

    Since we are using the same callback function, self.gpio25, as both a Keypress event callback and as a Button callback, it must be a function that can take zero or one argument.

    In Python, the way to do that is to define gpio25 with a call signature like:

    def gpio25(self, event = None):
    

    self is an instance of the class App. self.gpio25 is a bound method with self bound as its first argument. So calling self.gpio25() will call the gpio25 function with self as the first argument (and the local variable event will be assigned the value None). Calling self.gpio25(foo) will call gpio25 with self as the first argument, and foo as the second argument (and the local variable event will be assigned the value foo).

    In your case, the value assigned to event does not really matter, since event is not used in the body of gpio25. We just needed to set up self.gpio25 to be ready to accept zero or one argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made some code that makes powerpoint and excel work together. And I
I made some code to produce boxes every time my code encounters a white
I made some code, for understanding the concept/basic of pointer: int a=1; int *b=&a;
It made some errors like this: KDnsCache.cpp: In member function ‘unsigned int KDnsCache::GetName(const char*)’:
After I made some researches, I found out there is only one way to
I made some test and I was able to create and remove boost::interprocess::shared_memory_object in
Okay so I made some changes in my project that resulted in a huge
I have made some custom attached properties that enable me to create a pop
I've written an application that uses some serial port hardware directly, and abstracted the
A made some nine-patches for my buttons in Android but they scale in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.