RE: [pygtk] Re: adjust key bindings
Fabian, I have had a go at binding the "z" key to the down cursor key. I can block the Z key press and assign the "down" cursor key press and print out the "down" key press but I can't redirect the signal back to the widget. I have enclosed the code below, maybe someone else on the list can finish off the code. The code below assumes that you have a window in Glade with one treeview widget. Both widgets have the "key_press_event" assigned in glade. import shutil import pygtk import findgtk import gtk import gtk.glade import gobject import os import sys class Shopcard: def __init__(self): self.wTree = gtk.glade.XML ("project12.glade", "window1") dic={"on_window1_delete_event" : self.quit100, } self.wTree.signal_autoconnect (dic) tree1 = self.wTree.get_widget("treeview1") windy = self.wTree.get_widget("window1") tree1.connect("key_press_event",self.callback3000,tree1,windy) model = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING) self.hostmodel = model host_inter=self.insert_row(model,None,"test",'Test') host_inter=self.insert_row(model,None,"test1",'Test') host_inter=self.insert_row(model,None,"test2",'Test') host_inter=self.insert_row(model,None,"test3",'Test') host_inter=self.insert_row(model,None,"test4",'Test') tree1.set_model(model) renderer=gtk.CellRendererText() column=gtk.TreeViewColumn("Col1",renderer,text=0) column.set_resizable(True) tree1.append_column(column) renderer=gtk.CellRendererText() column=gtk.TreeViewColumn("Col2",renderer,text=1) column.set_resizable(True) tree1.append_column(column) def insert_row(self,model,parent,firstcolumn,secondcolumn, thirdcolumn=None): myiter=model.insert_after(parent,None) model.set_value(myiter,0,firstcolumn) model.set_value(myiter,1,secondcolumn) if thirdcolumn !=None: model.set_value(myiter,2,thirdcolumn) return myiter def callback3000(self,widget,event,tree1,windy): import re """prevents the possibility of inputting wrong chars""" ## fixme: missing comma, and cut&paste key = gtk.gdk.keyval_name (event.keyval) ONLYDIGITS="([0-9.,z]|BackSpace|Left|Right|F1|period|Tab|Up|Down)" if not re.match (ONLYDIGITS, key): print "True" return True else: if key == "z": tree1.emit_stop_by_name("key_press_event") # This blocks the signal from the key press event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) event.keyval = 65364 # This is the code for the down key event.state = gtk.gdk.CONTROL_MASK event.time = 0 # assign current time print gtk.gdk.keyval_name(event.keyval) tree1.emit('key_press_event', event) tree1.connect("key_press_event",self.callback3001) else: print event.keyval print "else" return False def callback3001(self,widget,event): import re """prevents the possibility of inputting wrong chars""" ## fixme: missing comma, and cut&paste key = gtk.gdk.keyval_name (event.keyval) print key print "made it" ONLYDIGITS="([0-9.,z]|BackSpace|Left|Right|F1|period|Tab|Up|Down)" if not re.match (ONLYDIGITS, key): print "True2" return True def quit(self,obj): gtk.main_quit() sys.exit(1) def quit100(self,obj,wind): gtk.main_quit() sys.exit(1) if __name__ == '__main__': Shopcard() try: gtk.threads_init() except: print "No threading was enabled when you compiled pyGTK!" import sys sys.exit(1) gtk.threads_enter() gtk.main() gtk.threads_leave() The code outputs the following: When you hit the down cursor it prints: 65364 else and when you hit the "z" key it prints: Down 65364 else Regards, John. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Fabian Braennstroem Sent: 18 November 2006 19:14 To: pygtk@daa.com.au Subject: [pygtk] Re: adjust key bindings Hi, sorry, is that question to stupid or does nobody has a hint? * On 14 Nov 2006 * Fabian Braennstroem wrote: > Hi, > > I just start to use pygtk ... so it is just my first > question :-) > > I would like to create a small file manager based on 'lfm' > (curses based file manager). I used glade for the gui and I > am able to display the existing files and directories using > two treeview widgets. > Now, at the beginning I am kind of stuck with the key bindings. In 'lfm' > it was pretty easy to define special key bindings: > > > keytable = { > # movement > ord('p'):
RE: [pygtk] key_press_event
Sandro, That's exactly what I need. Thanks, John. > def callback3(self,data,widget): > > input = data.get_text() > print input > data.set_text("test") If you don't return True, default callback will be called that insert the 'a'. I have something like this: def digits_check_input_cb(self, widget, event): """prevents the possibility of inputting wrong chars""" ## fixme: missing comma, and cut&paste key = gtk.gdk.keyval_name (event.keyval) ONLYDIGITS="([0-9.,]|BackSpace|Left|Right|F1|period|Tab|Up|Down)" if not re.match (ONLYDIGITS, key): return True Not sure whether this is the best way thought... sandro *;-) -- Sandro Dentella *:-) http://www.tksql.orgTkSQL Home page - My GPL work ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
[pygtk] key_press_event
Hi, I'm using Python 2.4, pygtk and Glade2. I have a few text entries. I am trying to put validation on the text entry boxes. I am trying to catch the key_press_event, check the key that has been pressed and either allow it or put back the old text. The code is below with the output after it. class Test: def __init__(self): self.wTree = gtk.glade.XML ("test.glade", "window1") dic={"on_window1_delete_event" : self.quit10, } self.wTree.signal_autoconnect(dic) cancel = self.wTree.get_widget("button2") cancel.connect("clicked", self.quit, ) text1 = self.wTree.get_widget("entry1") text2 = self.wTree.get_widget("entry2") text2.connect("key_press_event",self.callback3) login = self.wTree.get_widget("button1") login.connect("clicked", self.callback2, text1,text2) def callback3(self,data,widget): input = data.get_text() print input data.set_text("test") def callback2(self,data,text1,text2): print 'hello' def quit10(self,obj,data): gtk.main_quit() sys.exit(1) def quit(self,obj): gtk.main_quit() sys.exit(1) if __name__ == '__main__': Test() try: gtk.threads_init() except: print "No threading was enabled when you compiled pyGTK!" import sys sys.exit(1) gtk.threads_enter() gtk.main() gtk.threads_leave() The box starts of with 123 in it. If I hit the 'a' key on the key board I get the following result. 123 is printed and 'atest' appears in the text entry box. Is there a way to catch the key_press (in this case the 'a') and check to see if it is a number. If it is not a number, ignore it. If it is a number accept it. Why does the 'a' appear in 'atest'. It is like the code works first, sets the text to 'test' and then the key press works and inserts the 'a'. Do I need to disconnect the key press signal? Thanks, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
[pygtk] FW: [Tutor] Help me make it look pretty!
Good evening all. I am writing a program using python 2.4, glade 2 and pygtk. It takes input from the user using textentry boxes. The input should be a number. When the user keys the data in, it automatically left justifies. The function below, takes the input for four boxes and right justifies it by using an ugly, string format. The boxes are 45 characters big, so I use the len function to fit them into the box. The code is below. I am looking for a way to set the text entry up, so that there is a decimal point in the box regardless of what the user does and I am also looking for a way to keep the numbers right justified. Any suggestions or comments as always are greatly appreciated. Regards, John. def callback36(self,data,text37,text38,text39,text40,text41,text42,label): a = text37.get_text() b = text38.get_text() c = text39.get_text() d = text40.get_text() a= float(a) b= float(b) c= float(c) d= float(d) try: e = float(a + b + c + d) g = e/a e = "%0.2f" % e g = "%0.2f" % g g = str(g) label.hide() e = " %s" % e a = " %s" % a b = " %s" % b c = " %s" % c d = " %s" % d g = "%s%%" % g text42.set_text(str(g)) if len(e)>45: x = len(e) - 45 x = x + 4 y = e[x:] text41.set_text(str(y)) return else: text41.set_text(str(e)) return except: label.show() return ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
[pygtk] 2 Combo Boxes! What was I thinking?
Hi, I have set up a GUI which has amongst other widgets two combo boxes. I am using: PythonCard version: 0.8.1 wxPython version: 2.6.1.0 Python version: 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] Platform: win32 Glade 2 I started by setting up comboboxentry2 to accept two settings in its drop down list. (open + closed). This worked fine. Code below: self.wTree = gtk.glade.XML ("phonelog.glade", "window1") dic={"on_window1_destroy" : self.quit, } self.wTree.signal_autoconnect (dic) combo1 = self.wTree.get_widget("comboboxentry2") combo1.append_text("Open") combo1.append_text("Closed") combo1.set_active(0) combo1.connect("changed", self.callback2, combo1) My problems started when I went to set up comboboxentry3. I used the following code: self.wTree = gtk.glade.XML ("phonelog.glade", "window1") dic={"on_window1_destroy" : self.quit, } self.wTree.signal_autoconnect (dic) combo3 = self.wTree.get_widget("comboboxentry3") combo3.connect("changed", self.callback3, "comboboxentry3") combo3.append_text ("Mon") combo3.append_text ("Tue") combo1 = self.wTree.get_widget("comboboxentry2") combo1.append_text("Open") combo1.append_text("Closed") combo1.set_active(0) combo1.connect("changed", self.callback2, combo1) I got the following error: Gtkwarning: gtk_combo_box_append_text: assertion GTK_IS_LIST_STORE(Combobox->Priv->Model) Failed Combo3.append_text(“Mon”) Gtkwarning: gtk_combo_box_append_text: assertion GTK_IS_LIST_STORE(Combobox->Priv->Model) Failed Combo3.append_text(“Tue”) I then tried the code: self.wTree = gtk.glade.XML ("phonelog.glade", "window1") dic={"on_window1_destroy" : self.quit, } self.wTree.signal_autoconnect (dic) combo3 = self.wTree.get_widget("comboboxentry3") combo3.connect("changed", self.callback3, "comboboxentry3") combo3.child.append_text ("Mon") combo3.child.append_text ("Tue") combo1 = self.wTree.get_widget("comboboxentry2") combo1.append_text("Open") combo1.append_text("Closed") combo1.set_active(0) combo1.connect("changed", self.callback2, combo1) I get the following error message: DeprecationWarning: use GtkEditable.insert_text Combo3.child.append_text(“Mon”) DeprecationWarning: use GtkEditable.insert_text Combo3.child.append_text(“Tue”) The combobox3 is populated on the GUI but instead of being a drop down list it appears as a string on one line and looks like this: MonTue I have to confess I am not really sure what I am doing. I just guessed at putting the child command in. It gets me close but not close enough. Can anyone tell me what I am doing wrong or explain what happens when you put two combo boxes on the one GUI? Any help greatly appreciated. Thanks, John. ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/