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


Groups > comp.lang.python > #105507

Re: Key Binding Problem

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Key Binding Problem
Date Wed, 23 Mar 2016 03:02:51 +0000
Lines 100
Message-ID <mailman.27.1458702187.2244.python-list@python.org> (permalink)
References <o6ydncSeoNfrnm_LnZ2dnUU7-IfNnZ2d@giganews.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de 6+7GxiZOo0wFt7/b2C3+1AY2YTCY+GyYe+u97ncST1DQ==
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'root': 0.04; 'importerror:': 0.05; 'definitions': 0.07; 'none):': 0.07; 'width': 0.07; 'buttons': 0.09; 'command.': 0.09; 'python:': 0.09; 'statements': 0.09; 'python': 0.10; 'syntax': 0.13; 'def': 0.13; 'variables': 0.15; '"object': 0.16; 'base64,': 0.16; 'bind': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'platform:': 0.16; 'quit.': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problem': 0.16; 'subprocess': 0.16; 'tk()': 0.16; 'wrote:': 0.16; 'app': 0.16; 'try:': 0.18; 'load': 0.20; 'class,': 0.22; 'keys': 0.22; 'os,': 0.22; 'tkinter': 0.22; 'code.': 0.23; 'defined': 0.23; 'errors': 0.23; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'linux': 0.26; 'rest': 0.26; 'appreciated.': 0.27; 'skip:# 10': 0.27; 'question': 0.27; 'errors.': 0.27; 'executing': 0.27; 'operations,': 0.27; 'function': 0.28; 'actual': 0.28; 'arrangements': 0.29; 'skip:_ 10': 0.32; 'received:84': 0.32; 'getting': 0.33; 'run': 0.33; 'class': 0.33; 'problem': 0.33; 'file': 0.34; 'except': 0.34; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'basic': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; "won't": 0.38; 'button': 0.38; 'several': 0.38; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'save': 0.60; 'different': 0.63; 'places': 0.64; 'hours': 0.65; 'binding': 0.66; 'python-list': 0.66; 'click': 0.76; 'ideas.': 0.84; 'io,': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=bsGxfxui c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=jc_vtz2qLWtxwsWOaZ4A:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0
In-Reply-To <o6ydncSeoNfrnm_LnZ2dnUU7-IfNnZ2d@giganews.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105507

Show key headers only | 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