Key Binding Problem

MRAB python at mrabarnett.plus.com
Tue Mar 22 23:02:51 EDT 2016


On 2016-03-23 02:46, Wildman via Python-list wrote:
> Platform: Linux
> Python: v.2.7.9
> Tkinter: v.8.6.2
>
> My program has some buttons for file operations, load_image,
> save_image, and quit.  I would like to bind a key that will
> execute the procedures for each of the buttons.  The binding
> for the quit button was easy...
>
> root.bind("<q>", quit)
> root.bind("<Q>", quit)
>
> That works but it not executing a quit button procedure.
> It is merely executing an internal command.  My problem is
> calling an actual button procedure.  Over the last several
> hours I have tried many different syntax arrangements and
> I keep getting "object not defined" errors.  I also tried
> placing the bind statements into other places in the code.
> I have run out of ideas.
>
> Below is a basic skeleton of my code.  Any help appreciated.
>
> #!/usr/bin/env python
>
> try:
>      import Tkinter as tk
>      from Tkinter import Tk
> except ImportError:
>      import tkinter as tk
>      from tkinter import Tk
> import tkFileDialog, tkMessageBox
> import Image, ImageTk
> import base64, io, os, subprocess
>
> class cv():
>
>      # global variables
>
> class Window(tk.Frame):
>
>      def __init__(self, master = None):
>          tk.Frame.__init__(self,master)
>          self.master = master
>          self.init_window()
>
>      def init_window(self):
>          self.master.title("My Program")
>          self.pack(fill=tk.BOTH, expand=1)
>          self.quitButton = tk.Button(self,
>                                      text="Quit",
>                                      underline=0,
>                                      width=10,
>                                      command=self.quit)
>          self.quitButton.place(x=224, y=422)
>
>          self.loadButton = tk.Button(self,
>                                      text="Load Image",
>                                      underline=0,
>                                      width = 10,
>                                      command=self.load_image)
>          self.loadButton.place(x=138, y=46)
>
>          # create the rest of the widgets
>
>      def load_image(self):
>          # load image file
>
>      def save_image(self):
>          # save the image
>
>      def other procedure definitions
>
> root = Tk()
> root.bind("<q>", quit)  # these two work
> root.bind("<Q>", quit)
> root.bind("<l>", load_image)  # these do not work
> root.bind("<L>", load_image)  # object not defined errors
> root.bind("<s>", save_image)
> root.bind("<S>", save_image)
> root.minsize(width=554, height=462)
> root.maxsize(width=554, height=462)
> app = Window(root)
> root.mainloop()
>
> My question is how do I coax bind into executing the
> button procedures?  Or is there a way to generate the
> button click event from the binding?
>
It won't let you bind to a function called "load_image" because there 
isn't a function called "load_image"!

The "Window" class, however, does have a method with that name.

Try binding the keys in Window.__init__ or Window.init_window:

     def init_window(self):
         ...
         root.bind("<l>", self.load_image)




More information about the Python-list mailing list