-2

I have been using the following example to create a tkinter GUI which can switch between different frames.

Switch between two frames in tkinter

I would like to add a keyboard short cut to switch between the frames. E.g pressing '1' on the keyboard switches to page one in the example. I'm not sure how to use the existing structure to add in this new feature. I've read the effbot.org documentation on events and bindings but I'm confused how their examples can be applied to this particular case. Any help would be greatly appreciated, thanks.

1 Answers1

1

One can use bind method to attach keyboard events to call methods such as show_frame(given that it selects the pages in a list: "StartPage", "PageOne", "PageTwo":

def on_key_release(event):
    key_mapping = {'0':"StartPage", '1':"PageOne", '2':"PageTwo"}
    key_released = event.keysym
    if key_released in key_mapping:
        app.show_frame(key_mapping[key_released])


if __name__ == "__main__":
    app = SampleApp()
    app.bind('<KeyRelease>', on_key_release)
    app.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79