python - wxPython Acting on Element with Focus -
in program have notebook vertical box sizer buttons next it. buttons performed actions on list box had on frame. moved list boxes tabs of notebook. how can act on elements of tab button not child of notebook sibling. means variable listbox no longer in same scope.
this brings me other part of question. if had 2 listboxes side side , buttons, want buttons act on whichever listbox has mouse focus. how achieve this?
i assume 2 questions related wrong. want these buttons able act on whatever tab or items selected.
a small example wonders me understand element of wxpython. i'm having trouble docs , examples have found far. know has factory i'm not sure how , can't find examples work.
i realize think i'm having trouble understanding event argument passed functions bind. how solve issue? how it?
thanks
this fun little exercise. want traverse widgets of selected tab , check see item selected in listbox.
note: mentioned in comment question, problem see listbox has focus until moment press button. press button, button has focus.
here's 1 approach:
import random import wx ######################################################################## class tabpanel(wx.panel): #---------------------------------------------------------------------- def __init__(self, parent): """""" wx.panel.__init__(self, parent=parent) colors = ["red", "blue", "gray", "yellow", "green"] self.setbackgroundcolour(random.choice(colors)) lbox = wx.listbox(self, choices=colors) sizer = wx.boxsizer(wx.vertical) sizer.add(lbox, 0, wx.all, 10) self.setsizer(sizer) ######################################################################## class demoframe(wx.frame): """ frame holds other widgets """ #---------------------------------------------------------------------- def __init__(self): """constructor""" wx.frame.__init__(self, none, wx.id_any, "notebook tutorial", size=(600,400) ) panel = wx.panel(self) self.tab_num = 3 self.notebook = wx.notebook(panel) tabone = tabpanel(self.notebook) self.notebook.addpage(tabone, "tab 1") tabtwo = tabpanel(self.notebook) self.notebook.addpage(tabtwo, "tab 2") sizer = wx.boxsizer(wx.vertical) sizer.add(self.notebook, 1, wx.all|wx.expand, 5) btn = wx.button(panel, label="get color") btn.bind(wx.evt_button, self.addpage) sizer.add(btn) panel.setsizer(sizer) self.layout() self.show() #---------------------------------------------------------------------- def addpage(self, event): """""" page = self.notebook.getcurrentpage() widget in page.getchildren(): if isinstance(widget, wx.listbox): selection = widget.getstring(widget.getselection()) if selection: print "current color = " + selection else: print "no color selected" #---------------------------------------------------------------------- if __name__ == "__main__": app = wx.app(false) frame = demoframe() app.mainloop()
i annoying error of sorts on xubuntu, think it's warning , doesn't effect running code above. getting:
python:12770): gtk-critical **: ia__gtk_tree_model_iter_nth_child: assertion 'n >= 0' failed
i don't believe worry though. it's artifact of using version of wxpython has warnings enabled.
Comments
Post a Comment