pyqt4: setText() inside a function

l.freschi at gmail.com l.freschi at gmail.com
Thu Apr 16 16:04:38 EDT 2009


On 16 Apr, 20:17, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> l.fres... at gmail.com 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!")



More information about the Python-list mailing list