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 7559389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:37:25+00:00 2026-05-30T12:37:25+00:00

So I have the following code creating a grid of buttons using tkinter: class

  • 0

So I have the following code creating a grid of buttons using tkinter:

class Application(Frame):UP = 'Up'
    DOWN = 'Down'
    LEFT = 'Left'
    RIGHT = 'Right'
    END = "E"
    SEND = "S"
    STAR = "*"
    POUND = "#"
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.arrowButtons = []
        self.arrowButtons += [Button(self, text=self.LEFT[0], command=self.buttonPressFactory(self.LEFT) )]
        self.arrowButtons += [Button(self, text=self.UP[0], command=self.buttonPressFactory(self.UP) )]
        self.arrowButtons += [Button(self, text=self.RIGHT[0], command=self.buttonPressFactory(self.RIGHT) )]
        self.arrowButtons += [Button(self, text=self.DOWN[0], command=self.buttonPressFactory(self.DOWN) )]

        self.send = Button(self, text=self.SEND, command=self.buttonPressFactory(self.SEND) )
        self.end = Button(self, text=self.END, command=self.buttonPressFactory(self.END) )
        self.send.grid(row=2,column=1)
        self.end.grid(row=2,column=3)
        self.numButtons = []

        for i in range(0,10):#make the number buttons
            self.numButtons.append(Button(self, text=str(i), command=self.buttonPressFactory(str(i))))

        self.starButton = Button(self, text=self.STAR, command=self.buttonPressFactory(self.STAR) )
        self.hashButton = Button(self, text=self.POUND, command=self.buttonPressFactory(self.POUND) )

        self.arrowButtons[0].grid(row=1,column=1)
        self.arrowButtons[1].grid(row=1,column=2)
        self.arrowButtons[2].grid(row=1,column=3)
        self.arrowButtons[3].grid(row=2,column=2)
        self.send.grid(row=2,column=1)
        self.end.grid(row=2,column=3)
        self.numButtons[1].grid(row=3,column=1)
        self.numButtons[2].grid(row=3,column=2)
        self.numButtons[3].grid(row=3,column=3)
        self.numButtons[4].grid(row=4,column=1)
        self.numButtons[5].grid(row=4,column=2)
        self.numButtons[6].grid(row=4,column=3)
        self.numButtons[7].grid(row=5,column=1)
        self.numButtons[8].grid(row=5,column=2)
        self.numButtons[9].grid(row=5,column=3)
        self.starButton.grid(row=6,column=1)
        self.numButtons[0].grid(row=6,column=2)
        self.hashButton.grid(row=6,column=3)

    def press(self, x):
        print(x)

    def buttonPressFactory(self, button):
        def buttonPress(*args):
            self.press(button)
        root.bind("<"+button+">", buttonPress)
        return buttonPress
if __name__ == '__main__':
    root = Tk()
    app = Application(root)                    
    app.master.title("stackoverflow is great")
    app.mainloop()  

The problem started when I added the line in buttonPressFactory that starts “root.bind(”
When I added that line, suddenly clicking any button seems to call press(1) then call press(whatever). The keybinds work correctly except for 1,2,3,4, and 5 (on the numpad or numrow). If you remove that line, everything works ok, but of course then you can’t use the keybinds. I am new to tkinter, so I might have made an obvious mistake, though my intuition tells me I might have made a syntax mistake.

How can I add keybindings to the code without breaking it?

  • 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-05-30T12:37:26+00:00Added an answer on May 30, 2026 at 12:37 pm

    '<1>' is mouse button 1, '1' is the literal character (relevant docs). So don’t use brackets on literal keys.

    Here is the fixed buttonPressFactory:

    def buttonPressFactory(self, button):
        def buttonPress(*args):
            self.press(button)
        eventname = '<' + button + '>' if len(button) > 1 else button
        root.bind(eventname, buttonPress)
        return buttonPress
    

    Regarding your statement

    I am new to tkinter, so I might have made an obvious mistake, though
    my intuition tells me this is actually a python mistake.

    A good rule of thumb is that it is almost always your mistake. That’s true for everyone.

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

Sidebar

Related Questions

I have the following code for creating tabs. It works at the end of
I have following code class User attr_accessor :name end u = User.new u.name =
I have been following this nice walkthrough for creating a grid using MVC contrib.
I have the following code: Class B { void generator() { // creating random
i am using nopcommerce 1.9 and in creating discount i have the following code
I'm creating a Java application in NetBeans, and I have the following code that
I have following code that I creating a grid var store = Ext.create('Ext.data.ArrayStore', {
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a

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.