[PyQt] PyQt4 - Display text on GUI Window:

David Boddie david at boddie.org.uk
Sun Oct 25 20:37:20 GMT 2009


On Sun Oct 25 20:19:56 GMT 2009, M.Chavez wrote:

> I think I'm getting closer, here is an updated version with all the
> SQLite3 taken out. Everything loads fine, but nothing gets sent to the
> MainWindow.

 class gameWindow(QtGui.QMainWindow):
     def __init__(self, parent=None):
         super(gameWindow, self).__init__(parent)
         QtGui.QMainWindow.__init__(self)
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self)

         buttonHarvest = QPushButton("Harvest") #Create the harvest button -
                                                # but QT Designer made it?
         buttonMining = QPushButton("Mining") # Create the mining button -
                                              # but QT Designer made it?

If these buttons have suitable names in Qt Designer then you should be able
to refer to the existing self.ui.buttonHarvest and self.ui.buttonMining
objects. You do not need to create new buttons.

         self.label = QLabel("Example") # Set the empty label that's not
                                        # showing

Did you also create a label in Qt Designer for this purpose? If so, then you
can just refer to that.

>         self.connect(buttonHarvest, SIGNAL("clicked()"), self.skillHarvest)
[...]
>         self.connect(buttonMining, SIGNAL("clicked()"), self.skillMining)

You can connect the signal from the existing self.ui.buttonHarvest and
self.ui.buttonMining objects to the slots. Alternatively, you can use the
automatic connection feature of setupUi() and give the slots appropriate
names. From memory, something like this should work:

     def on_buttonHarvest_clicked(self):
         harvest = "You find some roots."
         self.label.setText(harvest)

     def on_buttonMining_clicked(self):
         mining = "You found some gold."
         self.label.setText(mining)

If you use this naming convention, don't use the code given above to connect
the signals from the buttons to the slots - it will cause the slots to be
called twice for each button click.

David


More information about the PyQt mailing list