wxpythonc++;正在删除部分TextCtrl
我试图用Python为一个学校项目组装一个gui,但我遇到了一个错误,我真的不明白为什么 self.prompt.addAnswer(i,self.ansControls[i].GetValue())文件 “C:\Python27\lib\site packages\wx-3.0-msw\wx_core.py”,第16712行,在 getattr 提升PyDeadObjectError(Self.AtExtR%Surviv.NoX)Wx.yCy.PyDeasObjeCurrist: TeXTCRL对象的C++部分 已删除,不再允许属性访问 我理解错误的含义,TextCtrl对象不再存在,因此我无法访问它。我不明白为什么TextCtrl对象不再存在。流程如下: 框架将显示标签、文本框和按钮。用户输入数据并点击下一步。一切进展顺利。然后创建同一PromptFrame类的不同实例,同样的事情也会发生。然而,这一次,当用户点击next时,我得到了前面提到的错误。代码如下: 后台运行显示的服务:wxpythonc++;正在删除部分TextCtrl,python,python-2.7,wxpython,Python,Python 2.7,Wxpython,我试图用Python为一个学校项目组装一个gui,但我遇到了一个错误,我真的不明白为什么 self.prompt.addAnswer(i,self.ansControls[i].GetValue())文件 “C:\Python27\lib\site packages\wx-3.0-msw\wx_core.py”,第16712行,在 getattr 提升PyDeadObjectError(Self.AtExtR%Surviv.NoX)Wx.yCy.PyDeasObjeCurrist: TeXTCR
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.
谢谢你们的帮助,伙计们
编辑:这是我的init.py文件:
from MainFrame import MainFrame
import wx
app = wx.App(False)
frame = MainFrame(None, "My App")
app.MainLoop()
ansControls
当前定义为类变量。这意味着在任何窗口中定义的任何控件都将添加到其中
在第一个实例中创建一个控件,它被添加到类中,但窗口属于该实例。因此,当您销毁类时,实例将被销毁,但指向它的python对象仍然存在
然后打开第二个窗口,添加更多控件,然后点击循环,在其中循环。循环中的第一个将不再有一个有效的C++对象在它下面,并且将失败。
不知道为什么它们被定义为类变量,但是您也需要保留一个指向根窗口的指针,在父窗口被删除时删除类控件,或者(更简单)在程序中的某个点(在调用任何类之前)将
ansControls
作为实例变量而不是类变量…是否创建wx.App
实例?是的,在init.py文件中创建。它只有4行代码,所以我没有包括它。@wnnmaw,我撒谎,它只有三行代码,有两个导入。我把它加到我的问题上了嗨,科利,谢谢你的回答!我将ansControls创建为类变量,以便可以在类的所有方法中访问它,就像全局变量一样。我不知道类变量和实例变量之间的区别是什么,但实例变量听起来像我正在寻找的,所以我会做一些研究。这就是诀窍,在这个过程中我学到了一些新的东西。谢谢你的帮助!
from MainFrame import MainFrame
import wx
app = wx.App(False)
frame = MainFrame(None, "My App")
app.MainLoop()