[Tkinter-discuss] Running system calls through TKinter

Guilherme Polo ggpolo at gmail.com
Fri Jan 30 18:52:39 CET 2009


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.

> or
> does anyone know a way of outputting system calls in to a tkinter window in
> realtime, one line at a time.
>
> I think I have explained my problem really badly, so sorry.

Actually I think you did well explaining it ;)

> I have searched
> online and all I seem to get is the same use popen. Maybe I don't understand
> the popen stuff well enough, but its time for another shot. This little DOS
> prompt by the side of my app is getting on my nerves.
>
> Any Suggestions?, Im lost!
>

Oh well, let me start it and someone with more Windows (got one of
these yesterday, so now I can at least test code there :) experience
can improve it. The code is ugly on purpose so you may resist to just
copy and paste it, and it is also an indication that it might not
work!


import subprocess
import Tkinter

from win32pipe import PeekNamedPipe
import msvcrt

root = Tkinter.Tk()

proc = subprocess.Popen(['c:/python26/python.exe', '-u', 'ex1.py'],
                       stdout=subprocess.PIPE)
out = proc.stdout

hndl = msvcrt.get_osfhandle(out.fileno())

def check_output():
    retcode = proc.poll()
    # using PeekNamedPipe to check for data available -- it returns immediately
    # (at least in single thread applications)
    _, avail, _ = PeekNamedPipe(hndl, 0)

    if avail:
        text.insert('end', out.read(avail))

    if retcode is not None:
        return
    else:
        text.after(100, check_output)

text = Tkinter.Text()
text.pack()
text.after_idle(check_output)

root.mainloop()


The sample used, ex1.py, is this one:

import time

print "hi"
time.sleep(2)
print "there"


Note that I have cheated by using a sample that happens to be a python
file and I "just happened" to run python in unbuffered mode. This is
where you will likely have problems, your .exe probably doesn't flush
after every single line. But hopefully this help you in some way.

> Many Thanks,
>
> Matt
>



-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list