{"id":95,"date":"2018-08-16T20:16:04","date_gmt":"2018-08-16T12:16:04","guid":{"rendered":"http:\/\/172.81.211.114\/?p=95"},"modified":"2018-08-16T20:16:04","modified_gmt":"2018-08-16T12:16:04","slug":"95","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=95","title":{"rendered":"wxpython \u7684pub\/sub\u53d1\u5e03\u8ba2\u9605\uff0c\u5938\u7a97\u53e3\u4f20\u9012\u4fe1\u606f"},"content":{"rendered":"<p>\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u4e24\u4e2a\u7a97\u53e3\u4e4b\u95f4\u4e92\u76f8\u4f20\u9012\u4fe1\u606f\uff0c\u6216\u8005\u628a\u4e00\u4e2a\u7a97\u53e3\u91cc\uff0c\u6587\u672c\u6846\u8f93\u5165\u7684\u503c\uff0c\u4f20\u9012\u5230\u53e6\u5916\u4e00\u4e2aframe\u7684\u53e6\u5916\u4e00\u4e2a\u6587\u672c\u6846\u91cc\uff0c<br \/>\n\u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528wxpython\u7684pub\/sub\u65b9\u6cd5\u3002<br \/>\ntest.py<\/p>\n<pre class=\"hljs ruby\"><code class=\"ruby\">\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-title\">wx<\/span>.<span class=\"hljs-title\">App<\/span>):<\/span>\n          <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">self<\/span>)<\/span><\/span>:\n                  <span class=\"hljs-comment\">#mainFrame \u662f\u7528wxglade\u753b\u7684\u4e00\u4e2aframe\u5bfc\u51fa\u6765\u7684python\u6587\u4ef6<\/span>\n                   <span class=\"hljs-keyword\">self<\/span>.frame1 = mainFrame.MyFrame(None, wx.ID_ANY, <span class=\"hljs-string\">\"\"<\/span>)\n                   <span class=\"hljs-keyword\">self<\/span>.frame1.Show()\n                   <span class=\"hljs-keyword\">self<\/span>.children=test2.children()\n                  <span class=\"hljs-comment\">#\u5b9a\u4e49\u4e00\u4e2aname1\u5c5e\u6027\uff0c\u503c\u6765\u81ea\u4e8e\u7a97\u53e3\u4e0a\u7684\u6587\u672c\u6846\u63a7\u4ef6<\/span>\n                   <span class=\"hljs-keyword\">self<\/span>.name1 = <span class=\"hljs-keyword\">self<\/span>.frame1.text_ctrl_1\n                  <span class=\"hljs-comment\">#  \u79bb\u5f00\u7126\u70b9\uff0c\u5c31\u628afram1\u7684\u6587\u672c\u6846\u91cc\u7684\u503c\u4f20\u5165children\u7684\u53e6\u5916\u4e00\u4e2a\u6587\u672c\u6846\u5185<\/span>\n                  <span class=\"hljs-keyword\">self<\/span>.name1 .Bind(wx.EVT_COMMAND_KILL_FOCUS, <span class=\"hljs-keyword\">self<\/span>.save)\n                  <span class=\"hljs-comment\">#\u53d1\u5e03\u4e00\u4e2a\u53ebin_sync\u7684\u4e3b\u9898\uff0c\u89e6\u53d1sync\u51fd\u6570<\/span>\n                   pub.subscribe(<span class=\"hljs-keyword\">self<\/span>.sync, <span class=\"hljs-string\">\"in_sync\"<\/span>)\n          <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">sync<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">self<\/span>, msg)<\/span><\/span>:\n                  <span class=\"hljs-comment\">#\u7ed9children\u7684text_ctrl_2\u5b57\u6bb5\u8bbe\u7f6e\u503c<\/span>\n                  <span class=\"hljs-keyword\">self<\/span>.children.frame.text_ctrl_2.SetValue(msg)\n          <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">save<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">self<\/span>,evt)<\/span><\/span>:\n                    value=<span class=\"hljs-keyword\">self<\/span>.name1.GetValue()\n                    <span class=\"hljs-comment\">#\u8c03\u7528\u4e00\u4e0bin_sync\u65b9\u6cd5\uff0c\u5982\u679cin_sync\u65b9\u6cd5\u5728\u522b\u7684class\u91cc\uff0c\n                    #\u4e5f\u662f\u53ef\u4ee5\u8c03\u7528pub.sendMessage\u6210\u529f\u7684<\/span>\n                    <span class=\"hljs-keyword\">self<\/span>.children.in_sync(value)\n<\/code><\/pre>\n<p>test2.py<\/p>\n<pre class=\"hljs ruby\"><code class=\"ruby\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">children<\/span>(<span class=\"hljs-title\">wx<\/span>.<span class=\"hljs-title\">App<\/span>):<\/span>\n           <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">self<\/span>)<\/span><\/span>:\n                  <span class=\"hljs-comment\">#mainFrame \u662f\u7528wxglade\u753b\u7684\u4e00\u4e2aframe\u5bfc\u51fa\u6765\u7684python\u6587\u4ef6<\/span>\n                   <span class=\"hljs-keyword\">self<\/span>.frame = mainFrame.MyFrame(None, wx.ID_ANY, <span class=\"hljs-string\">\"\"<\/span>)\n                   <span class=\"hljs-keyword\">self<\/span>.frame.Show()\n           <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">in_sync<\/span><span class=\"hljs-params\">(text)<\/span><\/span>:\n                 <span class=\"hljs-comment\">#\u8ba2\u9605\"in_sync\"\u4e3b\u9898<\/span>\n                 wx.CallAfter(pub.sendMessage, <span class=\"hljs-string\">\"in_sync\"<\/span>, msg=text)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u4e24\u4e2a\u7a97\u53e3\u4e4b\u95f4\u4e92\u76f8\u4f20\u9012\u4fe1\u606f\uff0c\u6216\u8005\u628a\u4e00\u4e2a\u7a97\u53e3\u91cc\uff0c\u6587\u672c\u6846\u8f93\u5165\u7684\u503c\uff0c\u4f20\u9012\u5230\u53e6\u5916\u4e00\u4e2aframe\u7684\u53e6\u5916\u4e00\u4e2a\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[12],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-python","tag-wxpython"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":0,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}