WxPython使用Listbox和其他带有Button的UserInput

时间:2015-03-12 19:11:29

标签: python listbox wxpython web-crawler

我正在尝试根据特定用户输入创建网络抓取工具。例如,我尝试接收的用户输入来自ListBox和文本字段。一旦我掌握了这些信息,我希望用户点击一个按钮,开始搜索所收集的信息。

这是我遇到问题的地方。 EVT功能无法识别列表框,因为它已链接到Button evt。有办法解决问题吗? EVT信息可以与其他EVT共享吗?

这是我到目前为止所做的:

import wx  # for gui


class MyFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
        tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
        levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
        levelBox.SetSelection(1)  # default selection
        checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
        self.Bind(wx.EVT_BUTTON, self.OnClick, checkButton)

    def OnClick(self, event):
        currLevel = event.GetSelection()
        print(currLevel) # to test if GetSelection is working


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

如果我能让按钮识别ListBox结果,我会很高兴。 谢谢你的时间!

2 个答案:

答案 0 :(得分:1)

您可以从列表框中抓取它,您不需要从事件中获取它。见下文:

import wx  # for gui


class MyFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
        tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
        self.levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
        self.levelBox.SetSelection(1)  # default selection
        self.checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.checkButton)

    def OnClick(self, event):
        currLevel = self.levelBox.GetSelection()
        print(currLevel) # to test if GetSelection is working


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

更具体地说,如果将levelBox存储为self.levelBox,则可以在OnClick方法中将其作为MyFrame属性访问。然后,您可以对此对象使用GetSelection方法(该事件),这将获得当前选择。

答案 1 :(得分:0)

您可以将levelBox转换为self.levelBox并将其作为@brettb提及的方式访问,从而使import wx # for gui class MyFrame(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self, parent, id, 'Title', size=(300,200)) panel = wx.Panel(self) tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000'] levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel) levelBox.SetSelection(1) # default selection checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40)) evt = lambda caller, widget=levelBox: self.OnClick(caller, widget) checkButton.Bind(wx.EVT_BUTTON, evt) def OnClick(self, event, widget): currLevel = widget.GetSelection() print(currLevel) # to test if GetSelection is working print widget.GetString(currLevel) if __name__ == '__main__': app = wx.App() frame = MyFrame(parent=None, id=-1) frame.Show() app.MainLoop() 成为该类的属性。但是你可以稍微狡猾,并使用lambda为你的回调将Listbox小部件传递给事件处理程序:

panel

另请注意,您没有定义{{1}},因此您的原始代码无效。有关更多信息,请参阅以下内容: