Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105507

Re: Key Binding Problem

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Key Binding Problem
Date 2016-03-23 03:02 +0000
Message-ID <mailman.27.1458702187.2244.python-list@python.org> (permalink)
References <o6ydncSeoNfrnm_LnZ2dnUU7-IfNnZ2d@giganews.com>

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-22 21:46 -0500
  Re: Key Binding Problem MRAB <python@mrabarnett.plus.com> - 2016-03-23 03:02 +0000
    Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-22 23:28 -0500
      Re: Key Binding Problem Terry Reedy <tjreedy@udel.edu> - 2016-03-23 02:47 -0400
        Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-23 10:40 -0500
        Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-23 10:58 -0500
          Re: Key Binding Problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-03-23 20:34 -0400
            Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-23 21:17 -0500
              Re: Key Binding Problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-03-24 08:06 -0400
                Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-24 11:19 -0500
                Re: Key Binding Problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-03-24 21:43 -0400
                Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-24 23:37 -0500
                Re: Key Binding Problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-03-25 12:50 -0400
  Re: Key Binding Problem Terry Reedy <tjreedy@udel.edu> - 2016-03-22 23:52 -0400
    Re: Key Binding Problem Wildman <best_lay@yahoo.com> - 2016-03-22 23:30 -0500

csiph-web