Domanda

Sto ancora imparando Python e PyQt4, non riesco proprio a vedere nulla da mostrare sulla mia finestra della GUI quando " Harvest " viene premuto il pulsante. Ho sottolineato in grassetto la mia mancanza di conoscenza su segnali e slot.

Codice aggiornato:

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_()
È stato utile?

Soluzione 2

Risolto:

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

Altri suggerimenti

mi sembra che la definizione per il metodo "uno" ha un rientro grave.

sul tuo campione, è stato dichiarato come una funzione secondaria di TestApp. init (), quindi dall'esterno non puoi chiamarne uno (). prova a rimuovere la definizione di one () per renderlo un metodo di classe TestApp.

Solo per riferimento, per la connessione dei segnali agli slot è possibile utilizzare più "pitone" forma:

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

Va ??così:

widget.signal.connect(slot)

Puoi trovare maggiori informazioni qui

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top