我还在学习Python和PyQt4,当“收获”时,我似乎无法在我的gui窗口上显示任何内容。按钮被按下。我粗略地强调了我对信号和插槽缺乏了解。

更新代码:

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_()
有帮助吗?

解决方案 2

解决:

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

其他提示

在我看来,方法的定义是“一”。非常缩进。

在您的示例中,它已被声明为TestApp的子功能。 init (),因此从外部您无法调用one()。尝试取消one()的定义,使其成为TestApp类的方法。

仅供参考,为了将信号连接到插槽,您可以使用更多的“pythonic”。形式:

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

它是这样的:

widget.signal.connect(slot)

您可以在此处找到更多信息

scroll top