[Tutor] Dictionary blues...

Igor Riabtchuk igor.r at vodafone.net
Thu Mar 24 00:51:21 CET 2005

My deepest and most sincere apologies - cooking dinner for the family and 
posting questions do not mix, I keep making mistakes in the code I type. 
Once again my apologies - here's the code as it is in my source:

import sys, os, unicodedata
from Tkinter import *

class CRED(Frame):
    def __init__(self):
        self.txt.bind('<Key>', self.conv)

    def conv(self,event):
        if event.keysym=='t':
        return 'break'


This works - i.e. when I press 't' on the keyboard, it gives me 'p'. What I 
want to do is, instead of coding a conversion for each letter separately 
using a long "if.... elif" sequence, to put all the conversion values into 
the dictionary and then a general function for each keypress that would take 
the values out of the dictionary.

E.g. - say I have dictionary D={'p':'t','t':'z'}

Instead of coding the conv function for each letter:

if event.keysym=='p':
elif event.keysym=='t':

put all the conversion values into a dictionary and make the function use 
the key:value pairs from dictionary.

I hope I am making sense.


----- Original Message ----- 
From: "Alan Gauld" <alan.gauld at freenet.co.uk>
To: "Igor Riabtchuk" <igor.r at vodafone.net>; <tutor at python.org>
Sent: Wednesday, March 23, 2005 11:17 PM
Subject: Re: [Tutor] Dictionary blues...

> Igor,
>> I posted the wrong code before. The code is:
> Is this the actual code you have written? If so it is
> so far from being working code that it suggests you
> need to back up a little and work at the basics of
> Python before trying to tackle Tkinter and GUIs.
> I'll assume this really is your code and make some
> comments...
>> from Tkinter import *
>> D={a:"tom", b:"dick", c:"harry"}
> You need to put the keys in quotes too
>> text.bind('<Key>', self.Conv)
> self is only used to access the members of a class,
> you don't have any class so you don't need self.
> You also don't, at this stage, have anything called
> 'text' so you can't bind anything to it. You need to
> create a text widget which in turn it parented under
> a Tk object
> top = Tk()
> text = Text(top)
> text.bind('<Key>', Conv)
> But even here, Conv hasn't been defined yet so you
> need to move the Conv definition above those lines.
>> def Conv(self,event):
> You can remove the self fro the parameter list, its
> only needed if this is a method of a class.
>>    if D.has_key(event.keysym):
>>      str=D[event.keysym]
>>    self.text.insert(END,str)
> You can remove the 'text.' thats only used if text
> were part of a class, which in this case it isn't.
> Also you probably want to indent the insert line as
> part of the if clause.
>>    return 'break'
> And before anything works you need to 'pack' the text
> widget and set the event loop running:
> text.pack()
> top.mainloop()
>> The error message says wrong syntax...
> I'm sure it said a lot more than that. Please send the
> whole error message, the bit that tells us what exactly
> Python thought was wrong and where. In this case there
> is so much that is wrong it doesn't matter too much but
> in future it will be important. The more you help us
> the more we can help you.
> If the above doesn't make sense can I suggest you try
> building a textual version first using the dictionary
> and raw_input to read the keys from the console. Once
> you have the basics working putting it into a GUI will
> be much easier.
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld

More information about the Tutor mailing list