1

I'm trying to print the key used to press a button. How do you extract the char from the event (eg. <KeyPress event keysym=1 keycode=49 char='1' x=95 y=34 >)

What I've got so far:

from tkinter import Button, Frame, Tk

class ButtonCreator:
    def __init__(self, master, num, options):
        frame = Frame(master)
        frame.pack()
        self.button = Button(frame, text=str(num) + ' ' + str(options[num-1]),
                             command=self.func)

        self.button.pack(side='left')
        master.bind(str(num), self.func)

    def func(self, occur, _event=None):
        print(occur)                # <- num is event, not char
        print(options[occur-1])     # <- failing here: TypeError: list indices must be
                                    #    integers or slices, not Event


options = ['one', 'two', 'three']
button_count = len(options)

root = Tk()
for j in range(button_count):
    abc = ButtonCreator(root, j+1, options)

root.mainloop()

Edit: Solved my issue. Instead of using

options[occur-1]

I should be using

options[int(occur.char)-1]
D_00
  • 1,440
  • 2
  • 13
  • 32
JoshW
  • 83
  • 7
  • What is your expected output? Why are you setting `_event` to `None`. What are you expecting to happen with `master.bind(str(num), lambda e:self.func(e))`. – Delrius Euphoria Feb 28 '21 at 19:09
  • @CoolCloud The desired output for the print function is the key pressed. Since this is binding keys "1" to "len(options)" (eg. in this case, buttons "1", "2" and "3"), it'd return a number which could be used to identify the output in a string. – JoshW Feb 28 '21 at 22:28

2 Answers2

2

Try this:

from tkinter import Button, Frame, Tk
from functools import partial


class ButtonCreator:
    def __init__(self, master, num, options):
        frame = Frame(master)
        frame.pack()
        self.num = num # Save the button number
        self.button = Button(frame, text=str(num) + ' ' + str(options[num-1]))
        self.button.pack(side='left')

        self.button.bind("<Button-1>", self.func)
        self.button.bind("<Button-2>", self.func)
        self.button.bind("<Button-3>", self.func)
        master.bind(str(num), self.func)

    def func(self, event):
        print("You pressed it with this key=", event.num)
        print("Button number =", self.num)
        print("Option chosen:", options[self.num-1])


options = ['one', 'two', 'three']
button_count = len(options)

root = Tk()
for j in range(button_count):
    abc = ButtonCreator(root, j+1, options)
root.mainloop()

I used <tkinter>.bind. For more info read this.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • While this wasn't exactly what I was looking for, it got me on the right track. Instead of using "options[occur-1]", I should've used "options[int(occur.char)-1]". Thanks for the help! – JoshW Feb 28 '21 at 22:42
  • @JoshW happy to have helped (even if it is indirectly) – TheLizzard Feb 28 '21 at 22:45
0

Instead of using options[occur-1] I should be using options[int(occur.char)-1] - as that outputs the specific character aspect of the event rather than the entire event.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
JoshW
  • 83
  • 7