Wxpython如何获取组合框&x27;Panel2中Panel1的s值

Wxpython如何获取组合框&x27;Panel2中Panel1的s值,python,wxpython,Python,Wxpython,现在我有两个面板一和面板二,并在一个框架中使用笔记本 当我点击按钮时,我想将Panelone的值返回到PanelTwo 像 首先,如果您正在学习,我建议您使用WxGlade来构建图形界面。您的代码纯粹是意大利面条,并且充满了语法错误:( 在您的示例中,这是非常简单的,因为所有元素都属于同一个文件并且在同一个类中 例如: #!/usr/bin/env python # -*- coding: UTF-8 -*- import wx # THE MAIN FRAME: class Mai

现在我有两个面板一和面板二,并在一个框架中使用笔记本 当我点击按钮时,我想将Panelone的值返回到PanelTwo 像


首先,如果您正在学习,我建议您使用WxGlade来构建图形界面。您的代码纯粹是意大利面条,并且充满了语法错误:(

在您的示例中,这是非常简单的,因为所有元素都属于同一个文件并且在同一个类中

例如:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-    

import wx

# THE MAIN FRAME:
class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        self.SetSize((734, 501))
        # The main notebook:
        self.main_notebook = wx.Notebook(self, wx.ID_ANY)
        # Notebook's panels:
        self.panel1 = wx.Panel(self.main_notebook, wx.ID_ANY)
        self.panel2 = wx.Panel(self.main_notebook, wx.ID_ANY)
        # Content of panel1:
        self.choiceFruits = wx.Choice(self.panel1, wx.ID_ANY, choices=[])
        self.btn_send = wx.Button(self.panel1, wx.ID_ANY, "Send Value to Panel2")
        #Content of panel2:
        self.txt_result = wx.TextCtrl(self.panel2, wx.ID_ANY, "")

        #Binding events:
        # event, custom event handler, gui element
        self.Bind(wx.EVT_BUTTON, self.OnBtnSendClick, self.btn_send)

        self.__set_properties()
        self.__do_layout()

    def __set_properties(self):

        self.SetTitle("frame")

        choices = ['Apple', 'Banana', 'Peach', 'Strawberry']
        self.choiceFruits.SetItems(choices)
        self.choiceFruits.SetSelection(0)
        

    def __do_layout(self):
        # begin wxGlade: MainFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_2 = wx.FlexGridSizer(2, 1, 0, 0)
        grid_sizer_1 = wx.FlexGridSizer(2, 2, 0, 10)
        label_1 = wx.StaticText(self.panel1, wx.ID_ANY, "Choose a fruit:")
        grid_sizer_1.Add(label_1, 0, wx.ALL, 10)
        grid_sizer_1.Add((0, 0), 0, 0, 0)
        grid_sizer_1.Add(self.choiceFruits, 0, wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
        grid_sizer_1.Add(self.btn_send, 0, wx.BOTTOM | wx.EXPAND | wx.RIGHT, 10)
        self.panel1.SetSizer(grid_sizer_1)
        grid_sizer_1.AddGrowableCol(0)
        label_2 = wx.StaticText(self.panel2, wx.ID_ANY, "You have selected:")
        grid_sizer_2.Add(label_2, 0, wx.ALL, 10)
        grid_sizer_2.Add(self.txt_result, 0, wx.ALL | wx.EXPAND, 10)
        self.panel2.SetSizer(grid_sizer_2)
        grid_sizer_2.AddGrowableCol(0)
        self.main_notebook.AddPage(self.panel1, "Panel 1")
        self.main_notebook.AddPage(self.panel2, "Panel 2")
        sizer_1.Add(self.main_notebook, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade

    # Custom event handler:
    def OnBtnSendClick(self, event):
        selectedFruit = self.choiceFruits.GetString(self.choiceFruits.GetSelection())
        self.txt_result.SetValue(selectedFruit)
        wx.MessageBox("You have selected \"%s\"" % selectedFruit)


# The Main Class:
class MyApp(wx.App):
    def OnInit(self):
        self.main_frame = MainFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.main_frame)
        self.main_frame.Show()
        return True


# Main APP Method.
if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()
但这并不常见。通常每个面板将位于其自身类中的单独文件中。在这种情况下,您必须将一个引用从主框架传递到每个面板,然后我们使用此引用访问主框架上的元素(例如,另一个面板)

大型机

#!/usr/bin/env python
# -*- coding: UTF-8 -*-    

import wx
from panel1 import Panel1
from panel2 import Panel2

# THE MAIN FRAME:
class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        self.SetSize((734, 501))
        # The main notebook:
        self.main_notebook = wx.Notebook(self, wx.ID_ANY)
        # Notebook's panels:
        self.panel1 = Panel1(self.main_notebook, wx.ID_ANY)
        self.panel2 = Panel2(self.main_notebook, wx.ID_ANY)

        # Pass reference of the main frame to each panel:
        self.panel1.SetParent(self)
        self.panel2.SetParent(self)


        self.__set_properties()
        self.__do_layout()

    def __set_properties(self):
        self.SetTitle("frame")
        

    def __do_layout(self):        
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_2 = wx.FlexGridSizer(2, 1, 0, 0)        
        self.main_notebook.AddPage(self.panel1, "Panel 1")
        self.main_notebook.AddPage(self.panel2, "Panel 2")
        sizer_1.Add(self.main_notebook, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()        

    
# The Main Class:
class MyApp(wx.App):
    def OnInit(self):
        self.main_frame = MainFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.main_frame)
        self.main_frame.Show()
        return True


# Main APP Method.
if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()
面板1

# -*- coding: UTF-8 -*-

import wx

class Panel1(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Panel1.__init__
        kwds["style"] = kwds.get("style", 0) | wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        # Content of panel1:
        self.choiceFruits = wx.Choice(self, wx.ID_ANY, choices=[])
        self.btn_send = wx.Button(self, wx.ID_ANY, "Send Value to Panel2")

        self._parent = None

        #Binding events:
        # event, custom event handler, gui element
        self.Bind(wx.EVT_BUTTON, self.OnBtnSendClick, self.btn_send)

        self.__set_properties()
        self.__do_layout()


    def __set_properties(self):        
        choices = ['Apple', 'Banana', 'Peach', 'Strawberry']
        self.choiceFruits.SetItems(choices)
        self.choiceFruits.SetSelection(0)        

    def __do_layout(self):        
        grid_sizer_2 = wx.FlexGridSizer(2, 2, 0, 0)
        label_2 = wx.StaticText(self, wx.ID_ANY, "Choose a fruit:")
        grid_sizer_2.Add(label_2, 0, wx.LEFT | wx.RIGHT | wx.TOP, 10)
        grid_sizer_2.Add((0, 0), 0, 0, 0)
        grid_sizer_2.Add(self.choiceFruits, 0, wx.ALL | wx.EXPAND, 10)
        grid_sizer_2.Add(self.btn_send, 0, wx.BOTTOM | wx.RIGHT | wx.TOP, 10)
        self.SetSizer(grid_sizer_2)
        grid_sizer_2.Fit(self)
        grid_sizer_2.AddGrowableCol(0)
        self.Layout()
        # end wxGlade

    def SetParent(self, parent):
        self._parent = parent

    # Custom event handler:
    def OnBtnSendClick(self, event):
        selectedFruit = self.choiceFruits.GetString(self.choiceFruits.GetSelection())

        # here is the trick !!!
        self._parent.panel2.txt_result.SetValue(selectedFruit)
        wx.MessageBox("You have selected \"%s\"" % selectedFruit)
面板2

# -*- coding: UTF-8 -*-

import wx

class Panel2(wx.Panel):
    def __init__(self, *args, **kwds):        
        kwds["style"] = kwds.get("style", 0) | wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)

        #Content of panel2:
        self.txt_result = wx.TextCtrl(self, wx.ID_ANY, "")
        
        self.__set_properties()
        self.__do_layout()        

    def __set_properties(self):       
        pass
       

    def __do_layout(self):        
        grid_sizer_2 = wx.FlexGridSizer(2, 1, 0, 0)
        label_2 = wx.StaticText(self, wx.ID_ANY, "You have selected:")
        grid_sizer_2.Add(label_2, 0, wx.LEFT | wx.RIGHT | wx.TOP, 10)
        grid_sizer_2.Add(self.txt_result, 0, wx.ALL | wx.EXPAND, 10)
        self.SetSizer(grid_sizer_2)
        grid_sizer_2.Fit(self)
        grid_sizer_2.AddGrowableCol(0)
        self.Layout()

def SetParent(self, parent):
    self._parent = parent
技巧在Panel1的(btn_发送)按钮事件处理程序中:

# self._parent is a reference to MainFrame
# panel2 is a main_frame's element.
# txt_result is a TextCtrl in Panel2 class.
self._parent.panel2.txt_result.SetValue(selectedFruit)

您尝试了什么?您是否尝试了
PanelTwo.choice1.GetSelection()
例如?您好,您的建议我尝试getvalue get selection all将获得错误PanelTwo没有属性选项1如果我回答了您的问题…请将其标记为已接受的答案好吗?
# self._parent is a reference to MainFrame
# panel2 is a main_frame's element.
# txt_result is a TextCtrl in Panel2 class.
self._parent.panel2.txt_result.SetValue(selectedFruit)