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>'is mouse button 1,'1'is the literal character (relevant docs). So don’t use brackets on literal keys.Here is the fixed
buttonPressFactory:Regarding your statement
A good rule of thumb is that it is almost always your mistake. That’s true for everyone.