{"id":168,"date":"2010-07-23T16:10:15","date_gmt":"2010-07-23T08:10:15","guid":{"rendered":"http:\/\/csslayer.tk\/wordpress\/?p=168"},"modified":"2010-07-23T16:16:34","modified_gmt":"2010-07-23T08:16:34","slug":"easy-pyqt-demo","status":"publish","type":"post","link":"https:\/\/www.csslayer.info\/wordpress\/linux\/qt-linux\/easy-pyqt-demo\/","title":{"rendered":"\u7b80\u5355\u6613\u61c2\u7684PyQt Demo"},"content":{"rendered":"<p><a href=\"http:\/\/csslayer.tk\/wordpress\/wp-content\/uploads\/2010\/07\/PyQtDemo.tar.gz\">PyQtDemo.tar.gz<\/a><\/p>\n<p>\u5176\u5b9e\u662f\u4e13\u95e8\u4e3a\u53f3\u4eac\u6837\u4e00\u5144\u6240\u4f5c\uff0c\u641e\u4e86\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u7a97\u53e3\u3002\u95f2\u8bdd\u5c11\u8bf4\uff0c\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\u4e1c\u897f\u3002<\/p>\n<p>\u5185\u5bb9\u7269\u5f88\u7b80\u5355\u3002<br \/>\n<code><br \/>\n.<br \/>\n\u251c\u2500\u2500 build.py<br \/>\n\u251c\u2500\u2500 demowindow.py<br \/>\n\u251c\u2500\u2500 main.pyw<br \/>\n\u2514\u2500\u2500 ui<br \/>\n\u2514\u2500\u2500 demowindow.ui<\/code><\/p>\n<p>\u9996\u5148\u9700\u8981\u8fd0\u884c.\/build.py\u6839\u636eui\u6587\u4ef6\u751f\u6210\u4e00\u4e2apy\u6587\u4ef6\uff0c\u7136\u540e.\/main.pyw\u5c31\u53ef\u4ee5\u8fd0\u884c\u4e86\u3002\u5185\u5bb9\u4e5f\u8d85\u5c11\uff0c\u9996\u5148\u662fmain.pyw\u7684\u5185\u5bb9\uff1a<\/p>\n<pre lang=\"python\">#!\/usr\/bin\/env python\r\n\r\nimport sys\r\nfrom PyQt4.QtGui import QApplication\r\nfrom demowindow import DemoWindow\r\nfrom PyQt4.QtCore import Qt, SIGNAL\r\n\r\nif __name__ == \"__main__\":\r\n    app = QApplication(sys.argv);\r\n    app.connect( app, SIGNAL( \"lastWindowClosed()\" ), app.quit);\r\n    demowindow = DemoWindow()\r\n    demowindow.show()\r\n    sys.exit(app.exec_())\r\n<\/pre>\n<p>\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2aQApplication\u5bf9\u8c61\uff08Qt\u7a0b\u5e8f\u90fd\u9700\u8981\uff09\uff0c\u8bbe\u7f6e\u4e00\u4e2asignal\uff0c\u8868\u793a\u6240\u6709\u7a97\u53e3\u9000\u51fa\u540e\u7a0b\u5e8f\u9000\u51fa\u3002<\/p>\n<p>\u7136\u540e\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a\u6211\u4eec\u7528Qt Designer\u521b\u5efa\u7684\u7a97\u53e3\uff0c\u5e76\u663e\u793a\u3002<\/p>\n<p>\u7136\u540e\u662fdemowindow.py\u7684\u5185\u5bb9\uff1a<\/p>\n<pre lang=\"python\">from PyQt4.QtCore import Qt, SIGNAL\r\nfrom PyQt4.QtGui import *\r\n\r\nfrom ui_demowindow import Ui_DemoWindow\r\n\r\nclass DemoWindow(QWidget, Ui_DemoWindow):\r\n    def __init__(self, parent = None):\r\n        QWidget.__init__(self,parent)\r\n        self.setupUi(self)\r\n        self.connect(self.testButton, SIGNAL(\"clicked()\"), self.test)\r\n        self.connect(self.exitButton, SIGNAL(\"clicked()\"), self.exit)\r\n\r\n    def test(self):\r\n        self.helloLabel.setText(\"abc\");\r\n\r\n    def exit(self):\r\n        self.close()\r\n<\/pre>\n<p>\u5185\u5bb9\u4f9d\u65e7\u7b80\u5355\u660e\u4e86\uff0c\u4e3b\u8981\u5c31\u662f\u5904\u7406\u6309\u94ae\u4e8b\u4ef6\uff0c\u6309\u4e0bTest\u5c31\u4f1a\u628a\u663e\u793a\u7684\u6587\u5b57\u6539\u6210abc\uff0c\u6309\u4e0bExit\u5c31\u662f\u9000\u51fa\u3002\u521d\u59cb\u5316\u51fd\u6570\u7684\u60ef\u4f8b\u5c31\u662f\u9700\u8981\u8c03\u7528setupUi\uff0c\u8fd9\u4e2a\u51fd\u6570\u76f8\u5f53\u4e8e\u5904\u7406\u4e86\u754c\u9762\u7684\u6784\u5efa\u3002\u90a3\u4e48\u539f\u7406\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u5462\uff1f\u9996\u5148\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aui\u6587\u4ef6\uff08\u5229\u7528qt designer\uff0c\u6d82\u6d82\u753b\u753b\u4ec0\u4e48\u7684\uff0c\u6700\u7b80\u5355\u4e86\uff0clayout\u5f97\u81ea\u5df1\u73a9\u73a9\u624d\u641e\u5f97\u660e\u767d\uff09\u3002\u7136\u540e\u5229\u7528pyuic4\u8fd9\u4e2a\u7a0b\u5e8f\u5c06ui\u6587\u4ef6\u8f6c\u6362\u4e3apython\u4ee3\u7801pyuic4 -o ui_demowindow.py ui\/demowindow.ui\uff0c\u8fd9\u91cc\u8fd9\u4e2apython\u6587\u4ef6\u4e2d\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u7c7bUi_classname\uff0c\u8fd9\u4e2aclass name\u5c31\u662fui\u6587\u4ef6\u4e2d\u9876\u7ea7widget\u7684\u540d\u5b57\uff08\u533a\u5206\u5927\u5c0f\u5199\uff09\u3002\u7136\u540e\u4e00\u822c\u90fd\u662f\u5229\u7528\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7c7b\u7ee7\u627fUi_\u7c7b\uff0c\u548c\u5bf9\u5e94\u7684Widget\u7c7b\u578b\u3002\u7136\u540e\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u8c03\u7528setupUi(self)\u521d\u59cb\u5316\u754c\u9762\u3002<\/p>\n<p>\u7136\u540e\u89e3\u91ca\u4e00\u4e0bQt\u7684\u6d88\u606f\u5904\u7406\u673a\u5236\uff08\u4e2a\u4eba\u7406\u89e3\uff09\uff0cQt\u7684\u6d88\u606f\u5206\u4e3aSLOT\u548cSIGNAL\uff0c\u5173\u8054SIGNAL\u548cSLOT\u79f0\u4e3aconnect\u3002SIGNAL\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u4f8b\u5982<\/p>\n<pre lang=\"python\">self.connect(self.testButton, SIGNAL(\"clicked()\"), self.test)<\/pre>\n<p>\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u5c31\u662f\u5728self.testButton\u8fd9\u4e2a\u6309\u94ae\u4ea7\u751fclicked\u7684SIGNAL\u65f6\uff0c\u8c03\u7528test\u8fd9\u4e2a\u51fd\u6570\u3002connect\u5c31\u662f\u5c06\u8fd9\u4e2a\u4fe1\u53f7\u548cSLOT\uff08test\uff09\u5173\u8054\u8d77\u6765\u3002<\/p>\n<p>P.S. \u5410\u69fd\uff1a\u53f3\u4eac\u6837\u4e00\u5144\u4f60\u7684blog\u6ca1\u6709trackback\u5417\uff1f\u8fd8\u662f\u4f60\u7684\u4e3b\u9898\u4e0d\u652f\u6301trackback\u94fe\u63a5\u2026\u2026\u6211\u672c\u6765\u60f3trackback\u4e00\u4e0b\u4f60\u7684\u539f\u59cb\u6587\u5462\u3002<\/p>\n<p>P.S.\u7684P.S. \u6211\u731c\u51fatrackback\u94fe\u63a5\u4e86\u2026\u2026<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p><a href=\"http:\/\/www.riverbankcomputing.co.uk\/static\/Docs\/PyQt4\/pyqt4ref.html\">http:\/\/www.riverbankcomputing.co.uk\/static\/Docs\/PyQt4\/pyqt4ref.html<\/a><\/p>\n<p><a href=\"http:\/\/diotavelli.net\/PyQtWiki\/Creating_GUI_Applications_with_PyQt_and_Qt_Designer\">http:\/\/diotavelli.net\/PyQtWiki\/Creating_GUI_Applications_with_PyQt_and_Qt_Designer<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyQtDemo.tar.gz \u5176\u5b9e\u662f\u4e13\u95e8\u4e3a\u53f3\u4eac\u6837\u4e00\u5144\u6240\u4f5c\uff0c\u641e\u4e86\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u7a97\u53e3\u3002\u95f2\u8bdd\u5c11\u8bf4\uff0c\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\u4e1c\u897f\u3002 \u5185\u5bb9\u7269\u5f88\u7b80\u5355\u3002 . \u251c\u2500\u2500 build.py \u251c\u2500\u2500 demowindow.py \u251c\u2500\u2500 main.pyw \u2514\u2500\u2500 ui \u2514\u2500\u2500 demowindow.ui \u9996\u5148\u9700\u8981\u8fd0\u884c.\/build.py\u6839\u636eui\u6587\u4ef6\u751f\u6210\u4e00\u4e2apy\u6587\u4ef6\uff0c\u7136\u540e.\/main.pyw\u5c31\u53ef\u4ee5\u8fd0\u884c\u4e86\u3002\u5185\u5bb9\u4e5f\u8d85\u5c11\uff0c\u9996\u5148\u662fmain.pyw\u7684\u5185\u5bb9\uff1a #!\/usr\/bin\/env python import sys from PyQt4.QtGui import QApplication from demowindow import DemoWindow from PyQt4.QtCore import Qt, SIGNAL if __name__ == &#8220;__main__&#8221;: app = QApplication(sys.argv); app.connect( &hellip; <a href=\"https:\/\/www.csslayer.info\/wordpress\/linux\/qt-linux\/easy-pyqt-demo\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[92],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-qt-linux","tag-qt-pyqt"],"_links":{"self":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/comments?post=168"}],"version-history":[{"count":11,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/168\/revisions\/180"}],"wp:attachment":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}