473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Guification of console app

Say I have a console app that does something in three steps:
- opens a file
- transfers the file through a serial port
- does some elaborations

and I want to build a GUI around it that, for example, sets the file
name to open or starts the different steps.

I started using wxPython (actually boa-constructor) to build a frame
with everything I want.
The problem is: when the console app method is working, the GUI is
locked...

How can the console app communicate with the GUI in order to make it
tick (think of status messages, progress bars, log messages...)?
If I use threads and events, or wxYield, or EVT_IDLE, I'm tweaking the
console app so that it won't work just in console anymore.

How do you do that?

Thanks!

Nov 24 '05 #1
6 1661
metiu wrote:
Say I have a console app that does something in three steps:
- opens a file
- transfers the file through a serial port
- does some elaborations

and I want to build a GUI around it that, for example, sets the file
name to open or starts the different steps.

I started using wxPython (actually boa-constructor) to build a frame
with everything I want.
The problem is: when the console app method is working, the GUI is
locked...

How can the console app communicate with the GUI in order to make it
tick (think of status messages, progress bars, log messages...)?
If I use threads and events, or wxYield, or EVT_IDLE, I'm tweaking the
console app so that it won't work just in console anymore.


Could you please explain what makes this a "console" app, specifically?
You are proposing wrapping a GUI around it, which would make it not a
console app (by definition), so it would help if you specified exactly
what elements (which functions, etc.) cause you to label it a "console"
app. (For example, maybe it calls msvcrt.kbhit/getch or something...)

The reason for asking is that the specifics of your answer will make it
easier to describe how to modify this "console" so that the relevant
parts work equally well as a _real_ console app (no GUI) or with the
GUI. We could describe it in more general terms, but it might not be
apparent how to adapt that to your own case.

-Peter

Nov 25 '05 #2
metiu wrote:
Say I have a console app that does something in three steps:
- opens a file
- transfers the file through a serial port
- does some elaborations

and I want to build a GUI around it that, for example, sets the file
name to open or starts the different steps.

I started using wxPython (actually boa-constructor) to build a frame
with everything I want.
The problem is: when the console app method is working, the GUI is
locked...

How can the console app communicate with the GUI in order to make it
tick (think of status messages, progress bars, log messages...)?
If I use threads and events, or wxYield, or EVT_IDLE, I'm tweaking the
console app so that it won't work just in console anymore.

How do you do that?

Thanks!

I've achieved this in the past by having the GUI trigger the console app
in a separate thread and then use a Queue.Queue to send output from the
console app to the GUI. As long as the GUI portion is careful never to
block on an empty Queue this can work quite well.

There's also a cookbook recipe by Jacob Hallen that shows you how to
keep a Tkinter GUI running with parallel worker threads.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 25 '05 #3
Yes, I'll try to give you an example:
you have a compression utility that works as a standard *nix filter, so
it takes something from stdin and gives it back compressed to stdout
you like to use it as such, because it's nice to call it from the
command line

now someone finds your utility quite nice, and says it would be nice to
have a GUI that shows you, for example, how long it will take to
compress...

one way for sure it would be to fork your app, but this would be a
waste of time

you'd like to reuse the compression library you've already written for
your GUI and your console, but:
- you'd like to have your console app clean and simple, such as:

import sys
import CompressLib

data = sys.stdin.read()
cdata = CompressLib.compress(data)
print cdata

but you'd like the GUI to show some progress and status info.

I guess the way is still to make the compress call split in microsteps
and have the app call it, so you can use an EVT_IDLE call from the GUI,
but maybe there's a better way...

Thanks again

Peter Hansen ha scritto:
metiu wrote:
Say I have a console app that does something in three steps:
- opens a file
- transfers the file through a serial port
- does some elaborations

and I want to build a GUI around it that, for example, sets the file
name to open or starts the different steps.

I started using wxPython (actually boa-constructor) to build a frame
with everything I want.
The problem is: when the console app method is working, the GUI is
locked...

How can the console app communicate with the GUI in order to make it
tick (think of status messages, progress bars, log messages...)?
If I use threads and events, or wxYield, or EVT_IDLE, I'm tweaking the
console app so that it won't work just in console anymore.


Could you please explain what makes this a "console" app, specifically?
You are proposing wrapping a GUI around it, which would make it not a
console app (by definition), so it would help if you specified exactly
what elements (which functions, etc.) cause you to label it a "console"
app. (For example, maybe it calls msvcrt.kbhit/getch or something...)

The reason for asking is that the specifics of your answer will make it
easier to describe how to modify this "console" so that the relevant
parts work equally well as a _real_ console app (no GUI) or with the
GUI. We could describe it in more general terms, but it might not be
apparent how to adapt that to your own case.

-Peter


Nov 25 '05 #4
"metiu" wrote:
you have a compression utility that works as a standard *nix filter, so
it takes something from stdin and gives it back compressed to stdout
you like to use it as such, because it's nice to call it from the
command line

now someone finds your utility quite nice, and says it would be nice to
have a GUI that shows you, for example, how long it will take to
compress...

one way for sure it would be to fork your app, but this would be a
waste of time

you'd like to reuse the compression library you've already written for
your GUI and your console, but:
- you'd like to have your console app clean and simple, such as:

import sys
import CompressLib

data = sys.stdin.read()
cdata = CompressLib.compress(data)
print cdata

but you'd like the GUI to show some progress and status info.


here's a simple, stupid, and portable solution. the first script simulates
your compression utility:

# compress.py (simulator)

import time, sys

for i in range(100):
sys.stdout.write(".")
sys.stderr.write("%d%% done\r" % i)
sys.stderr.flush()
time.sleep(0.1)

(it prints "N% done" messages to stderr during the compression)

the second script simulates your GUI. the stuff in the while loop should
be run by a timer/alarm function, at regular intervals:

import re, subprocess, time, os

class monitor:
# looks for "N% done" messages in the output stream
def __init__(self, tfile):
# could use dup/reopen instead
self.file = open(tfile.name, "r")
def poll(self):
pos = self.file.tell()
data = self.file.read()
if data:
data = re.findall("(\d+)% done", data)
if not data:
self.file.seek(pos)
else:
return int(data[-1])
def close(self):
self.file.close()

ifile = open("in.txt", "rb")
ofile = open("out.dat", "wb")
tfile = open("out.tmp~", "wb")

p = subprocess.Popen(
"python compress.py",
stdin=ifile, stdout=ofile, stderr=tfile,
)

m = monitor(tfile)

while 1:
# this should be placed in a background task that's called
# every second or so
if p.poll() is not None:
print "DONE"
break
status = m.poll()
if status is not None:
# update status monitor
print status, "PERCENT DONE"
# wait a while before calling the background task again
print "."
time.sleep(0.5)

# clean up
ifile.close()
ofile.close()
m.close()
name = tfile.name
tfile.close()
os.remove(name)

if you limit yourself to Unix only, you can simplify things quite a bit (e.g.
using a pipe instead of the temporary file and use select to poll it, or use
dup/reopen tricks to avoid opening the temporary file twice; if you do
the latter, you can also use safe tempfile creation methods (see the
"tempfile" method for details. etc).

hope this helps!

</F>

Nov 25 '05 #5
I like it! Thanks!

Nov 25 '05 #6
On 11/25/05, Fredrik Lundh <fr*****@pythonware.com> wrote:
"metiu" wrote:
you have a compression utility that works as a standard *nix filter, so
it takes something from stdin and gives it back compressed to stdout
you like to use it as such, because it's nice to call it from the
command line

now someone finds your utility quite nice, and says it would be nice to
have a GUI that shows you, for example, how long it will take to
compress...

one way for sure it would be to fork your app, but this would be a
waste of time

you'd like to reuse the compression library you've already written for
your GUI and your console, but:
- you'd like to have your console app clean and simple, such as:

import sys
import CompressLib

data = sys.stdin.read()
cdata = CompressLib.compress(data)
print cdata

but you'd like the GUI to show some progress and status info.
here's a simple, stupid, and portable solution. the first script simulates
your compression utility:

# compress.py (simulator)

import time, sys

for i in range(100):
sys.stdout.write(".")
sys.stderr.write("%d%% done\r" % i)
sys.stderr.flush()
time.sleep(0.1)

(it prints "N% done" messages to stderr during the compression)

the second script simulates your GUI. the stuff in the while loop should
be run by a timer/alarm function, at regular intervals:

import re, subprocess, time, os

class monitor:
# looks for "N% done" messages in the output stream
def __init__(self, tfile):
# could use dup/reopen instead
self.file = open(tfile.name, "r")
def poll(self):
pos = self.file.tell()
data = self.file.read()
if data:
data = re.findall("(\d+)% done", data)
if not data:
self.file.seek(pos)
else:
return int(data[-1])
def close(self):
self.file.close()

ifile = open("in.txt", "rb")
ofile = open("out.dat", "wb")
tfile = open("out.tmp~", "wb")

p = subprocess.Popen(
"python compress.py",
stdin=ifile, stdout=ofile, stderr=tfile,
)

m = monitor(tfile)

while 1:
# this should be placed in a background task that's called
# every second or so
if p.poll() is not None:
print "DONE"
break
status = m.poll()
if status is not None:
# update status monitor
print status, "PERCENT DONE"
# wait a while before calling the background task again
print "."
time.sleep(0.5)

# clean up
ifile.close()
ofile.close()
m.close()
name = tfile.name
tfile.close()
os.remove(name)

if you limit yourself to Unix only, you can simplify things quite a bit (e.g.
using a pipe instead of the temporary file and use select to poll it, or use
dup/reopen tricks to avoid opening the temporary file twice; if you do
the latter, you can also use safe tempfile creation methods (see the
"tempfile" method for details. etc).

hope this helps!

</F>


If you are using wxPython, you can skip writing this scaffolding
yourself and use wx.Execute and wx.Process, which will allow you to
execute other processes and re-direct thier input and output. The
wxPython demo has an example.

--
http://mail.python.org/mailman/listinfo/python-list

Nov 25 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
7
by: shawnk | last post by:
Hello Everyone How do you format format numbers right-justified using Console.WriteLine(), i.e I need to line up numbers in vertical columns and the MSDN documentation is pretty poor Here is the...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
5
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
5
by: portroe | last post by:
Hi I am using console.Writeline in my simple program. I do not however see anything happening in the output window when I debug, there are also no error messages, Has anybody a tip on what...
6
by: tony | last post by:
Hello! When you have windows forms you have the same possibility as when you have a Console application to use Console.Writeln to write whatever on the screen. Now to my question: Is it...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.