使用 wxpython 调用框架函数 python 解释器 returns 错误

using wxpython calling frame function python interpreter returns error

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(620, 665))

        splitter = wx.SplitterWindow(self, wx.ID_ANY)
        self.MainPanel = NewPanel(splitter)
        self.MyNotebook = Nbook(splitter)
        splitter.SplitHorizontally(self.MainPanel, self.MyNotebook, sashPosition=210)
        self.sb = self.CreateStatusBar(3)
        self.MainPanel.Show()
        self.Centre()

    def setNotebookDisabled(self):
        self.MyNotebook.Enabled = False

    def PrintSomething(self):
        print("Something")


class NewPanel(wx.Panel):
    def __init__(self, parent):
        super(NewPanel, self).__init__(parent=parent)

    def Disable_Controls(self):
        self.GrandParent.MyNotebook.Enabled = False

class Nbook(wx.Notebook):
    def __init__(self, parent):
        super(Nbook, self).__init__(parent)

        # self.MyNotebook = wx.Notebook(splitter,wx.ID_ANY)
        page_one = NbPanel1(self)
        page_two = NbPanel2(self)
        page_three = NbPanel3(self)
        self.AddPage(page_one, "Cable")
        self.AddPage(page_two, "Busduct")
        self.AddPage(page_three, "Transformer")

但是我可以成功调用 PrintSomething()...
错误:
文件 "c:/Users/Mark/Documents/FaultWireCalc/FCCalc.py",第 331 行,在 Disable_Controls 中 self.GrandParent.MyNotebook.Enabled = 假
AttributeError: 'MyFrame' 对象没有属性 'MyNotebook'

这与其说是一个答案,不如说是一个正在进行的工作,因为评论根本无法破解它。

鉴于您没有说明错误发生的时间或方式,并且它发生在 1000 行代码中的某处,我们看不到,现在可能是
的好时机 a minimal, complete and verifiable example (mcve)
https://whosebug.com/help/minimal-reproducible-example

由于您似乎不愿意提供,让我们试试这个:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(620, 665))

        splitter = wx.SplitterWindow(self, wx.ID_ANY)
        self.MainPanel = NewPanel(splitter)
        self.MyNotebook = Nbook(splitter)
        splitter.SplitHorizontally(self.MainPanel, self.MyNotebook, sashPosition=210)
        self.sb = self.CreateStatusBar(3)
        self.MainPanel.Show()
        self.Centre()
        self.Show()

    def setNotebookDisabled(self):
        self.MyNotebook.Enabled = False

    def PrintSomething(self):
        print("Something")


class NewPanel(wx.Panel):
    def __init__(self, parent):
        super(NewPanel, self).__init__(parent=parent)
        testbut = wx.Button(self, -1, "Panel Hit Me")
        self.Bind(wx.EVT_BUTTON, self.Disable_Controls)

    def Disable_Controls(self, event):
        self.GrandParent.MyNotebook.Enabled = not self.GrandParent.MyNotebook.IsEnabled()


class Nbook(wx.Notebook):
    def __init__(self, parent):
        super(Nbook, self).__init__(parent)

        # self.MyNotebook = wx.Notebook(splitter,wx.ID_ANY)
        page_one = wx.Panel(self)
        page_two = wx.Panel(self)
        page_three = wx.Panel(self)
        self.AddPage(page_one, "Cable")
        self.AddPage(page_two, "Busduct")
        self.AddPage(page_three, "Transformer")


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None, "Test")
    app.MainLoop()        

如你所见,它的功能(下图)笔记本可以enabled/disabled。

那么,您能否使其产生错误或从中推断出您的代码在产生错误的方式或调用代码时有何不同?

您可能会发现 GrandParent 并不是您想的那样! (我怀疑涉及splitter
你可以试试:

print("I am",self)
print("My parent is",self.Parent)
print("Granddad is",self.GrandParent)
print("His parent is",self.GrandParent.GetParent())

调用例程时您可能会尝试的另一件事是直接调用它:

MyFrame.setNotebookDisabled(parent.GetParent())

或获得正确父级的变体,具体取决于您从何处调用它。

交给你了。