[Tutor] Any Suggestions?

alan.gauld@bt.com alan.gauld@bt.com
Sun Nov 17 12:22:01 2002


> I'm just learning Python and would appreciate any suggestions 
> regarding the programs listed below.  The programs do function.
> 
> runit2.py
> #!/usr/bin/env python
> 
> import runit2tk
> from os import system
> from sys import exit
> 
> gui1 = runit2tk.Gui1()
> gui1.mainloop()
> 
> if gui1.text != "":
>     command = "%s &" % gui1.text
>     system(command)

Why not just add an execute button to the GUI to do this? Then you
can execute many comands instead of just one...

> runit2tk.py
> #!/usr/bin/env python
> 
> import Tkinter
> 
> class Gui1:
> 	def __init__(self, text=""):
> 	    self.text = text
>             self.root = Tkinter.Tk()
>             self.root.title('PyRun')
>             self.label = Tkinter.Label(self.root, text="Run")
>             self.label.pack(side="left")
>             self.entry = Tkinter.Entry(self.root, takefocus="true")
>             self.entry.pack(side="left", fill="x", expand="true")
>             self.entry.bind('<Key-Return>', self.getinput)
>             self.entry.focus()

You could add a Text widget to capture the output of the executed command

> 
>         def mainloop(self):
> 	        self.root.mainloop()
> 
>         def getinput(self, event):
>             self.text = self.entry.get()
>             self.root.destroy()
>             return self.text

returning a value from an event handler is pointless...


Add your execution code here but use popen() instead of system() 
to capture the results and dump them into the Text widget

Just some ideas. Assuming you want a GUI wrapper to a command 
interpreter... :-)

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld