我按照本教程http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot,目標是我有一大堆的URL在列表中,我需要返回一個爲我的下一個Python類,以便我可以刮來自該頁面的統計信息。我安裝我的代碼昨晚並確保它的工作沒有任何麻煩,但是當我今天早上回來吧我收到以下內容:Python程序錯誤「列表」對象不可調用
/usr/bin/python2.7 /home/student/Project/country_selection.py
Traceback (most recent call last):
File "/home/student/Project/country_selection.py", line 39, in <lambda>
self.ui.irelandButton.clicked.connect(lambda: self.country_url(11))
TypeError: 'list' object is not callable
Process finished with exit code 0
這是我使用的這個項目我將大大代碼欣賞可用的輸入。
import sys
from PyQt4 import QtCore, QtGui
from country_selection_ui import Ui_CricketStats
class CountryPlayer(QtGui.QMainWindow):
"""
This program is used to return the url required for the player selection program
each button on the GUI represents a different country and is defined so that when
it is clicked it will pass a variable to the country_url method which in turn
will then use the variable to return a url.
"""
def __init__(self, parent=None):
"""
In this method I have used the lambda function so that I can push the variable
required in the country_url. Lambda is a built in tool in Python and is there
for building functions. I once asked Darren Dowdall to explain what lambda was
and his explanation seems to fit best with me "It is a throw away function". We
use lambda function once then it is gone unlike def function which can be used
over and over again.
:param parent:
"""
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_CricketStats()
self.ui.setupUi(self)
# what the button does when pushed
self.ui.australiaButton.clicked.connect(lambda: self.country_url(0))
self.ui.indiaButton.clicked.connect(lambda: self.country_url(1))
self.ui.englandButton.clicked.connect(lambda: self.country_url(2))
self.ui.pakistanButton.clicked.connect(lambda: self.country_url(3))
self.ui.southafricaButton.clicked.connect(lambda: self.country_url(4))
self.ui.newzealandButton.clicked.connect(lambda: self.country_url(5))
self.ui.srilankaButton.clicked.connect(lambda: self.country_url(6))
self.ui.westindiesButton.clicked.connect(lambda: self.country_url(7))
self.ui.zimbabweButton.clicked.connect(lambda: self.country_url(8))
self.ui.bangladeshButton.clicked.connect(lambda: self.country_url(9))
self.ui.kenyaButton.clicked.connect(lambda: self.country_url(10))
self.ui.irelandButton.clicked.connect(lambda: self.country_url(11))
self.ui.canadaButton.clicked.connect(lambda: self.country_url(12))
self.ui.netherlandsButton.clicked.connect(lambda: self.country_url(13))
self.ui.scotlandButton.clicked.connect(lambda: self.country_url(14))
self.ui.afghanistanButton.clicked.connect(lambda: self.country_url(15))
self.ui.usaButton.clicked.connect(lambda: self.country_url(16))
def country_url(self, n):
"""
This method is used to return the url required for the player selection program.
:param n: is the number which is directly related to the country url position in the
self.country_url list.
:return:
"""
self.country_url = ["http://www.espncricinfo.com/australia/content/player/country.html?country=2",
"http://www.espncricinfo.com/india/content/player/country.html?country=6",
"http://www.espncricinfo.com/england/content/player/country.html?country=1",
"http://www.espncricinfo.com/pakistan/content/player/country.html?country=7",
"http://www.espncricinfo.com/southafrica/content/player/country.html?country=3",
"http://www.espncricinfo.com/newzealand/content/player/country.html?country=5",
"http://www.espncricinfo.com/srilanka/content/player/country.html?country=8",
"http://www.espncricinfo.com/westindies/content/player/country.html?country=4",
"http://www.espncricinfo.com/zimbabwe/content/player/country.html?country=9",
"http://www.espncricinfo.com/bangladesh/content/player/country.html?country=25",
"http://www.espncricinfo.com/kenya/content/player/country.html?country=26",
"http://www.espncricinfo.com/ireland/content/player/country.html?country=29",
"http://www.espncricinfo.com/canada/content/player/country.html?country=17",
"http://www.espncricinfo.com/netherlands/content/player/country.html?country=15",
"http://www.espncricinfo.com/scotland/content/player/country.html?country=30",
"http://www.espncricinfo.com/afghanistan/content/player/country.html?country=40",
"http://www.espncricinfo.com/usa/content/player/country.html?country=11"]
return self.country_url[n]
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = CountryPlayer()
myapp.show()
sys.exit(app.exec_())
在情況下,我也忍了其與PyQt4的在以下鏈接使我的GUI代碼:https://paste.ee/p/OwAcw,因爲我在這一點上完全喪失。我不知道爲什麼它拋出錯誤'list'對象不可調用。我還將括號更改爲[],但出現以下錯誤:
/usr/bin/python2.7 /home/student/Project/country_selection.py
Traceback (most recent call last):
File "/home/student/Project/country_selection.py", line 39, in <lambda>
self.ui.irelandButton.clicked.connect(lambda: self.country_url[11])
TypeError: 'instancemethod' object has no attribute '__getitem__'
再次感謝您的任何輸入。
您似乎使用'country_url'作爲方法名稱'def country_url(self,n)'和一個屬性''self.country_url = ...',這可能會導致衝突。 – asongtoruin