[pygtk] Implementing command history with a gtk.Entry
N. French
nbm_clan at yahoo.com
Tue Jul 25 14:53:52 WST 2006
Um, no I don't want completion behavior, I want to capture the up/down
arrows from the gtk.Entry and act on them.
Thanks though. Anyone else?
> --- Paul Malherbe <paul at tartan.co.za> wrote:
>
>
> > Hi
> >
> > Here is a class I wrote which might help!
> >
> > Regards
> > > #!/usr/bin/python
> >
> > try:
> > import pygtk
> > pygtk.require("2.0")
> > import gtk, os, popen2, sys, time
> > except:
> > sys.exit()
> >
> > class RunPrg:
> > def __init__(self, cmd):
> > if cmd:
> > self.runCmd(cmd)
> > else:
> > self.runProcess()
> >
> > def runProcess(self):
> > win = gtk.Window()
> > win.connect("destroy", self.destroy)
> > win.connect("delete_event", self.destroy)
> > win.connect("key_press_event", self.keyPress)
> > hor = gtk.HBox(False, 0)
> > lab = gtk.Label("Command")
> > self.ent = gtk.Entry()
> > self.aut = gtk.EntryCompletion()
> > self.lst = gtk.ListStore(str)
> > self.aut.set_model(self.lst)
> > self.ent.set_completion(self.aut)
> > self.aut.set_text_column(0)
> > self.openFile()
> > self.ent.connect("activate", self.getCmd, self.ent)
> > self.aut.connect("match-selected", self.getCmd, self.ent)
> > if self.last:
> > self.ent.set_text(self.last)
> > chk = gtk.CheckButton("Xterm")
> > chk.connect("toggled", self.getCmd, self.ent)
> > hor.pack_start(lab, False, False, 5)
> > hor.pack_start(self.ent, False, False, 5)
> > hor.pack_start(chk, False, False, 5)
> > win.add(hor)
> > win.show_all()
> > win.set_position(gtk.WIN_POS_CENTER_ALWAYS)
> > gtk.main()
> >
> > def keyPress(self, widget, data):
> > while gtk.events_pending():
> > gtk.main_iteration()
> > keyname = gtk.gdk.keyval_name(data.keyval)
> > if keyname == "Escape":
> > self.destroy()
> > elif keyname not in ("Up", "Down"):
> > return
> > elif not self.data:
> > return
> > if keyname == "Up":
> > if self.pos == len(self.data):
> > pass
> > else:
> > self.pos += 1
> > elif self.pos:
> > self.pos -= 1
> > if self.pos == len(self.data):
> > self.ent.set_text(self.last)
> > else:
> > self.ent.set_text(self.data[self.pos])
> > self.ent.grab_focus()
> >
> > def openFile(self):
> > self.lst.clear()
> > self.data = []
> > self.last = None
> > home = os.environ.get("HOME")
> > if not os.path.isfile("%s/.runprgrc" % home):
> > self.file = open("%s/.runprgrc" % home, "w")
> > else:
> > self.file = open("%s/.runprgrc" % home, "r+")
> > old = self.file.readlines()
> > for line in old:
> > self.last = line.replace("\n", "")
> > if not self.data.count(self.last):
> > self.data.append(self.last)
> > self.lst.append([self.last])
> > self.pos = len(self.data)
> >
> > def destroy(self, *args):
> > gtk.main_quit()
> > sys.exit()
> >
> > def getCmd(self, *args):
> > gtk.main_quit()
> > while gtk.events_pending():
> > gtk.main_iteration()
> > if type(args[0]) == gtk.CheckButton:
> > xtm = args[0].get_active()
> > cmd = args[1].get_text()
> > elif type(args[0]) == gtk.EntryCompletion:
> > xtm = False
> > cmd = args[1][args[2]][0]
> > else:
> > xtm = False
> > cmd = args[1].get_text()
> > # Check if valid command and on path
> > prg = cmd.split()[0]
> > if os.path.exists(prg):
> > yes = True
> > else:
> > yes = False
> > path = os.environ["PATH"].split(":")
> > for p in path:
> > if os.path.exists(os.path.join(p, prg)):
> > yes = True
> > # Execute if valid
> > if yes:
> > if xtm:
> > err = self.runCmd("exec xterm -e '%s|less'" % cmd)
> > else:
> > err = self.runCmd("%s" % cmd)
> > if not err:
> > self.file.write("%s\n" % cmd)
> > # Exit
> > self.file.close()
> > sys.exit()
> >
> > def runCmd(self, cmd):
> > try:
> > fl1 = popen2.Popen3(cmd, True)
> > except:
> > return "error"
> >
> > if __name__ == "__main__":
> > if len(sys.argv) > 1:
> > inn = sys.argv[1:]
> > cmd = inn[0]
> > for c in inn[1:]:
> > cmd = "%s %s" % (cmd, c)
> > else:
> > cmd = None
> > RunPrg(cmd)
> >
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the pygtk
mailing list