wxpython How to change the values of widgets in other panel -
my frame split in 3 panels, in paneltwo, there textctrl, in panelthree , there textctrl too, when input in textctrl of paneltwo, need change value of textctrl in panelthree, how it?
# -*- coding: utf-8 -*- import wx import sys class panelone(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.setbackgroundcolour("blue") font = wx.font(12, wx.decorative, wx.normal, wx.bold) button1 = wx.button(self, -1, label='button1') button2 = wx.button(self, -1, label='button2') button1.setfont(font) button2.setfont(font) vbox = wx.boxsizer(wx.vertical) vbox.add(button1, 1, wx.expand) vbox.add(button2, 1, wx.expand) self.setsizer(vbox) class paneltwo(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) textctrl = wx.textctrl(self, -1, pos=(200,120), style=wx.te_process_enter|wx.center) textctrl.bind(wx.evt_key_down, self.onenter) def onenter(self, event): keycode = event.getkeycode() if keycode == wx.wxk_return or keycode == wx.wxk_numpad_enter: """here, want print message in panelthree textctrl """ event.skip() class panelthree(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.setbackgroundcolour("white") text = wx.textctrl(self, -1, style = wx.te_multiline|wx.te_readonly| wx.hscroll) vbox = wx.boxsizer(wx.vertical) vbox.add(text, 1, wx.expand) self.setsizer(vbox) ######################################################################## class mainpanel(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) topsplitter = wx.splitterwindow(self, size=(800,600)) vsplitter = wx.splitterwindow(topsplitter) self.panelone = panelone(vsplitter) self.paneltwo = paneltwo(vsplitter) self.panelthree = panelthree(topsplitter) vsplitter.splitvertically(self.panelone, self.paneltwo) vsplitter.setsashgravity(0.3) topsplitter.splithorizontally(vsplitter, self.panelthree) topsplitter.setsashgravity(0.5) sizer = wx.boxsizer(wx.vertical) sizer.add(topsplitter, 1, wx.expand) ######################################################################## class mainframe(wx.frame): """""" #---------------------------------------------------------------------- def __init__(self): """constructor""" wx.frame.__init__(self, none, title='my program', size=(800,600)) panel = mainpanel(self) self.show() #---------------------------------------------------------------------- if __name__ == "__main__": app = wx.app(false) frame = mainframe() app.mainloop()
iam not sure if best way it. anyways, i've modified code below:
# -*- coding: utf-8 -*- import wx import sys class panelone(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.setbackgroundcolour("blue") font = wx.font(12, wx.decorative, wx.normal, wx.bold) button1 = wx.button(self, -1, label='button1') button2 = wx.button(self, -1, label='button2') button1.setfont(font) button2.setfont(font) vbox = wx.boxsizer(wx.vertical) vbox.add(button1, 1, wx.expand) vbox.add(button2, 1, wx.expand) self.setsizer(vbox) class paneltwo(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.textctrl = wx.textctrl(self, -1, pos=(200,120), style=wx.te_process_enter|wx.center) class panelthree(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.setbackgroundcolour("white") self.text = wx.textctrl(self, -1, style = wx.te_multiline|wx.te_readonly| wx.hscroll) vbox = wx.boxsizer(wx.vertical) vbox.add(self.text, 1, wx.expand) self.setsizer(vbox) ######################################################################## class mainpanel(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) topsplitter = wx.splitterwindow(self, size=(800,600)) vsplitter = wx.splitterwindow(topsplitter) self.panelone = panelone(vsplitter) self.paneltwo = paneltwo(vsplitter) self.panelthree = panelthree(topsplitter) self.paneltwo.textctrl.bind(wx.evt_key_down, self.onenter) vsplitter.splitvertically(self.panelone, self.paneltwo) vsplitter.setsashgravity(0.3) topsplitter.splithorizontally(vsplitter, self.panelthree) topsplitter.setsashgravity(0.5) sizer = wx.boxsizer(wx.vertical) sizer.add(topsplitter, 1, wx.expand) def onenter(self, event): keycode = event.getkeycode() if keycode == wx.wxk_return or keycode == wx.wxk_numpad_enter: """here, want print message in panelthree textctrl """ self.panelthree.text.setvalue("enter pressed") else: self.panelthree.text.setvalue("") event.skip() ######################################################################## class mainframe(wx.frame): """""" #---------------------------------------------------------------------- def __init__(self): """constructor""" wx.frame.__init__(self, none, title='my program', size=(800,600)) panel = mainpanel(self) self.show() #---------------------------------------------------------------------- if __name__ == "__main__": app = wx.app(false) frame = mainframe() app.mainloop()
edit:
however, referring greenasjade's answer question, using pubsub
seems cleaner way of achieving it.
# -*- coding: utf-8 -*- import wx import sys wx.lib.pubsub import pub class panelone(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.setbackgroundcolour("blue") font = wx.font(12, wx.decorative, wx.normal, wx.bold) button1 = wx.button(self, -1, label='button1') button2 = wx.button(self, -1, label='button2') button1.setfont(font) button2.setfont(font) vbox = wx.boxsizer(wx.vertical) vbox.add(button1, 1, wx.expand) vbox.add(button2, 1, wx.expand) self.setsizer(vbox) class paneltwo(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) textctrl = wx.textctrl(self, -1, pos=(200,120), style=wx.te_process_enter|wx.center) textctrl.bind(wx.evt_key_down, self.onenter) def onenter(self, event): keycode = event.getkeycode() pub.sendmessage("change_text",value="") if keycode == wx.wxk_return or keycode == wx.wxk_numpad_enter: """here, want print message in panelthree textctrl """ pub.sendmessage("change_text", value = "enter pressed") event.skip() class panelthree(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.setbackgroundcolour("white") self.text = wx.textctrl(self, -1, style = wx.te_multiline|wx.te_readonly| wx.hscroll) vbox = wx.boxsizer(wx.vertical) vbox.add(self.text, 1, wx.expand) self.setsizer(vbox) pub.subscribe(self.printmessage, "change_text") def printmessage(self, value): self.text.setvalue(value) ######################################################################## class mainpanel(wx.panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) topsplitter = wx.splitterwindow(self, size=(800,600)) vsplitter = wx.splitterwindow(topsplitter) self.panelone = panelone(vsplitter) self.paneltwo = paneltwo(vsplitter) self.panelthree = panelthree(topsplitter) vsplitter.splitvertically(self.panelone, self.paneltwo) vsplitter.setsashgravity(0.3) topsplitter.splithorizontally(vsplitter, self.panelthree) topsplitter.setsashgravity(0.5) sizer = wx.boxsizer(wx.vertical) sizer.add(topsplitter, 1, wx.expand) ######################################################################## class mainframe(wx.frame): """""" #---------------------------------------------------------------------- def __init__(self): """constructor""" wx.frame.__init__(self, none, title='my program', size=(800,600)) panel = mainpanel(self) self.show() #---------------------------------------------------------------------- if __name__ == "__main__": app = wx.app(false) frame = mainframe() app.mainloop()
Comments
Post a Comment