Python MFC: Updating static text in response to events -
i have following python mfc code. have listbox fill values, , when user clicks on values want static text control updated current selection. there 2 problems code. first value in text control updated first time click on listbox. second value lags behind real selected value in listbox, presumably because control handles click after handler code gets called. i'd appreciate either of these issues.
an odd thing, (perhaps clue) when mouse-down on 'ok' button move away mouse-up static text updated expect.
i've tried redrawwindow(), updatewindow(), showwindow() on both control , dialog, , nothing seems make difference.
import win32con pywin.mfc import dialog idc_list = 9000 idc_text = 9001 class chooserdialog(dialog.dialog): def __init__(self): dialogtemplate = [ ["test", (0, 0, 254, 199), win32con.ws_caption | win32con.ds_modalframe, none, (8, "ms sansserif")], [128, "ok", win32con.idok, (197,178,50,14), win32con.bs_pushbutton | win32con.ws_visible], ["listbox", "list", idc_list, (7,7,177,186), win32con.ws_visible], ["static", "", idc_text, (197,7,50,160), win32con.ws_child | win32con.ws_visible] ] dialog.dialog.__init__(self, dialogtemplate) def oninitdialog(self): rc = dialog.dialog.oninitdialog(self) in ["one", "two", "three"]: self.getdlgitem(idc_list).addstring(i) self.hookcommand(self.onnotify, idc_list) return rc def onnotify(self, ctrl, action): if ctrl == idc_list: selected = self.getdlgitem(idc_list).getcursel() self.setdlgitemtext(idc_text, "%d" % selected) self.getdlgitem(idc_text).redrawwindow() return 1 dia = chooserdialog() dia.domodal()
the first problem code win32con.lbs_notify style wasn't set listbox control. if isn't set won't messages lb. few messages getting due other events in dialog.
the second problem was using hookcommand(), intercepts commands, , allows handle them. instead wanted hookmessage() notifications. seems notifications called after update of control, that's wanted.
the lbn_selchange notification, according msdn documentation received through wm_command message. seems must subscribe wm_command messages in dialog, , filter them in handler. lbn_selchange documentation explains passed, , in conjunction hookmessage() python documentation can work out how handle it.
here working code:
import win32con pywin.mfc import dialog idc_list = 9500 idc_text = 9501 class chooserdialog(dialog.dialog): def __init__(self): dialogtemplate = [ ["test", (0, 0, 254, 199), win32con.ws_caption | win32con.ds_modalframe, none, (8, "ms sansserif")], [128, "ok", win32con.idok, (197,178,50,14), win32con.bs_pushbutton | win32con.ws_visible], ["listbox", "list", idc_list, (7,7,177,186), win32con.ws_visible|win32con.lbs_notify], ["static", "", idc_text, (197,7,50,160), win32con.ws_child | win32con.ws_visible] ] dialog.dialog.__init__(self, dialogtemplate) def oninitdialog(self): rc = dialog.dialog.oninitdialog(self) in ["one", "two", "three"]: self.getdlgitem(idc_list).addstring(i) self.hookmessage(self.onnotifycommand, win32con.wm_command) return rc def onnotifycommand(self, data): msg_id = data[1] # should wm_command wparam = data[2] lparam = data[3] list_id = wparam & 0xffff notification_code = (wparam & 0xffff0000) >> 16 if list_id != idc_list: return # not our list box. if notification_code != win32con.lbn_selchange: return # not change of selection selected = self.getdlgitem(idc_list).getcursel() self.setdlgitemtext(idc_text, "%d"%selected) dia = chooserdialog() dia.domodal()
Comments
Post a Comment