我有一个问题, 我试图制作一个简单的Python游戏..(你必须按正确的顺序从1到15按下保存在数组中的按钮。
Ť 这是我的View.py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Hauptfenster.ui'
#
# Created: Sun Feb 21 20:43:24 2016
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtGui, QtCore
import Model
import Controller
class Ui_MainWindow(object):
def __init__(self):
self.model= Model.Model()
self.spiele = self.model.getSpiele()
self.offen = self.model.getOffen()
self.korrekt = self.model.getKorrekt()
self.falsch = self.model.getFalsch()
self.gesamt = self.model.getGesamt()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(676, 306)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(100, 70, 561, 121))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.pushButton_4 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout.addWidget(self.pushButton_4, 0, 4, 1, 1)
self.pushButton_10 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_10.setObjectName("pushButton_10")
self.gridLayout.addWidget(self.pushButton_10, 1, 3, 1, 1)
self.pushButton_2 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.pushButton_6 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout.addWidget(self.pushButton_6, 1, 0, 1, 1)
self.pushButton_8 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_8.setObjectName("pushButton_8")
self.gridLayout.addWidget(self.pushButton_8, 1, 2, 1, 1)
self.pushButton_9 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_9.setObjectName("pushButton_9")
self.gridLayout.addWidget(self.pushButton_9, 0, 2, 1, 1)
self.pushButton_11 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_11.setObjectName("pushButton_11")
self.gridLayout.addWidget(self.pushButton_11, 1, 4, 1, 1)
self.pushButton_7 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_7.setObjectName("pushButton_7")
self.gridLayout.addWidget(self.pushButton_7, 1, 1, 1, 1)
self.pushButton_3 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 0, 3, 1, 1)
self.pushButton_13 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_13.setObjectName("pushButton_13")
self.gridLayout.addWidget(self.pushButton_13, 2, 0, 1, 1)
self.pushButton = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_14 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_14.setObjectName("pushButton_14")
self.gridLayout.addWidget(self.pushButton_14, 2, 1, 1, 1)
self.pushButton_15 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_15.setObjectName("pushButton_15")
self.gridLayout.addWidget(self.pushButton_15, 2, 2, 1, 1)
self.pushButton_16 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_16.setObjectName("pushButton_16")
self.gridLayout.addWidget(self.pushButton_16, 2, 3, 1, 1)
self.pushButton_17 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_17.setObjectName("pushButton_17")
self.gridLayout.addWidget(self.pushButton_17, 2, 4, 1, 1)
self.verticalLayoutWidget_2 = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(100, 190, 561, 81))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtGui.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButton_19 = QtGui.QPushButton(self.verticalLayoutWidget_2)
self.pushButton_19.setObjectName("pushButton_19")
self.horizontalLayout.addWidget(self.pushButton_19)
spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.ExitButton = QtGui.QPushButton(self.verticalLayoutWidget_2)
self.ExitButton.setObjectName("ExitButton")
self.horizontalLayout.addWidget(self.ExitButton)
spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem2)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.verticalLayoutWidget_3 = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(100, 10, 561, 73))
self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
self.verticalLayout_3 = QtGui.QVBoxLayout(self.verticalLayoutWidget_3)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.label = QtGui.QLabel(self.verticalLayoutWidget_3)
font = QtGui.QFont()
font.setPointSize(16)
font.setWeight(75)
font.setBold(True)
self.label.setFont(font)
self.label.setCursor(QtCore.Qt.CrossCursor)
self.label.setMouseTracking(False)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.verticalLayout_3.addWidget(self.label)
self.verticalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 61, 261))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout_4 = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.label_2 = QtGui.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setWeight(75)
font.setBold(True)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.verticalLayout_4.addWidget(self.label_2)
self.label_4 = QtGui.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setWeight(75)
font.setBold(True)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.verticalLayout_4.addWidget(self.label_4)
self.label_6 = QtGui.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setWeight(75)
font.setBold(True)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.verticalLayout_4.addWidget(self.label_6)
self.label_8 = QtGui.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setWeight(75)
font.setBold(True)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.verticalLayout_4.addWidget(self.label_8)
self.label_10 = QtGui.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setWeight(75)
font.setBold(True)
self.label_10.setFont(font)
self.label_10.setObjectName("label_10")
self.verticalLayout_4.addWidget(self.label_10)
self.verticalLayoutWidget_4 = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget_4.setGeometry(QtCore.QRect(70, 10, 31, 261))
self.verticalLayoutWidget_4.setObjectName("verticalLayoutWidget_4")
self.verticalLayout_5 = QtGui.QVBoxLayout(self.verticalLayoutWidget_4)
self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.label_3 = QtGui.QLabel(self.verticalLayoutWidget_4)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.verticalLayout_5.addWidget(self.label_3)
self.label_5 = QtGui.QLabel(self.verticalLayoutWidget_4)
self.label_5.setAlignment(QtCore.Qt.AlignCenter)
self.label_5.setObjectName("label_5")
self.verticalLayout_5.addWidget(self.label_5)
self.label_7 = QtGui.QLabel(self.verticalLayoutWidget_4)
self.label_7.setAlignment(QtCore.Qt.AlignCenter)
self.label_7.setObjectName("label_7")
self.verticalLayout_5.addWidget(self.label_7)
self.label_9 = QtGui.QLabel(self.verticalLayoutWidget_4)
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.verticalLayout_5.addWidget(self.label_9)
self.label_11 = QtGui.QLabel(self.verticalLayoutWidget_4)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.verticalLayout_5.addWidget(self.label_11)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtGui.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.ExitButton, QtCore.SIGNAL("clicked()"), MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_4.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_10.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_6.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_8.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_9.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_11.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_7.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_3.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_13.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_14.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_15.setText(QtGui.QApplication.translate("MainWindow", "PushButrrrton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_16.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_17.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_19.setText(QtGui.QApplication.translate("MainWindow", "Neu", None, QtGui.QApplication.UnicodeUTF8))
self.ExitButton.setText(QtGui.QApplication.translate("MainWindow", "Ende", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Drücken Sie die Buttons in aufsteigender Richtung!", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "offen:", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("MainWindow", "korrekt:", None, QtGui.QApplication.UnicodeUTF8))
self.label_6.setText(QtGui.QApplication.translate("MainWindow", "falsch:", None, QtGui.QApplication.UnicodeUTF8))
self.label_8.setText(QtGui.QApplication.translate("MainWindow", "gesamt:", None, QtGui.QApplication.UnicodeUTF8))
self.label_10.setText(QtGui.QApplication.translate("MainWindow", "Spiele:", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("MainWindow", str(self.offen), None, QtGui.QApplication.UnicodeUTF8))
self.label_5.setText(QtGui.QApplication.translate("MainWindow", str(self.korrekt), None, QtGui.QApplication.UnicodeUTF8))
self.label_7.setText(QtGui.QApplication.translate("MainWindow", str(self.falsch), None, QtGui.QApplication.UnicodeUTF8))
self.label_9.setText(QtGui.QApplication.translate("MainWindow", str(self.gesamt), None, QtGui.QApplication.UnicodeUTF8))
self.label_11.setText(QtGui.QApplication.translate("MainWindow", str(self.spiele), None, QtGui.QApplication.UnicodeUTF8))
self.toolBar.setWindowTitle(QtGui.QApplication.translate("MainWindow", "toolBar", None, QtGui.QApplication.UnicodeUTF8))
self.listebutons = (self.pushButton,self.pushButton_2,self.pushButton_3,self.pushButton_4,
self.pushButton_6,self.pushButton_7,self.pushButton_8,
self.pushButton_9,self.pushButton_10,self.pushButton_11,
self.pushButton_13,self.pushButton_14,self.pushButton_16,
self.pushButton_17,self.pushButton_15)
self.pushButton.setText(str(self.model.list_of_random_items[0]))
self.pushButton_2.setText(str(self.model.list_of_random_items[1]))
self.pushButton_3.setText(str(self.model.list_of_random_items[2]))
self.pushButton_4.setText(str(self.model.list_of_random_items[3]))
self.pushButton_6.setText(str(self.model.list_of_random_items[4]))
self.pushButton_7.setText(str(self.model.list_of_random_items[5]))
self.pushButton_8.setText(str(self.model.list_of_random_items[6]))
self.pushButton_9.setText(str(self.model.list_of_random_items[7]))
self.pushButton_10.setText(str(self.model.list_of_random_items[8]))
self.pushButton_11.setText(str(self.model.list_of_random_items[9]))
self.pushButton_13.setText(str(self.model.list_of_random_items[10]))
self.pushButton_14.setText(str(self.model.list_of_random_items[11]))
self.pushButton_16.setText(str(self.model.list_of_random_items[12]))
self.pushButton_17.setText(str(self.model.list_of_random_items[13]))
self.pushButton_15.setText(str(self.model.list_of_random_items[14]))
self.pushButton.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton))
self.pushButton_2.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_2))
self.pushButton_3.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_3))
self.pushButton_4.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_4))
self.pushButton_6.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_6))
self.pushButton_7.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_7))
self.pushButton_8.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_8))
self.pushButton_9.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_9))
self.pushButton_10.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_10))
self.pushButton_11.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_11))
self.pushButton_13.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_13))
self.pushButton_14.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_14))
self.pushButton_15.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_15))
self.pushButton_16.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_16))
self.pushButton_17.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton_17))
self.pushButton_19.clicked.connect(lambda : Model.Model.newGame(self,self.listebutons))
我的Model.py
import random
import View
import Controller
from PySide import QtGui
class Model(object):
def __init__(self):
"""
Der Konstruktor der Klasse Model
"""
self.gesamt=0
self.wert=0
self.spiele=1
self.korrekt=0
self.falsch=0
self.zahlen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
self.offen=15
self.num_to_select = 15
self.list_of_random_items= random.sample(self.zahlen, self.num_to_select)
self.list_of_random_items.sort()
def getZahlen(self):
return self.zahlen
def getListe(self):
return self.list_of_random_item
def getWert(self):
return self.wert
def getSpiele(self):
return self.spiele
def getOffen(self):
return self.offen
def getKorrekt(self):
return self.korrekt
def getFalsch(self):
return self.falsch
def getGesamt(self):
return self.gesamt
def clickedButton(self,button):
a=int(button.text())
if a == self.list_of_random_items[0]:
self.korrekt += 1
self.list_of_random_items.remove(a)
else:
self.falsch +=1
self.list_of_random_items.remove(a)
button.setEnabled(False)
#button.setStyleSheet('background-color:red')
self.wert += 1
self.offen -= 1
print(self.list_of_random_items)
print(self.korrekt)
Model.changestatistik(self)
def newGame(self,buttonse):
self.spiele +=1
self.label_11.setText(QtGui.QApplication.translate("MainWindow", str(self.spiele), None, QtGui.QApplication.UnicodeUTF8))
for a in buttonse:
a.setEnabled(True)
# connect the buttons with the clicked signal
# self.connectButtons()
# start a new game
# self.start()
# self.ui.pushButton_19.setText('19')
#model.getZahlen()
程序有效但当我试图按下按钮时,我收到一个AttributeError:
Traceback (most recent call last):
File "C:\Users\Nervous\PycharmProjects\Game\View.py", line 261, in <lambda>
self.pushButton.clicked.connect(lambda : Model.Model.clickedButton(self,self.pushButton))
File "C:\Users\Nervous\PycharmProjects\Game\Model.py", line 45, in clickedButton
if a == self.list_of_random_items[0]:
AttributeError: 'Ui_MainWindow' object has no attribute 'list_of_random_items'
我试着在我的代码中调用Model = Model.list_of_random_items但仍然没有...我真的不知道错误来自哪里。任何想法?
答案 0 :(得分:0)
您尚未在Ui_MainWindow类中定义list_of_random_items。确保在Ui_MainWindow类的 init ()方法中声明它。