2009-10-25 6 views
0

Je suis encore en train d'apprendre Python et PyQt4, je n'arrive pas à trouver quoi que ce soit à afficher sur ma fenêtre GUI lorsque le bouton "Récolter" est poussé. J'ai souligné en gras mon manque de connaissances sur les signaux et les slots.Signaux et Slots PyQt4

code Mise à 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_() 

Répondre

1

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_() 
1

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

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

+0

J'ai apporté quelques modifications au code mais cela ne fonctionne toujours pas. Tout se charge bien et je peux voir la fenêtre avec des boutons attachés. Mais quand les boutons sont activés, rien ne se passe. – Dunwitch

1

Juste pour référence, pour la connexion des signaux à créneaux horaires, vous pouvez utiliser la forme la plus "pythonique":

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

Il va comme ceci:

widget.signal.connect(slot) 

Vous pouvez trouver plus d'informations here

+0

Oh mon glob, je n'ai jamais connu ce format! Merci! –

Questions connexes