无法理解如何从 wxpython 中的 checklistbox 获取数据 [英] Cannot understand how to get data from checklistbox in wxpython

查看:24
本文介绍了无法理解如何从 wxpython 中的 checklistbox 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查字符串或检查列表中的整数.我似乎无法在任何地方得到它.在下面的代码中,您会看到一堆未注释的代码,这些只是我尝试过的不同方式.我想我会离开他们以防万一有人的建议与它有关.我对 GUI 编程和 wx 很陌生.感谢您的帮助.

I am trying to get either the strings checked or the integers from a check list. I cannot seem to get it anywhere. In the code below, you'll see a bunch of un-commented code, those are just different ways I've tried. I thought I would leave them in case any one's suggestions have to do with it. I am very new to GUI-programming and wx. Thanks for your help.

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()

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

**************************EDIT********************************

所以我把代码改成这样

import wx

checkedItems = []


class citPanel(wx.Panel):
def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    allLoc = ['One', 'Two', 'Three', 'Four']

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

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

    checkedItems = [i for i in range(citList.GetCount()) if citList.IsChecked(i)]


class nextButton(wx.Button):
def __init__(self, parent, id, label, pos):
    wx.Button.__init__(self, parent, id, label, pos)




class checkList(wx.Frame):
def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(400, 400))

    panel = citPanel(self, -1)

    nextButton(panel, -1, 'Ok', (275, 50))

    self.Bind(wx.EVT_BUTTON, self.Clicked)

    self.Centre()
    self.Show(True)

def Clicked(self, event):
    print checkedItems
    event.Skip()


app = wx.App()
checkList(None, -1, 'Charlie')
app.MainLoop()

当我最初这样做时,当我单击按钮时,它抛出了一个未定义到 wxstdout 的全局名称.我在顶部添加了检查清单,它起初没有显示,现在显示一个空列表.,一如既往的任何帮助,提前致谢

When I did this at first, when I clicked the button, It threw a global name not defined into wxstdout. I add the checkedlist at the top, and it at first showed none, now it shows an empty list., Any help, as always, thanks in advance

推荐答案

checkedItems = [i for i in range(citList.GetCount()) if citList.IsChecked(i)]

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

citList.GetChecked() should have also completed the task for you. May the problem be that you are trying to get selected items in __init__?

更新:您不希望在 __init__ 期间获得已检查的项目 - 用户此时无法检查它们.您最好在任何事件处理程序中检查它们,例如wx.EVT_BUTTON.

Upd.: You do not want to get checked items during __init__ - they cannot be checked by the user at that moment. You'd better check them in any event handler, e.g. wx.EVT_BUTTON.

尝试更频繁地编写self,例如:

Try writing self more often, e.g.:

self.citList = wx.CheckListBox(self, -1, (60, 50), wx.DefaultSize, allLoc)
# some code
self.panel = citPanel(self, -1)

并将 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()

希望这会有所帮助.

这篇关于无法理解如何从 wxpython 中的 checklistbox 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆