0

I'm writing the program for a psychology experiment that requires participants to learn a new number pad mapping.

If the keys were letters it would be easy enough: a=1, b=2, c=3 etc.

But apparently Python doesn't allow mapping to integers. Hmm.

A partial solution could be to use a dictionary, such as:

newdict = {'1': 9, '2': 8, '3': 7}

However, participants will be typing into a text box, and when their entry appears in the box it needs to have been converted already. There is no clear way to apply the newdict['1'] call.

How can this remapping be accomplished?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
adyo4552
  • 181
  • 1
  • 1
  • 8
  • Do you mean, for example, map `a` and `newdict['1']`. So when you run `a = 1`, then changes the `newdict['1']` to `1`? – Remi Guan Jan 20 '16 at 02:48
  • You need to heavily modify the textbox so that it modifies any inputs. Since you have not told us which GUI toolkit you are using, we cannot help you with that. Tell us which GUI toolkit you use (and [provide your current code](http://sscce.org/)) so that we can help you. In any case, it may be more advisable to simply capture keyboard presses and output the result in a non-writeable text display. – phihag Jan 20 '16 at 02:53
  • @ Kevin: Ideally the subject would type 1, and the text box would show 9 (or what have you). I'm not sure how to translate in real time from a subject typing an "old" number (i.e., 1) and showing the "new" number (i.e., 9). Does that make sense? "a = 1" was a bad example on my part, since I'm not looking to remap letters at all. – adyo4552 Jan 20 '16 at 02:54
  • @adyo4552: Oh, you mean that. Then what about: `newdict[int(intput())]`? Oh, also you can use `int` objects as dict's key so your dict could be `newdict = {1: 9, 2: 8, 3: 7}`. – Remi Guan Jan 20 '16 at 03:01

1 Answers1

1

Your instinct to use a dict is good -- it's an easy way to create a mapping from one character to another. You could also just use a list of tuples (eg: ((1, 9), (2, 8), ...))

You don't mention the GUI toolkit (or whether you're just doing a console app), so I'll give a tkinter example to illustrate how to set up a key binding for each key using the dictionary. Using a tuple would be nearly identical.

import tkinter as tk # for python 2 use 'Tkinter'

class Example(tk.Frame):
    keymap = {'1': '9', '2': '8', '3': '7',
              '4': '6', '5': '5', '6': '4',
              '7': '3', '8': '2', '9': '1'}

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.entry = tk.Entry(self, width=20)
        self.entry.pack(fill="x", pady=20, padx=20)

        for old_key, replacement in self.keymap.items():
            self.entry.bind('<KeyPress-%s>' % old_key,
                            lambda event, char=replacement: self.insert(char))

    def insert(self, char):
        self.entry.insert("insert", char)
        return "break"

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685