0

私はまだPythonとPyQt4を学んでいますが、[Harvest]ボタンを押しても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_()
4

3 に答える 3

1

解決済み:

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_()
于 2009-10-25T21:25:30.907 に答える
1

メソッド「one」の定義がひどくインデントされているように私には思えます。

サンプルでは、​​TestAppのサブ関数として宣言されています。init()なので、外部からone()を呼び出すことはできません。one()の定義のインデントを解除して、TestAppクラスのメソッドにしてみてください。

于 2009-10-25T07:26:55.540 に答える
1

参考までに、信号をスロットに接続するには、より「pythonic」形式を使用できます。

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

こんなふうになります:

widget.signal.connect(slot)

あなたはここでより多くの情報を見つけるかもしれません

于 2011-02-02T09:34:37.667 に答える