无法理解如何从wxpython的checklistbox中获取数据

2024-10-01 13:40:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从检查列表中获取字符串或整数。我好像哪儿也弄不到。在下面的代码中,您将看到一堆未注释的代码,这些只是我尝试过的不同方法。我想我会离开他们,以防有人提出建议。我对GUI编程和wx非常陌生。谢谢你的帮助。在

import  wx


class Panel1(wx.Panel):
def __init__(self, parent, log):
    wx.Panel.__init__(self, parent, -1)

    allLoc = ['One', 'Two', 'Three', 'Four']

    wx.StaticText(self, -1, "Choose:", (45, 15))

    citList = wx.CheckListBox(self, -1, (60, 50), wx.DefaultSize, allLoc)

    #self.Bind(wx.EVT_CHECKLISTBOX, self.GetChecks, citList)

    #h = citList.GetChecked()

    #cities = ()

    #v = cities.append(h)
    #print h
    pos = citList.GetPosition().x + citList.GetSize().width + 25
    nextBtn = wx.Button(self, -1, "Next", (pos, 50))
    self.Bind(wx.EVT_BUTTON, wx.EvyRadBox2, nextBtn)

#def GetChecks(self, event):
    #r = citList.GetId()
    #print r

#def printChecks(self, event):
    #l = event.CetCheckStrings()
    #print l

def EvyRadBox2(self, event):
    filepath2 = "c:\\logger2.txt"
    file1 = open(filepath2, 'w')
    file1.write('%d \n' % event.GetChecked())
    file1.close()





app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "Charlie")
Panel1(frame,-1)
frame.Show(1)
app.MainLoop()

**********************编辑**********************************

所以我把代码改成这样

^{pr2}$

当我一开始这样做的时候,当我单击按钮时,它会将一个未定义的全局名称放入wxstdout中。我在上面加了一个检查表,起初它显示了一个空列表,现在它显示了一个空列表。任何帮助,一如既往,提前谢谢


Tags: 代码selfevent列表initdefframefile1
1条回答
网友
1楼 · 发布于 2024-10-01 13:40:08
checkedItems = [i for i in range(citList.GetCount()) if citList.IsChecked(i)]

citList.GetChecked()也应该为您完成任务。问题可能是您试图在__init__中获取所选项目吗?

升级。您不希望在__init__期间得到检查的项目-此时用户无法检查这些项目。最好在任何事件处理程序中检查它们,例如wx.EVT_BUTTON。在

试着多写些self,例如:

^{pr2}$

并将Clicked更改为:

def Clicked(self, event):
    checkedItems = [i for i in range(self.panel.citList.GetCount()) if self.panel.citList.IsChecked(i)]
    print checkedItems
    event.Skip()

希望这有帮助。在

相关问题 更多 >