[Tutor] Text and Tkinter

2005-03-29 Thread Igor Riabtchuk



Hi,

As I am sure you know, the text widget in Tkinter 
by defaultprintskeyboard outputleft-to-right. Is there a way 
to make it print right-to-left?

Igor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] problems with dictionary

2005-03-23 Thread Igor Riabtchuk



Hi, 
was wondering whether you can help?

Say I got a dictionary of keys:values 
:

And what I want to do is depending on what 
key(a,b,c)the person presses, I want to output the value (tom, dic, 
harry).

So I program like this:

import Tkinter


D={a:"tom", b:"dick", c:"harry"}


text.bind('Key', self.Conv)

def Conv(self,event):
 if 
D.has_key(event.keysym):
  str="The name 
is"+str
 
self.text.insert(END,str)
 return 'break'

(If I had to do each one (i.e. without the 
dictionary)I would do as follows:

def Conv(self,event):
 if event.keysym==a:
 str="tom"
 self.text(END, str)
 return 'break'
)

There is clearly a mistake in the 
firstfunction, only thing is I cannot spot it and thus the thing does not 
work.

Any ideas?

Igor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dictionary blues...

2005-03-23 Thread Igor Riabtchuk



I posted the wrong code before. The code 
is:

from Tkinter import *


D={a:"tom", b:"dick", c:"harry"}


text.bind('Key', 
self.Conv)

def Conv(self,event): if 
D.has_key(event.keysym): 
str=D[event.keysym] 
self.text.insert(END,str) return 'break'

The error message says wrong syntax...

What I am basically trying to do is to allow the 
output of dictionaryvalues for each keyboard button pressed.

Igor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary blues...

2005-03-23 Thread Igor Riabtchuk
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):
   Frame.__init__(self)
   self.txt=Text(self)
   self.txt.bind('Key', self.conv)
   self.txt.pack()
   self.pack()
   self.txt.focus()
   def conv(self,event):
   if event.keysym=='t':
   str='p'
   self.txt.insert(END,str)
   return 'break'
app=CRED()
app.mainloop()
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':
   str='t'
elif event.keysym=='t':
   str='z'
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.
Igor

- Original Message - 
From: Alan Gauld [EMAIL PROTECTED]
To: Igor Riabtchuk [EMAIL PROTECTED]; tutor@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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Changing Keyboard output

2005-03-20 Thread Igor Riabtchuk



Hi,

I am totally new to Python and still 
learning.

I am looking for a way to change keyboard output 
within Tkinter widget - for example, say I press "p" and I want it to come out 
as "t".

Could anyone possibly point me in the right 
direction?

Igor 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor