Pregunta

Todavía estoy aprendiendo Python y PyQt4, parece que no puedo mostrar nada en la ventana de mi GUI cuando aparece " Harvest " Se pulsa el botón. Resalté en negrita mi falta de conocimiento sobre señales y ranuras.

Código actualizado:

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_()
¿Fue útil?

Solución 2

Resuelto:

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

Otros consejos

me parece que la definición del método " one " está mal sangrado.

en su muestra, se ha declarado como una subfunción de TestApp. init (), por lo que desde fuera no puede llamar a one (). intente desmarcar la definición de one () para convertirlo en un método de la clase TestApp.

Solo como referencia, para la conexión de las señales a las ranuras, puede usar más " pythonic " forma:

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

Va ??así:

widget.signal.connect(slot)

Puede encontrar más información aquí

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top