[Tutor] Text and Tkinter
Hi, As I am sure you know, the text widget in Tkinter by default prints keyboard output left-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] Tkinter and keyboard output
Hi, I was playing around with Tkinter bindings and I got a question which is probably not particularly bright. If I have the following code, it works because I specifically code a function for keypress: from Tkinter import *class CRED(Frame): def __init__(self): Frame.__init__(self) self.txt=Text(self) self.txt.bind('', self.conv) self.txt.pack() self.pack() self.txt.focus() def conv(self,event):self.txt.insert(END,'t') return 'break'app=CRED()app.mainloop() What if instead of coding , I coded - how would I implement the function which would allow the code to determine which 'Key' was pressed after Alt? Thank you. Igor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary blues...
I just solved it :) thank you all for chastising me :) particularly Alan Gauld - you save me again :) I reckon I will have to put a credit for you in the code :) Igor - Original Message - From: "Igor Riabtchuk" <[EMAIL PROTECTED]> To: Sent: Wednesday, March 23, 2005 11:51 PM Subject: Re: [Tutor] Dictionary blues... 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('', 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]>; 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('', 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('', 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 maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary blues...
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('', 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]>; 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('', 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('', 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] Dictionary blues...
I posted the wrong code before. The code is: from Tkinter import * D={a:"tom", b:"dick", c:"harry"} text.bind('', 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 dictionary values for each keyboard button pressed. Igor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] problems with dictionary
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('', 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 first function, 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] Changing Keyboard output
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