【问题标题】:Wxpython ListctrlWxpython Listctrl
【发布时间】:2013-01-30 22:20:25
【问题描述】:

请运行下面的代码,这里我在一帧中有一个按钮 addscript , onbutton 点击​​打开另一个框架,该框架由显示的数据组合框组成。选择适当的选项并单击更新和关闭按钮后,组合框中的值应打印在其他框架上的 listctrl 上

谁能帮我解决这个问题

提前致谢

import wx

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial",size=(700, 400))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.index = 0

        self.list_ctrl = wx.ListCtrl(panel, size=(-1,200),
                         style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN
                         )
        self.list_ctrl.InsertColumn(0, 'Machine Name')
        self.list_ctrl.InsertColumn(1, 'Sim Port')
        self.list_ctrl.InsertColumn(2, 'Sim Script', width=125)
        self.list_ctrl.InsertColumn(3, 'Status', width=150)
        self.list_ctrl.InsertColumn(4, 'Status Detail', width=300)

        btn = wx.Button(panel, label="Add Line")
        btn.Bind(wx.EVT_BUTTON, self.add_line)

        btn2 = wx.Button(panel,label = "Add Script")
        btn2.Bind(wx.EVT_BUTTON, self.add_script)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(btn2,0,wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

        menu = wx.Menu()
        #exit = menu.Append(-1, "Exit")
        #self.Bind(wx.EVT_MENU, self.OnExit, exit)
        menuBar = wx.MenuBar()
        self.SetMenuBar(menuBar)
        #wx.StaticText(self.m_panel1, -1,(25,25))
        self.popupmenu = wx.Menu()
        for text in "Start Stop Remove".split():
            item = self.popupmenu.Append(-1, text)
            self.Bind(wx.EVT_MENU, self.OnPopupItemSelected, item)
            self.list_ctrl.Bind(wx.EVT_CONTEXT_MENU, self.ShowPopUp)

        #self.list_ctrl.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.ShowPopUp)
    #----------------------------------------------------------------------

    def add_script(self, event):
        # mp is item 2
        self.new = NewWindow(parent=None, id=-1)
        self.new.Show()

    def add_line(self, event):
        #line = "Line %s" % self.index
        line = 'LDR2'
        self.list_ctrl.InsertStringItem(self.index,line)
        self.list_ctrl.SetStringItem(self.index, 1, "22000")
        self.list_ctrl.SetStringItem(self.index, 2, "LF1-trk")
        self.list_ctrl.SetStringItem(self.index, 3, "running")
        self.list_ctrl.SetStringItem(self.index, 4, "last Status info received")
        self.index += 1

    def OnPopupItemSelected(self, event):
        item = self.popupmenu.FindItemById(event.GetId())
        text = item.GetText()
        wx.MessageBox("You selected item '%s'" % text)

    def ShowPopUp( self, event ):
        #pos = self.list_ctrl.GetPosition()
        pos = event.GetPosition()
        pos = self.list_ctrl.ScreenToClient(pos)
        self.list_ctrl.PopupMenu(self.popupmenu,pos)
        event.Skip()

class NewWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
        #self.frame1 = MainWindow;

        wx.Frame.CenterOnScreen(self)
        panel = wx.Panel(self)

        sizer = wx.GridBagSizer(5, 5)

        text1 = wx.StaticText(panel, label="Machine")
        sizer.Add(text1, pos=(2, 0), flag=wx.LEFT, border=10)

        comboBox1Choices = [ u"LDR2", u"AT02" ]
       # self.m_comboBox4 = wx.ComboBox( panel, wx.ID_ANY, u"Running", wx.DefaultPosition, wx.DefaultSize, m_comboBoxChoices, 0 )
        self.comboBox1 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox1Choices, 0 )
        #comboBox1.SetSelection(1)
        sizer.Add( self.comboBox1, pos=(2, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        text2 = wx.StaticText(panel, label="Sim Port")
        sizer.Add(text2, pos=(3, 0), flag=wx.LEFT|wx.TOP, border=10)

        comboBox2Choices = [ u"22000", u"23000" ]
        self.comboBox2 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox2Choices, 0 )
        sizer.Add( self.comboBox2, pos=(3, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        text3 = wx.StaticText(panel, label="Sim Scripts")
        sizer.Add(text3, pos=(4, 0), flag=wx.TOP|wx.LEFT, border=10)

        comboBox3Choices = [ u"LF12-Ldr", u"LF1-Trk",u"CN1-Trk" ]
        self.comboBox3 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox3Choices, 0 )
        sizer.Add( self.comboBox3, pos=(4, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        text4 = wx.StaticText(panel, label="Status")
        sizer.Add(text4, pos=(5, 0), flag=wx.TOP|wx.LEFT, border=10)

        comboBox4Choices = [ u"Not Started", u"Running" ]
        self.comboBox4 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox4Choices, 0 )
        sizer.Add( self.comboBox4, pos=(5, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        self.button4 = wx.Button(panel, label="Update and Close")
        sizer.Add(self.button4, pos=(7, 3))

        sizer.AddGrowableCol(2)

        panel.SetSizer(sizer)

        self.button4.Bind(wx.EVT_BUTTON,self.onupdateandClose)

    def onupdateandClose(self,event):

       # self.index = 0
        self.main = MyForm()
        sc = self.comboBox1.GetSelection()
        print sc
        sd = self.comboBox1.GetString(sc)
        print sd
        #sa = self.main.m_staticText5.SetLabel(self.comboBox1.GetString(sc))
        #sa = self.frame1.m_staticText4.SetLabel(self.comboBox1.GetString(sc))
        #print sa
        cb1 = self.comboBox1.GetValue()
        #print sc
        cb2 = self.comboBox2.GetValue()
        cb3 = self.comboBox3.GetValue()
        cb4 = self.comboBox4.GetValue()
        #num_items = self.main.list_ctrl.GetItemCount()
        #print num_items
        num_items = 0

        #line = "Line %s" % self.index
        #print line
        line = 'LDR2'

        self.main.list_ctrl.InsertStringItem(num_items,line)
        self.main.list_ctrl.SetStringItem(num_items, 1, 'gjg')
        self.main.list_ctrl.SetStringItem(num_items, 2, str(cb2))
        num_items += 1
        self.Close()


    def test(self, event):
        self.new = NewWindow(parent=None, id=-1)
        self.new.Show()
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

【问题讨论】:

    标签: wxpython


    【解决方案1】:

    我不想更改您的代码。我只添加了代码。

    import wx
    
    ########################################################################
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial",size=(700, 400))
    
            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            self.index = 0
    
            self.list_ctrl = wx.ListCtrl(panel, size=(-1,200),
                             style=wx.LC_REPORT
                             |wx.BORDER_SUNKEN
                             )
            self.list_ctrl.InsertColumn(0, 'Machine Name')
            self.list_ctrl.InsertColumn(1, 'Sim Port')
            self.list_ctrl.InsertColumn(2, 'Sim Script', width=125)
            self.list_ctrl.InsertColumn(3, 'Status', width=150)
            self.list_ctrl.InsertColumn(4, 'Status Detail', width=300)
    
            btn = wx.Button(panel, label="Add Line")
            btn.Bind(wx.EVT_BUTTON, self.add_line)
    
            btn2 = wx.Button(panel,label = "Add Script")
            btn2.Bind(wx.EVT_BUTTON, self.add_script)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
            sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
            sizer.Add(btn2,0,wx.ALL|wx.CENTER, 5)
            panel.SetSizer(sizer)
    
            menu = wx.Menu()
            #exit = menu.Append(-1, "Exit")
            #self.Bind(wx.EVT_MENU, self.OnExit, exit)
            menuBar = wx.MenuBar()
            self.SetMenuBar(menuBar)
            #wx.StaticText(self.m_panel1, -1,(25,25))
            self.popupmenu = wx.Menu()
            for text in "Start Stop Remove".split():
                item = self.popupmenu.Append(-1, text)
                self.Bind(wx.EVT_MENU, self.OnPopupItemSelected, item)
                self.list_ctrl.Bind(wx.EVT_CONTEXT_MENU, self.ShowPopUp)
    
            #self.list_ctrl.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.ShowPopUp)
        #----------------------------------------------------------------------
    
        def add_script(self, event):
            # mp is item 2
            self.new = NewWindow(self, id=-1)
            self.new.Show()
    
        def add_line(self, event):
            #line = "Line %s" % self.index
            line = 'LDR2'
            self.list_ctrl.InsertStringItem(self.index,line)
            self.list_ctrl.SetStringItem(self.index, 1, "22000")
            self.list_ctrl.SetStringItem(self.index, 2, "LF1-trk")
            self.list_ctrl.SetStringItem(self.index, 3, "running")
            self.list_ctrl.SetStringItem(self.index, 4, "last Status info received")
            self.index += 1
    
        def add_line_dynamic(self, machine_name, sim_port, sim_script, status):
            print machine_name, sim_port, sim_script, status
            self.list_ctrl.InsertStringItem(self.index, machine_name)
            self.list_ctrl.SetStringItem(self.index, 1, sim_port)
            self.list_ctrl.SetStringItem(self.index, 2, sim_script)
            self.list_ctrl.SetStringItem(self.index, 3, status)
            self.list_ctrl.SetStringItem(self.index, 4, "none")
            self.index += 1
    
        def OnPopupItemSelected(self, event):
            item = self.popupmenu.FindItemById(event.GetId())
            text = item.GetText()
            wx.MessageBox("You selected item '%s'" % text)
    
        def ShowPopUp( self, event ):
            #pos = self.list_ctrl.GetPosition()
            pos = event.GetPosition()
            pos = self.list_ctrl.ScreenToClient(pos)
            self.list_ctrl.PopupMenu(self.popupmenu,pos)
            event.Skip()
    
    class NewWindow(wx.Frame):
    
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
            #self.frame1 = MainWindow;
            self.parent = parent
    
            wx.Frame.CenterOnScreen(self)
            panel = wx.Panel(self)
    
            sizer = wx.GridBagSizer(5, 5)
    
            text1 = wx.StaticText(panel, label="Machine")
            sizer.Add(text1, pos=(2, 0), flag=wx.LEFT, border=10)
    
            comboBox1Choices = [ u"LDR2", u"AT02" ]
           # self.m_comboBox4 = wx.ComboBox( panel, wx.ID_ANY, u"Running", wx.DefaultPosition, wx.DefaultSize, m_comboBoxChoices, 0 )
            self.comboBox1 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox1Choices, 0 )
            #comboBox1.SetSelection(1)
            sizer.Add( self.comboBox1, pos=(2, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )
    
            text2 = wx.StaticText(panel, label="Sim Port")
            sizer.Add(text2, pos=(3, 0), flag=wx.LEFT|wx.TOP, border=10)
    
            comboBox2Choices = [ u"22000", u"23000" ]
            self.comboBox2 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox2Choices, 0 )
            sizer.Add( self.comboBox2, pos=(3, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )
    
            text3 = wx.StaticText(panel, label="Sim Scripts")
            sizer.Add(text3, pos=(4, 0), flag=wx.TOP|wx.LEFT, border=10)
    
            comboBox3Choices = [ u"LF12-Ldr", u"LF1-Trk",u"CN1-Trk" ]
            self.comboBox3 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox3Choices, 0 )
            sizer.Add( self.comboBox3, pos=(4, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )
    
            text4 = wx.StaticText(panel, label="Status")
            sizer.Add(text4, pos=(5, 0), flag=wx.TOP|wx.LEFT, border=10)
    
            comboBox4Choices = [ u"Not Started", u"Running" ]
            self.comboBox4 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox4Choices, 0 )
            sizer.Add( self.comboBox4, pos=(5, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )
    
            self.button4 = wx.Button(panel, label="Update and Close")
            sizer.Add(self.button4, pos=(7, 3))
    
            sizer.AddGrowableCol(2)
    
            panel.SetSizer(sizer)
    
            self.button4.Bind(wx.EVT_BUTTON,self.onupdateandClose)
    
        def onupdateandClose(self,event):
    
           # self.index = 0
            self.main = MyForm()
            sc = self.comboBox1.GetSelection()
            machine_name = self.comboBox1.GetString(sc)
            #sa = self.main.m_staticText5.SetLabel(self.comboBox1.GetString(sc))
            #sa = self.frame1.m_staticText4.SetLabel(self.comboBox1.GetString(sc))
            #print sa
            machine_name = self.comboBox1.GetValue()
            #print sc
            sim_port = self.comboBox2.GetValue()
            sim_script = self.comboBox3.GetValue()
            status = self.comboBox4.GetValue()
            #num_items = self.main.list_ctrl.GetItemCount()
            #print num_items
            num_items = 0
    
            #line = "Line %s" % self.index
            #print line
            # line = 'LDR2'
    
            # self.main.list_ctrl.InsertStringItem(num_items,line)
            # self.main.list_ctrl.SetStringItem(num_items, 1, 'gjg')
            # self.main.list_ctrl.SetStringItem(num_items, 2, str(cb2))
            # num_items += 1
    
            self.parent.add_line_dynamic(machine_name, sim_port, sim_script, status)
            self.Close()
    
    
        # def test(self, event):
        #     self.new = NewWindow(parent=None, id=-1)
        #     self.new.Show()
    #----------------------------------------------------------------------
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

    我在 MyForm 类中添加了一个函数,*名为 add_line_dynamic(machine_name, sim_port, sim_script, status)*。

    def add_line_dynamic(self, machine_name, sim_port, sim_script, status):
            print machine_name, sim_port, sim_script, status
            self.list_ctrl.InsertStringItem(self.index, machine_name)
            self.list_ctrl.SetStringItem(self.index, 1, sim_port)
            self.list_ctrl.SetStringItem(self.index, 2, sim_script)
            self.list_ctrl.SetStringItem(self.index, 3, status)
            self.list_ctrl.SetStringItem(self.index, 4, "none")
            self.index += 1
    

    这个函数将从另一个类 NewWindow() 中调用。 通话是与父母联系的。 *self.parent.add_line_dynamic(machine_name, sim_port, sim_script, status).*

    self.parent.add_line_dynamic(machine_name, sim_port, sim_script, status)
    

    NewWindow 是从 MyForm 调用的。而NewWindow 知道他的父母Myform。您可以从他的父级调用另一个函数并将参数传递给该类。 我希望你明白其中的逻辑。

    附言对不起我的英语:)

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2014-04-05
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 2013-05-16
      相关资源
      最近更新 更多