TextCtrl的WXPython C++部分被删除

2024-06-02 07:25:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python为一个学校项目组装一个gui,但是我遇到了一个错误,我不明白为什么。在

self.prompt.addAnswer(i, self.ansControls[i].GetValue()) File "C:\Python27\lib\site-packages\wx-3.0-msw\wx_core.py", line 16712, in getattr raise PyDeadObjectError(self.attrStr % self._name) wx._core.PyDeadObjectError: The C++ part of the TextCtrl object has been deleted, attribute access no longer allowed.

我理解错误的含义,textcrl对象不再存在,所以我无法访问它。我不明白为什么textcrl对象不再存在。流程如下:

框架将显示标签、文本框和按钮。用户输入数据并点击下一个。一切顺利。同样的PromptFrame实例也会被创建出来。但是这次,当用户点击next时,我得到了前面提到的错误。代码如下:

后台运行节目的服务:

class AppService(object):

    prompts = [Prompt_1, Prompt_2, Prompt_3, Prompt_4, Prompt_5, Prompt_6, Prompt_7,
            Prompt_8, Prompt_9, Prompt_10, Prompt_11, Prompt_12, Prompt_13, Prompt_14,
             Prompt_15, Prompt_16, Prompt_17, Prompt_18, Prompt_19]

    skippedPromptIndices = []

    def __init__(self):
        print "Service Started"
        PromptFrame(self, self.prompts[0], 0, len(self.prompts))

    def doBack(self, curIndex, curPrompt):
        if curIndex >= 0:
            self.prompts[curIndex] = curPrompt
            PromptFrame(self, self.prompts[curIndex - 1], curIndex - 1, len(self.prompts))
        else:
            posCurIndex = (curIndex * -1) - 1
            self.prompts[posCurIndex] = curPrompt

            backIndex = self.skippedPromptIndices.index(curIndex) - 1
            nextPromptIndex = 0
            if backIndex < 0:
                nextPromptIndex = len(self.prompts) - 1
            else:
                nextPromptIndex = self.skippedPromptIndices[backIndex]

            PromptFrame(self, self.prompts[(nextPromptIndex * -1) - 1], nextPromptIndex, len(self.prompts))

    def doSkip(self, curIndex, curPrompt):
        skipIndex = (curIndex + 1) * -1
        if self.skippedPromptIndices.count(skipIndex) > 0:
            self.skippedPromptIndices.remove(skipIndex)

        self.skippedPromptIndices.append(skipIndex)
        self.doNext(curIndex, curPrompt)

    def doNext(self, curIndex, curPrompt):
        if curIndex >= 0:
            self.prompts[curIndex] = curPrompt
        else:
            self.prompts[(curIndex * -1) - 1] = curPrompt


        if (curIndex >= 0 and curIndex < (len(self.prompts) - 1)):
            PromptFrame(self, self.prompts[curIndex + 1], curIndex + 1, len(self.prompts))
        elif len(self.skippedPromptIndices) > 0:
            skipIndex = self.skippedPromptIndices.pop(0)
            nextIndex = (skipIndex * -1) - 1
            PromptFrame(self, self.prompts[nextIndex], skipIndex, len(self.prompts))
        else:
            dlg = wx.MessageDialog(self, "Done!", "Message", wx.OK)
            dlg.ShowModal() # Shows it
            dlg.Destroy() # finally destroy it when finished.

这是PromptFrame类:

^{pr2}$

谢谢你们的帮助!在

编辑:这是我的init.py文件:

from MainFrame import MainFrame
import wx

app = wx.App(False)
frame = MainFrame(None, "My App")
app.MainLoop() 

Tags: selflenifdef错误promptelsewx
1条回答
网友
1楼 · 发布于 2024-06-02 07:25:30

ansControls当前定义为类变量。这意味着在任何窗口中定义的任何控件都将被添加到其中。在

在第一个实例中创建一个控件,它被添加到类中,但窗口属于该实例。因此,当销毁类时,实例将被销毁,但指向它的python对象仍然存在。在

然后打开第二个窗口,添加更多控件,然后在循环的地方单击循环。在循环中的第一个将不再有一个有效的C++对象在它下面,并且将失败。在

不知道为什么它们被定义为类变量,但是你要么需要保留一个指向根窗口的指针,要么在父窗口被删除时删除类控件,要么(更简单地)把ansControls变成一个实例变量而不是一个类变量。。。在

相关问题 更多 >