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

问题描述 投票:0回答:1
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”,Disable_Controls中的第331行self.GrandParent.MyNotebook.Enabled = FalseAttributeError:“ MyFrame”对象没有属性“ MyNotebook”

python-3.x wxpython
1个回答
0
投票

考虑到评论根本不会破解它,这与其说是正在进行的工作,不如说是一个答案。

鉴于您尚未说明错误的发生时间或方式以及错误发生在1000行代码中的某个位置(我们现在看不到),现在可能是个好时机a minimal, complete and verifiable example (mcve)https://stackoverflow.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()        

如您所见,它确实可以(在下面)启用/禁用笔记本计算机。

enter image description here

因此,您能否使它产生错误或从中推论出您的代码在方式或调用代码时有何不同,从而产生错误?

您可能会发现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())

或获取正确的父代的变体,具体取决于您从哪里调用它。

交给您。

© www.soinside.com 2019 - 2024. All rights reserved.