pyqt4: setText() inside a function

L

l.freschi

I'm developing a PyQt4 application.

I have created a button:
....
self.start_button=QtGui.QPushButton("start simulation", self)
....

that is connected to a function:
....
self.connect(self.start_button, QtCore.SIGNAL('clicked()'),
self.simulate)
....

This is the function:
....
def simulate(self):

self.log_inspector.setText('')
cmds=['rm engine','make engine', './
engine']
first_cmd="./parser "+str(self.filename)
cmds.insert(0, first_cmd)
for cmd in cmds:
self.status_inspector.setText(cmd)
status, output = commands.getstatusoutput(cmd)
output_list=output.split("\n")
output_list.reverse()
output_def="\n".join(output_list)
if status != 0:
self.log_inspector.setText(cmd"...
[ERROR]\n"+output_def)
return
self.status_inspector.setText("Done!")
....

I would like to change the value of status_inspector (It's a QLabel)
during the execution of the function.
Is it possible?

I'm a newbie with PyQt and OOP!

Thank you!

Luca
 
D

Diez B. Roggisch

I'm developing a PyQt4 application.

I have created a button:
...
self.start_button=QtGui.QPushButton("start simulation", self)
...

that is connected to a function:
...
self.connect(self.start_button, QtCore.SIGNAL('clicked()'),
self.simulate)
...

This is the function:
...
def simulate(self):

self.log_inspector.setText('')
cmds=['rm engine','make engine', './
engine']
first_cmd="./parser "+str(self.filename)
cmds.insert(0, first_cmd)
for cmd in cmds:
self.status_inspector.setText(cmd)
status, output = commands.getstatusoutput(cmd)
output_list=output.split("\n")
output_list.reverse()
output_def="\n".join(output_list)
if status != 0:
self.log_inspector.setText(cmd"...
[ERROR]\n"+output_def)
return
self.status_inspector.setText("Done!")
...

I would like to change the value of status_inspector (It's a QLabel)
during the execution of the function.
Is it possible?

If you want GUI-updates while performing time-consuming tasks, you need
to make sure Qt's event-loop get's called every now and then. This
should help you:

void QCoreApplication::processEvents ( QEventLoop::processEventsFlags
flags = QEventLoop::AllEvents ) [static]


Diez
 
L

l.freschi

(e-mail address removed) schrieb:


I'm developing a PyQt4 application.
I have created a button:
...
self.start_button=QtGui.QPushButton("start simulation", self)
...
that is connected to a function:
...
self.connect(self.start_button, QtCore.SIGNAL('clicked()'),
self.simulate)
...
This is the function:
...
def simulate(self):
                self.log_inspector.setText('')
                cmds=['rm engine','make engine', './
engine']
                first_cmd="./parser "+str(self.filename)
                cmds.insert(0, first_cmd)
                for cmd in cmds:
                        self.status_inspector.setText(cmd)
                        status, output = commands.getstatusoutput(cmd)
                        output_list=output.split("\n")
                        output_list.reverse()
                        output_def="\n".join(output_list)
                        if status != 0:
                                self.log_inspector.setText(cmd"...
[ERROR]\n"+output_def)
                                return
                self.status_inspector.setText("Done!")
...
I would like to change the value of status_inspector (It's a QLabel)
during the execution of the function.
Is it possible?

If you want GUI-updates while performing time-consuming tasks, you need
to make sure Qt's event-loop get's called every now and then. This
should help you:

void QCoreApplication::processEvents ( QEventLoop::processEventsFlags
flags = QEventLoop::AllEvents )   [static]

Diez


It works! I added:
application_object.processEvents()


Thank you Diez!


def simulate(self):

self.log_inspector.setText('')
cmds=['rm engine','make engine', './
engine']
first_cmd="./parser "+str(self.filename)
cmds.insert(0, first_cmd)
for cmd in cmds:
self.status_inspector.setText(cmd)
application_object.processEvents()
status, output = commands.getstatusoutput(cmd)
output_list=output.split("\n")
output_list.reverse()
output_def="\n".join(output_list)
if status != 0:
self.log_inspector.setText(cmd"...
[ERROR]\n"+output_def)
return
self.status_inspector.setText("Done!")
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top