[Tkinter-discuss] Running system calls through TKinter

Guilherme Polo ggpolo at gmail.com
Fri Jan 30 20:17:17 CET 2009


On Fri, Jan 30, 2009 at 3:52 PM, Guilherme Polo <ggpolo at gmail.com> wrote:
> On Fri, Jan 30, 2009 at 11:59 AM, AbsoluteMatt <mattx_13 at hotmail.com> wrote:
>>
>> Hi All,
>>
>> I have been writing a little app in python/tkinter to allow non technical
>> users at my office setup/restore manipulate Oracle databases. I am using
>> cx_Oracle plugin as well.
>>
>> The app works fine at the moment, doing everything I want it to. However
>> when it comes to importing a db I am just using imp.exe and dpimp.exe
>> basically making external system calls to run it. I know this is not ideal
>> and I should probably be using popen etc, but I am not to sure on how to get
>> it working. I tried a while ago and it broke more than it fixed so I paniced
>> and went back to good old command prompt method.
>>
>> I want to be able to view the output of an oracle import in realtime,
>
> Tricky!
>
>> anyone
>> that has seen imp/exp knows that when running on command line you can see
>> the tables importing as they import line by line. Ideally I would like to
>> have a "process" tab on my tkinter app rather than a seperate DOS prompt
>> running the command, where you can see this execution.
>>
>> I cant remember exactly what I tried before, but I think it involved using
>> popen and stdout and then printing stdout. However when I did it like this
>> it used to print out output in bulk blocks not nicely one line at a time.
>>
>> So.... Is there anyway to embed a command window inside a tkinter window,
>
> I don't think so, maybe something like powershell might be able. It
> would be easy to do it with xterm, for example, since it has support
> for a -into option.

I just noticed there is a win32process module included with pywin32,
so it must be possible. Better than that, there is something ready
already called pyconsole. It is found it here:
http://code.google.com/p/pyconsole/

I got it and tried doing an example with it:

import Tkinter
import pyconsole

class ConsoleProcessWindow(Tkinter.Text):
	def __init__(self, master=None):
		Tkinter.Text.__init__(self)

		self.console_process = pyconsole.ConsoleProcess('cmd.exe',
				console_update=self.console_update)

		self.last_line = 0

	def test(self):
		self.console_process.write('dir\n')

	def console_update(self, x, y, text):
		self.after_idle(lambda: self.update_text(x, y, text))

	def update_text(self, x, y, text):
		y += 1
		for i in xrange(y - self.last_line):
			self.insert('%d.0' % (self.last_line + i + 1), '\n')
		self.last_line = y
		self.insert('%d.%d' % (y, x), text)
		self.update_idletasks()


class App(Tkinter.Frame):
	def __init__(self):
		Tkinter.Frame.__init__(self)

		self.console = ConsoleProcessWindow(self)
		self.console.pack(side='left', fill='both', expand=True)
		vscroll = Tkinter.Scrollbar(orient='vertical',
				command=self.console.yview)
		self.console['yscrollcommand'] = vscroll.set
		vscroll.pack(side='right', fill='y')

		self.after(500, self.console.test)

root = Tkinter.Tk()
app = App()
app.pack(fill='both', expand=True)
root.mainloop()


And it works! So you just need to send things to this ConsoleProcess
like you would do in your typical command prompt.

-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list