Question

J'apprends toujours Python et PyQt4. Je ne parviens tout simplement pas à afficher quoi que ce soit sur la fenêtre de mon interface graphique lorsque le message "Harvest" est affiché. bouton est enfoncé. J'ai souligné en gras mon manque de connaissances sur les signaux et les créneaux.

Code mis à jour:

import sys, random, sqlite3, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
from geodesic import Ui_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?
        self.label = QLabel("Example") # Set the empty label that's not showing

        self.connect(buttonHarvest, SIGNAL("clicked()"), self.skillHarvest) #Gets from def skillHarvest
        self.setWindowTitle("Geodesic")
        # Next -------------------------------------------------------------------------------------
        self.connect(buttonMining, SIGNAL("clicked()"), self.skillMining) #Gets from def skillMining

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

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

app = QApplication(sys.argv)
showWindow = gameWindow()
showWindow.show()
app.exec_()
Était-ce utile?

La solution 2

Résolu:

import sys, random, sqlite3, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
from geodesic import Ui_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 = self.ui.buttonHarvest
        buttonMining = self.ui.buttonMining
        #showLabel = self.ui.label

        self.connect(buttonHarvest, SIGNAL("clicked()"), self.onButtonHarvest)
        # Next
        self.connect(buttonMining, SIGNAL("clicked()"), self.onButtonMining)

    def onButtonHarvest(self):
        harvest = "You find some roots."
        showLabel = self.ui.label
        showLabel.setText(harvest)

    def onButtonMining(self):
        mining = "You found some gold."
        showLabel = self.ui.label
        showLabel.setText(mining)

app = QApplication(sys.argv)
showWindow = gameWindow()
showWindow.show()
app.exec_()

Autres conseils

il me semble que la définition de la méthode " one " est mal mis en retrait.

sur votre exemple, il a été déclaré en tant que sous-fonction de TestApp. init (); vous ne pouvez donc pas appeler one () de l'extérieur. essayez de désindenter la définition de one () pour en faire une méthode de la classe TestApp.

Juste pour référence, pour la connexion des signaux aux slots, vous pouvez utiliser le plus "pythonique". forme:

buttonHarvest.clicked.connect(self.onButtonHarvest)
buttonMining.clicked.connect(self.onButtonMining)

Cela ressemble à ceci:

widget.signal.connect(slot)

Vous pouvez trouver plus d'informations ici

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top