wxpython - 从另一个框架返回输入变量

发布于 2024-12-22 07:26:11 字数 1751 浏览 1 评论 0原文

如何在一帧中的一个面板中引用另一帧中另一个面板中的输入变量?

我基本上想在 wxpython gui 的菜单栏中创建一个“选项”选项卡,单击该选项卡时,会打开一个新框架,允许用户更改一些变量。但是,当我稍后尝试引用这些变量时,我得到 AttributeError:类型对象“OptionsPanel”没有属性“Input1”

我将两个面板和两个框架都定义为类。 这是我的完整代码:

import wx
class MainFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,300))
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        m_options = menu.Append(wx.ID_EDIT, "&Options", "Options")
        self.Bind(wx.EVT_MENU, self.OnOptions, m_options)
        menuBar.Append(menu, "&Options")
        self.SetMenuBar(menuBar)
        panel=MainPanel(self)
    def OnOptions(self, event):
        frame = OptionsFrame("Options Frame")
        frame.Show()
class OptionsFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,200))    
        panel=OptionsPanel(self)
class OptionsPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.label = wx.StaticText(self, label="Input Value", pos=(40,60))
        self.Input1 = wx.TextCtrl(self, value="1.0", pos=(80,80), size=(60,-1))
class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.button =wx.Button(self, label="GO", pos=(60,100))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
    def OnClick(self,event):
        MyVariable= OptionsPanel.Input1.GetValue() #This won't work!
        print dt0
if __name__=="__main__":
    app = wx.App(redirect=False)
    frame = MainFrame("Multiple Frames Attempt")
    frame.Show()
    app.MainLoop()

提前致谢!

How can I, in one panel that is in one frame, reference an input variable that is in another panel in another frame?

I basically want to create an 'Options' tab in the menubar of my wxpython gui, that when clicked on, opens a new frame that allows the user to change some variables. However, when I try to reference those variables later, I get
AttributeError: type object 'OptionsPanel' has no attribute 'Input1'

I have both panels and both frames defined as classes.
Here is my complete code:

import wx
class MainFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,300))
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        m_options = menu.Append(wx.ID_EDIT, "&Options", "Options")
        self.Bind(wx.EVT_MENU, self.OnOptions, m_options)
        menuBar.Append(menu, "&Options")
        self.SetMenuBar(menuBar)
        panel=MainPanel(self)
    def OnOptions(self, event):
        frame = OptionsFrame("Options Frame")
        frame.Show()
class OptionsFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,200))    
        panel=OptionsPanel(self)
class OptionsPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.label = wx.StaticText(self, label="Input Value", pos=(40,60))
        self.Input1 = wx.TextCtrl(self, value="1.0", pos=(80,80), size=(60,-1))
class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.button =wx.Button(self, label="GO", pos=(60,100))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
    def OnClick(self,event):
        MyVariable= OptionsPanel.Input1.GetValue() #This won't work!
        print dt0
if __name__=="__main__":
    app = wx.App(redirect=False)
    frame = MainFrame("Multiple Frames Attempt")
    frame.Show()
    app.MainLoop()

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2024-12-29 07:26:11

一个问题是类 MainPanel 中的 OnClick 方法引用了类 OptionsPanel,而不是引用类 OptionsFrame 中的类 OptionsPanel(“panel”)的实例化。

import wx
class MainFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,300))
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        m_options = menu.Append(wx.ID_EDIT, "&Options", "Options")
        self.Bind(wx.EVT_MENU, self.OnOptions, m_options)
        menuBar.Append(menu, "&Options")
        self.SetMenuBar(menuBar)
        panel=MainPanel(self)
        self.options_frame = None
    def OnOptions(self, event):
        self.options_frame = OptionsFrame("Options Frame")
        self.options_frame.Show()
    def GetInput1Value(self):
        if self.options_frame is not None:
            return(self.options_frame.options_panel.Input1.GetValue())
        else:
            return('None')
class OptionsFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,200))    
        self.options_panel=OptionsPanel(self)
class OptionsPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.label = wx.StaticText(self, label="Input Value", pos=(40,60))
        self.Input1 = wx.TextCtrl(self, value="1.0", pos=(80,80), size=(60,-1))
class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.parent_frame = parent
        self.button =wx.Button(self, label="GO", pos=(60,100))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
    def OnClick(self,event):
        MyVariable= self.parent_frame.GetInput1Value()
        print MyVariable
if __name__=="__main__":
    app = wx.App(redirect=False)
    frame = MainFrame("Multiple Frames Attempt")
    frame.Show()
    app.MainLoop()

One problem is that the OnClick method in class MainPanel is referencing the class OptionsPanel instead of referencing the instantiation of the class OptionsPanel ('panel') in class OptionsFrame.

import wx
class MainFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,300))
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        m_options = menu.Append(wx.ID_EDIT, "&Options", "Options")
        self.Bind(wx.EVT_MENU, self.OnOptions, m_options)
        menuBar.Append(menu, "&Options")
        self.SetMenuBar(menuBar)
        panel=MainPanel(self)
        self.options_frame = None
    def OnOptions(self, event):
        self.options_frame = OptionsFrame("Options Frame")
        self.options_frame.Show()
    def GetInput1Value(self):
        if self.options_frame is not None:
            return(self.options_frame.options_panel.Input1.GetValue())
        else:
            return('None')
class OptionsFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,200))    
        self.options_panel=OptionsPanel(self)
class OptionsPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.label = wx.StaticText(self, label="Input Value", pos=(40,60))
        self.Input1 = wx.TextCtrl(self, value="1.0", pos=(80,80), size=(60,-1))
class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.parent_frame = parent
        self.button =wx.Button(self, label="GO", pos=(60,100))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
    def OnClick(self,event):
        MyVariable= self.parent_frame.GetInput1Value()
        print MyVariable
if __name__=="__main__":
    app = wx.App(redirect=False)
    frame = MainFrame("Multiple Frames Attempt")
    frame.Show()
    app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文