【问题标题】:Cannot understand how to get data from checklistbox in wxpython无法理解如何从 wxpython 中的复选框获取数据
【发布时间】:2010-01-04 19:45:42
【问题描述】:

我正在尝试从检查列表中检查字符串或整数。我似乎无法在任何地方得到它。在下面的代码中,您会看到一堆未注释的代码,这些只是我尝试过的不同方式。我想我会离开他们,以防任何人的建议与它有关。我对 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()

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

所以我将代码更改为如下所示

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 中的全局名称。我在顶部添加了checklist,起初它没有显示,现在它显示一个空列表。任何帮助,一如既往,提前谢谢

【问题讨论】:

    标签: python user-interface wxpython


    【解决方案1】:
    checkedItems = [i for i in range(citList.GetCount()) if citList.IsChecked(i)]
    

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

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

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

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

    希望这会有所帮助。

    【讨论】:

    • 我可以把它绑定到按钮上吗?
    • 当然,你可以为它写类似self.Bind(wx.EVT_BUTTON, self.EvyRadBox2, nextBtn)的东西(假设你在self.EvyRadBox2中得到检查项目)。
    • 当我在控制台打开的情况下运行时,我看到的只是 (),一个空元组。我不知道是否正在读取支票,我想将它们传递给其他东西,我只是不认为我正在捕获它们。
    • 您可以尝试使用wx.MessageBox('Some info to display', 'Info') 显示您喜欢的任何信息。
    猜你喜欢
    • 2019-03-23
    • 2018-09-10
    • 2018-02-14
    • 1970-01-01
    • 2021-10-07
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多