output

H

H.S. Art

Hi folks,

I have a sript that runs very well. I do my output to the python shell
with the print command. That works well, too.
print 'Error occured ' + str(i+1)

No I want to have my output into a textctrl frame in a window. I start
my script by pushing a button in my window. It starts fine but I can't
see any output. For test purposes I added the following code to the
event handler of the button:
self.textCtrl1.WriteText('TEST\n')
This produces output to my text ctrl window. So I replaced all 'print'
commands in my script with 'self.textCtrl1.WriteText ...' Unfortunately
it still doesn't work. I don't get any output from my script. I always
get an error saying self is unknown. That's why I replaced it with the
name of the file of the window and so on and so on. Nothing worked. Can
you help me telling me how I have to write my code to get an output?

Thank you very much.
Henry
 
F

Fredrik Lundh

H.S. Art said:
I have a sript that runs very well. I do my output to the python shell
with the print command. That works well, too.
print 'Error occured ' + str(i+1)

No I want to have my output into a textctrl frame in a window. I start
my script by pushing a button in my window. It starts fine but I can't
see any output. For test purposes I added the following code to the
event handler of the button:

self.textCtrl1.WriteText('TEST\n')

This produces output to my text ctrl window. So I replaced all 'print'
commands in my script with 'self.textCtrl1.WriteText ...'

someone else will have to sort the variable scoping issues for you [1],
but in the meantime, you could try the following little trick.

1) at the top of your program, import the "sys" module:

import sys

2) in the event handler, add the following redirection code:

# redirect output to my window
class redirect:
def __init__(self, window):
self.write = window.WriteText
sys.stdout = redirect(self.textCtrl1)

</F>

1) the following links might be somewhat helpful:

http://www.python.org/doc/current/tut/node11.html#SECTION0011200000000000000000
http://www.python.org/doc/current/ref/naming.html (technical)
 
H

H.S. Art

Thank you for your answer!
That "little" trick works perfect for me. :)



Fredrik said:
H.S. Art wrote:


I have a sript that runs very well. I do my output to the python shell
with the print command. That works well, too.
print 'Error occured ' + str(i+1)

No I want to have my output into a textctrl frame in a window. I start
my script by pushing a button in my window. It starts fine but I can't
see any output. For test purposes I added the following code to the
event handler of the button:

self.textCtrl1.WriteText('TEST\n')

This produces output to my text ctrl window. So I replaced all 'print'
commands in my script with 'self.textCtrl1.WriteText ...'

someone else will have to sort the variable scoping issues for you [1],
but in the meantime, you could try the following little trick.

1) at the top of your program, import the "sys" module:

import sys

2) in the event handler, add the following redirection code:

# redirect output to my window
class redirect:
def __init__(self, window):
self.write = window.WriteText
sys.stdout = redirect(self.textCtrl1)

</F>

1) the following links might be somewhat helpful:

http://www.python.org/doc/current/tut/node11.html#SECTION0011200000000000000000
http://www.python.org/doc/current/ref/naming.html (technical)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top