Skip to main content
5 of 8
edited tags; edited title; edited title
200_success
  • 144.2k
  • 22
  • 188
  • 473

Binding a keyboard key to a Tkinter button

I'm specifically asking about binding a keyboard keystroke to a button in a GUI. Originally I planned to bind the keystroke to the GUI button itself, but that seemed impossible, and potentially unwise after I considered it later.`

Is this the proper(, pythonic) way to bind a keyboard key and GUI button?

## start.py
print 'start'

#from tkinter import *
from Tkinter import *
import os
from PIL import ImageTk, Image

class MyClass:
    '''This is my class.'''

    imgpath = '/Users/user/Dropbox/Camera Uploads'
    imgfile = 'susshi.jpg'
    #imgfilepath = os.path.join(self.imgpath, self.imgfile)
    imgfilepath = os.path.join(imgpath, imgfile)

    def __init__(self,master):
        print 'in __init__'

        # Create and load a frame into the tk (tkinter) window.
        frame = Frame(master)
        frame.pack()

        # Create and load two buttons in to the above-created frame.
        self.button = Button(frame, text="QUIT", fg="red", command=quit)
        self.button.pack(side=LEFT)
        self.slogan = Button(frame, text="Hello", command = self.f)
        self.slogan.pack(side=RIGHT)
        
        # Bind a key to each button from above.
        master.bind('q', quit)
        master.bind('f', self.f)
        
        # Open the image, resize it...
        self.image = Image.open(self.imgfilepath)
        self.image.thumbnail((600,600))
        # Make a tkinter-friendly image object ...
        self.display_image = ImageTk.PhotoImage(self.image)
        # Make a tkinter canvas, plug in the image, and pack the canvas.
        self.canvas = Canvas(master, bg='red')
        self.canvas.create_image(0,0, image=self.display_image, anchor="nw")
        self.canvas.pack(fill=BOTH, expand=1)
        ### FIXME: Set canvas focus!
        
    def f(self, event=None):
        print 'in f()'
        return 'hello world'
        
    def get_image_list(self):
        pass
    
    def next_image(self):
        pass
    
    def previous_image(self):
        pass

    def load_image(self):
        #stub
        pass
        
    def update_image(self):
        pass
    
root = Tk()
abc = MyClass(root)
root.mainloop()

print 'abc has been defined'
print '"abc.f()" returns "%s"' % abc.f()
#print str(abc.f)
#print repr(abc.f)

print 'end'

The intention of the project is to make a simple image viewer in python, with buttons in the GUI for basic navigation functions, and keys mapped to those same functions.

This is the actual code I'm using. I haven't implemented the other functions yet, and I was hoping to just get the code implemented thus-far reviewed. In fact, the original question was just asking about reviewing one specific portion of the code - that is, the binding of the keystrokes in association with the buttons in the GUI. However, that specific-level of question was downvoted and given a recommendation to adhere to the guidance in the help center. I understand there may be a common issue of incomplete code segments posted, but I have truly included the entire module.

user3.1415927
  • 171
  • 1
  • 1
  • 6