微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wxpython如何从Panel2获取Panel1的ComboBox值

如何解决Wxpython如何从Panel2获取Panel1的ComboBox值

现在我有两个Panel Panelone和Paneltwo,并且在Frame中使用笔记本 当我单击按钮时,我想将Panelone的值返回给PanelTwo 喜欢

class PanelTwo(wx.panel):
   def __init__(self,parent):
   super(PanelTwo,self).__init__(parent)
   self.choice1 = wx.ComboBox(self,value=**the Panelone Value**,choices=,style=wx.CB_SORT,pos=(100,5)) or 
   self.choice1.SetValue(the Panelone Value)
class Panelone(wx.panel):
    def __init__(self,parent):
    choicelist = ['1','2','3','5','6']
    super(Panelone,self).__init__(parent)
    self.choice = wx.ComboBox(self,value="1",choices=choicelist,5))
    self.btn = wx.Button(self,label="Summit",pos=(250,10),size=(80,50))
    self.Bind(wx.EVT_BUTTON,self.BtnCheck,self.btn)
    def BtnCheck(self,event):
        **When I click the button,I want to return the value of Panelone to PanelTwo**
class Game(wx.Frame):
    def __init__(self,parent,title):
        super(Game,self).__init__(parent,title=title,size=(900,700))
        self.InitUI()

    def InitUI(self):
        nb = wx.Notebook(self)
        nb.AddPage(PanelOne(nb),"PanelOne")
        nb.AddPage(PanelTwo(nb),"PanelTwo")
        self.Centre()
        self.Show(True)

解决方法

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

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

例如:

    = Sku Internals


type alias Internals =
    { id : String --                    READONLY,name : String --                  READ/WRITE,imgExtras : ( List ImgExtra,String ) --      READ/WRITE,availableSizes : List String --   READ/WRITE
  }

但这并不常见。通常,每个面板都将在其自己的类中的单独文件中。在这种情况下,您必须将引用从主机框架传递到每个面板,然后我们使用该引用来访问主机架上的元素(例如,另一个面板)。

MAINFRAME

#!/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,**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,"Send Value to Panel2")
        #Content of panel2:
        self.txt_result = wx.TextCtrl(self.panel2,"")

        #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)
        grid_sizer_1 = wx.FlexGridSizer(2,2,10)
        label_1 = wx.StaticText(self.panel1,"Choose a fruit:")
        grid_sizer_1.Add(label_1,wx.ALL,10)
        grid_sizer_1.Add((0,0),0)
        grid_sizer_1.Add(self.choiceFruits,wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.RIGHT,10)
        grid_sizer_1.Add(self.btn_send,wx.BOTTOM | wx.EXPAND | wx.RIGHT,10)
        self.panel1.SetSizer(grid_sizer_1)
        grid_sizer_1.AddGrowableCol(0)
        label_2 = wx.StaticText(self.panel2,"You have selected:")
        grid_sizer_2.Add(label_2,10)
        grid_sizer_2.Add(self.txt_result,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,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,"")
        self.SetTopWindow(self.main_frame)
        self.main_frame.Show()
        return True


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

面板1

#!/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,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,0)        
        self.main_notebook.AddPage(self.panel1,0)
        self.SetSizer(sizer_1)
        self.Layout()        

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


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

面板2

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

import wx

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

        self._parent = None

        #Binding events:
        # event,self.btn_send)

        self.__set_properties()
        self.__do_layout()


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

    def __do_layout(self):        
        grid_sizer_2 = wx.FlexGridSizer(2,0)
        label_2 = wx.StaticText(self,"Choose a fruit:")
        grid_sizer_2.Add(label_2,wx.LEFT | wx.RIGHT | wx.TOP,10)
        grid_sizer_2.Add((0,0)
        grid_sizer_2.Add(self.choiceFruits,10)
        grid_sizer_2.Add(self.btn_send,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)

诀窍在于Panel1的(btn_send)按钮事件处理程序中:

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

import wx

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

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

    def __set_properties(self):       
        pass
       

    def __do_layout(self):        
        grid_sizer_2 = wx.FlexGridSizer(2,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。