1

它是一个使用 PyQt5 和 Python3.3 的带有 GUI 的简单堆栈显示程序。之间突出显示的 Gui 程序错误区域###Error####

#Modules
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
import stacks_queues

#Classes
class MainWindow(QtWidgets.QMainWindow):
    '''Main Window'''

    #Constructor
    def __init__(self):
        super(MainWindow,self).__init__()
        self.createUI()   

    #Methods

###Error###
    def createUI(self):
        self.stack = stacks_queues.Stack()
        #Title
        self.setWindowTitle('Section 2.4: Stacks and queues')
        #Window Components
        self.mainViewWidget = QtWidgets.QWidget()
        #Layouts
        self.mainViewWidget.setLayout(self.mainGrid(self.stack))
        self.setCentralWidget(self.mainViewWidget)
###Error###

    def mainGrid(self,stack):
        #Layout components
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.addRemoveLayout = QtWidgets.QGridLayout()
        self.pushButtonLayout = QtWidgets.QHBoxLayout()
        #Create components
        #Labels
        self.titleLabel = QtWidgets.QLabel('Implementation of a Stack', self)
        self.counterLabel = QtWidgets.QLabel('Number of items in stack: 0', self)
        self.contentsLabel = QtWidgets.QLabel('Contents of stack', self)
        self.addWordLabel = QtWidgets.QLabel('Word to add:', self)
        self.removeWordLabel = QtWidgets.QLabel('Word being removed', self)
        #Buttons
        self.pushButton = QtWidgets.QPushButton('Push', self)
        self.popButton = QtWidgets.QPushButton('Pop', self)
        #Line Edit
        self.addWordLineEdit = QtWidgets.QLineEdit('', self)
        self.removeWordLineEdit = QtWidgets.QLineEdit('Test', self)
        self.removeWordLineEdit.isReadOnly()
        #Text Edit
        self.stackTextEdit = QtWidgets.QTextEdit('', self)
        self.stackTextEdit.isReadOnly()
        #Add Widgets
        self.mainLayout.addWidget(self.titleLabel)
        self.addRemoveLayout.addWidget(self.addWordLabel, 0,0)
        self.addRemoveLayout.addWidget(self.removeWordLabel, 1,0)
        self.addRemoveLayout.addWidget(self.addWordLineEdit, 0,1)
        self.addRemoveLayout.addWidget(self.removeWordLineEdit, 1,1)
        self.mainLayout.addLayout(self.addRemoveLayout)
        self.mainLayout.addWidget(self.contentsLabel)
        self.mainLayout.addWidget(self.stackTextEdit)
        self.mainLayout.addWidget(self.counterLabel)
        self.pushButtonLayout.addWidget(self.pushButton)
        self.pushButtonLayout.addWidget(self.popButton)
        self.mainLayout.addLayout(self.pushButtonLayout)
        #Connections
        self.pushButton.clicked.connect(self.pushStack(self.stack))
        self.popButton.clicked.connect(self.popStack)
        return self.mainLayout

###Error###
    def pushStack(self,stack):
        item = self.addWordLineEdit.text()
        self.stack.push(item)
        #Re-draw Window
        self.mainViewWidget = QtWidgets.QWidget()
        self.mainViewWidget.setLayout(self.mainGrid(self.stack))
        self.setCentralWidget(self.mainViewWidget)
###Error###

    def popStack(self):
        #Not finished yet
        pass


#Functions
def Main():
    '''Main Program'''
    #Creates new application
    application = QtWidgets.QApplication(sys.argv)
    #Creates new instance of MainWindow
    mainWindow = MainWindow()
    #Display MainWindow
    mainWindow.show()
    #Keep MainWindow on top
    mainWindow.raise_()
    #Monitor for events
    application.exec_()
#Run Main
Main()

导入程序

class Stack():
    '''Stack data'''
    #Attributes
    items = []
    maxsize = 0

    #Methods
    def push(self,item):
        self.items.append(item)

    def pop(self,item):
        self.items.pop()

    def isEmpty(self):
        if self.items:
            return False
        else:
            return True

    def size(self):
        self.maxsize = len(self.items)

错误是 1993 行长,所以我只会显示不断重复的主要错误。

Traceback (most recent call last):
  File "C:\Users\Luke\Documents\A-Level-Work\A2\Computing\COMP 3\stack_queues_gui.py", line 92, in <module>
    Main()
  File "C:\Users\Luke\Documents\A-Level-Work\A2\Computing\COMP 3\stack_queues_gui.py",     line 84, in Main
    mainWindow = MainWindow()
  File "C:\Users\Luke\Documents\A-Level-Work\A2\Computing\COMP 3\stack_queues_gui.py",     line 13, in __init__
    self.createUI()
  File "C:\Users\Luke\Documents\A-Level-Work\A2\Computing\COMP 3\stack_queues_gui.py", line 23, in createUI
    self.mainViewWidget.setLayout(self.mainGrid(self.stack))
  File "C:\Users\Luke\Documents\A-Level-Work\A2\Computing\COMP 3\stack_queues_gui.py",     line 62, in mainGrid
    self.pushButton.clicked.connect(self.pushStack(self.stack))
  File "C:\Users\Luke\Documents\A-Level-Work\A2\Computing\COMP 3\stack_queues_gui.py",         line 71, in pushStack

错误一直在第 62 和 71 行重复,直到 python 超过其最大递归深度。

巨大的错误只在我添加后才发生,pushStack所以我不知道如何解决这个问题,因为我只有 A2 级的 Python 知识。该程序可以很好Stack地被调用MainWindow并在mainGrid.

4

1 回答 1

2

您创建了一个无限递归:

 def mainGrid(self,stack):
     # ...
    self.pushButton.clicked.connect(self.pushStack(self.stack))

来电

def pushStack(self,stack):
    # ...
    self.mainViewWidget.setLayout(self.mainGrid(self.stack))

看到self.mainGrid()那里了吗?但是self.mainGrid()调用self.pushStack(),并且在您的代码中没有任何地方可以跳出该循环。

您需要在此处删除一个或另一个调用,或者确定递归调用应该实现什么并创建两个方法之一调用另一个的条件。

很可能您不必在self.mainViewWidget每次将某些内容压入堆栈时都从头开始重新创建。此外,如果您打算在单击时被self.pushStack调用(而不是在将操作连接到按钮时),则不需要直接调用它:pushButton

self.pushButton.clicked.connect(self.pushStack)

请注意,现在self.pushStack 方法已连接,而不是返回值。

没有理由传到self.stack这里self.pushStack();该方法也可以self.stack在没有stack参数的情况下访问

def pushStack(self):
    # ...
    self.stack.push(item)
于 2013-11-07T16:50:30.850 回答