﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-杂家杂谈-随笔分类-Python</title><link>http://www.blogjava.net/glorywine/category/32060.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 16 Sep 2008 17:23:39 GMT</lastBuildDate><pubDate>Tue, 16 Sep 2008 17:23:39 GMT</pubDate><ttl>60</ttl><item><title>Django on Jython</title><link>http://www.blogjava.net/glorywine/archive/2008/09/17/229275.html</link><dc:creator>glorywine</dc:creator><author>glorywine</author><pubDate>Tue, 16 Sep 2008 16:41:00 GMT</pubDate><guid>http://www.blogjava.net/glorywine/archive/2008/09/17/229275.html</guid><wfw:comment>http://www.blogjava.net/glorywine/comments/229275.html</wfw:comment><comments>http://www.blogjava.net/glorywine/archive/2008/09/17/229275.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/glorywine/comments/commentRss/229275.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/glorywine/services/trackbacks/229275.html</trackback:ping><description><![CDATA[
		<font size="2" face="Courier New">   使用Django已经一段时间了，非常喜欢这样的状态，很少的配置文件和代码，就可以样一个原型跑起来。1.0已经发布一段时间了，有了很多的改变，改进，很吸引眼球。但是还有一个特性非常吸引我，Django可以运行在Jython上了，这就意味着，可以在Tomcat,Glassfish等服务器上跑Django了，EXCITING?<br /><br />   OK, Let's give a shot!</font>
		<font size="2" face="Courier New">
				<br />   1. of course, 先要现在Django</font>，<font size="2" face="Courier New">不过我还是推荐使用svn中的代码</font>。<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><font face="Courier New"><span style="color: rgb(0, 0, 0);">svn co http:</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">code.djangoproject.com/svn/django/trunk/ django-dev</span></font><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"></span></div>  <font size="2" face="Courier New"> 2. 接着就要下载Jython了，当前的stable版本是2.2.1，而我们需要的是2.5，该版本还是alpha阶段，所以，还是从svn中获取。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">svn co https:</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">jython.svn.sourceforge.net/svnroot/jython/trunk/jython/ jython-dev</span></div>   完成了Jython的检出，开始编译工作。需要指出的是，我们在这里使用ant来构建Jython，当然，也可以选择maven，看个人习惯了，ant和maven的具体安装方法大家可以去google，这里不再累述。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">cd jython</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">dev<br />ant</span></div>   稍等片刻，就可以完成编译工作了，生成的文件在jython-dev下的dist文件夹下。然后，我将dist下的文件全部拷贝到D:/jython下，我在这里使用的是windows，大家可以根据自己的平台进行设置。<br />   为了能在console里使用Jython，我们还需要将jython目录下的bin文件夹添加到path。这里大家也可以去google，不再多说。现在打开一个console，输入jython，是不是能看到非常熟悉的，python控制台呢？<br />   3. 好的，现在就需要安装Django了。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">cd django</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">dev<br />jython setup.py build<br />jython setup.py install</span></div>   安装完成后，就可以在jython/Lib/site-packages下看到Django了。为了查看是否安装成功，使用下面的方法。打开控制台：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">D:\</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">jython<br />Jython </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">.5a3</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> (trunk, </span><span style="color: rgb(0, 0, 0);">????</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">16</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2008</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">18</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">50</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">22</span><span style="color: rgb(0, 0, 0);">)<br />[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">.0_10</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">beta<br />Type </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">help</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">copyright</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">credits</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> or </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">license</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> more information.<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> django<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> django.get_version()<br />u</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">1.0-post-release-SVN-SVN-unknown</span><span style="color: rgb(0, 0, 0);">'</span></div>   如果看到了最后的输出就表示安装成功了。<br />   4. 当前Django on Jython只支持postgresql，所以，我们这里还需要下载一个Lib，Django-Jython.<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">svn co http:</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">django-jython.googlecode.com/svn/trunk/ <font color="#000000">django-jython<br />cd django-jython<br />jython setup.py build<br />jython setup.py install<br /></font></span></div>   这样就完成了django-jython的安装。<br />   5. 最后就是安装postgresql数据库了，大家可以根据官方的文档进行安装。<br /><br />   万事俱备，让我们开始真正的Django on Jython之旅吧。<br />   这里还需要说明一点，在完成Django的安装之后，在jython/bin下会有一个django-admin.py，这个是Django命令的工具，为了同CPython下的Django进行区别，我在这里将其重命名为django-admin-jy.py。<br />   好，切换到工作目录。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">django</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">admin</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">jy.py startproject djangonjython</span></div>   是不是有djangonjython这个目录出现呢？对了，这就是我们创建的工程。<br />   如果你是一个急性子，那么就让我们尝试一下，看看是不是能够运行呢？<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">cd djangonjython<br />jython manage.py runserver</span></div>   熟悉的页面是否出现呢？<br /></font><div align="center"><font size="2" face="Courier New"><img src="http://www.blogjava.net/images/blogjava_net/glorywine/runserver.PNG" alt="runserver.PNG" width="300" border="0" height="179" /></font><br /><div align="left">   <font size="2" face="Courier New">在观察一下djangonjython目录里的文件，哈，没有了.pyc，改成了.class，确实是jython在工作了。<br />   如果只是运行这么一个东西，肯定就没有什么价值了。OK，let's build an application。我们在这里创建一个简单的blog（老生常谈了:)）。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">jython manage.py startapp blog</span></div>   这样就完成了app的创建，当然在djangonjython下应该多了一个blog文件夹，里面还包含了一些文件。<br />   首先修改models.py，内容如下：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.db </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> models<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.contrib.auth.models </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> User<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Entry(models.Model):<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">    STATUS_CHOICES </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">        (0, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">live</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">        (</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">draft</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">        (</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">cancel</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    )<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    author </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> models.ForeignKey(User)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    title </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> models.CharField(max_length</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    slug </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> models.SlugField()<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    content </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> models.TextField()<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">    create_at </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> models.DateTimeField(auto_now_add</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">True)<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    status </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> models.IntegerField(default</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0, choices</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">STATUS_CHOICES)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__unicode__</span><span style="color: rgb(0, 0, 0);">(self):<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> self.title<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> get_absolute_url(self):<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">/blog/%s</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);"> (self.slug)<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Meta:<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        ordering </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">-create_at</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]</span></div>   这只是一个非常简单的原型，说明问题就行了。好，既然使用了Django 1.0那么就要有一些改变，现在的Django使用了newform-admin，那么就在blog目录下创建admin.py文件，内容如下：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.contrib </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> admin<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> djangonjython.blog.models </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> Entry<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> EntryAdmin(admin.ModelAdmin):<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">    list_display </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">title</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">author</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">create_at</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">status</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,)<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">    ordering </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">-create_at</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">    list_filter </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">author</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">status</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,)<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    prepopulated_fields </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">slug</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">: (</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">title</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,),}<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">admin.site.register(Entry, EntryAdmin)</span></div>   这样就将Entry注册到了admin site里，就可以在admin page里进行管理了。<br />   再进一步之前，先启动先前安装的postgresql，创建database blog。完成之后，就要对项目进行一些配置了。<br />   首先是djangonjython目录下的urls.py<br />   取消这几行的注释：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.contrib </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> admin<br />admin.autodiscover()<br /><br />(r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">^admin/(.*)</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, admin.site.root),</span></div>   为的是能够访问到admin page.<br />   接着是settings.py，<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">DATABASE_ENGINE </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">doj.backends.zxjdbc.postgresql</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">           </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">DATABASE_NAME </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">blog</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">             </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Or path to database file if using sqlite3.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">DATABASE_USER </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'yourname</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">             </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Not used with sqlite3.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">DATABASE_PASSWORD </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">yourpassword</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">         </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Not used with sqlite3.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">DATABASE_HOST </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">             </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Set to empty string for localhost. Not used with sqlite3.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">DATABASE_PORT </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">             </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Set to empty string for default. Not used with sqlite3.<br /><br /><font color="#000000">INSTALLED_APPS = (<br />    'django.contrib.auth',<br />    'django.contrib.contenttypes',<br />    'django.contrib.sessions',<br />    'django.contrib.sites',<br />    'django.contrib.admin',<br />    'djangonjython.blog',<br />    'doj',<br />)</font><br /></span></div><br />   对上面的代买进行一些说明：<br />   doj.backends.zxjdbc.postgresql是之前安装的django-jython提供的数据支持，当然，要使用这个，必须将doj这个库添加到PYTHONPATH中，所以在下面的INSTALLED_APPS里，加入了doj，而djangonjyton.blog就是之前创建的app。<br />   好，完成了这些，就可以开始数据库的创建了。熟悉的命令：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">jython manage.py syncdb</span></div>   按照上面的提示，一步一步的进行填写。完成之后，启动服务，是不是又是一个熟悉的画面？<br /></font><font size="2" face="Courier New">   填写之前要求的管理员用户名和密码，就进入到了admin page，我习惯称后台管理页面。</font><font size="2" face="Courier New"><br />   好了，在这里就可以添加一些entry了。完成了后台的管理，让我们添加一些前台的显示吧。</font><br /><font size="2" face="Courier New">   在这里，我打算使用django自带的generic views，使用起来非常的简单。为了同项目的urls.py区别开，在blog下创建一个新的urls.py，内容如下：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.conf.urls.defaults </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> djangonjython.blog.models </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> Entry<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">entry_dict </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">queryset</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">: Entry.objects.all(),<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">template_object_name</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">: </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">entry</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">urlpatterns </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> patterns(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">django.views.generic.list_detail</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    (r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">^blog/$</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">object_list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, entry_dict),<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    (r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">^blog/(?P&lt;slug&gt;.*)/$</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">object_detail</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, entry_dict),<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"></span></div>   然后就要修改项目目录下的urls.py了，在(r'^admin/(.*)', admin.site.root)之前添加：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">(r</span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">, include(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">djangonjython.blog.urls</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)),</span></div>   接着就要添加模板文件了，在blog目录下创建templates/blog这样的目录，然后再里面添加entry_list.html和entry_detail.html两个文件，内容分别如下：<br />entry_list.html<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">{% if entry_list %}<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">    {% for entry in entry_list %}<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">a </span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="/blog/{{ entry.slug }}"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">h3</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">{{ entry.title }}</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">h3</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"> Post at: {{ entry.create_at|date:"Y-b-d" }}</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">hr</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">    {% endfor %}<br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">{% endif %}</span></div>entry_detail.html<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">{% if entry %}<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">span </span><span style="color: rgb(255, 0, 0);">style</span><span style="color: rgb(0, 0, 255);">="color: green;"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">{{ entry.title }}</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">span</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">br</span><span style="color: rgb(0, 0, 255);">/&gt;&lt;</span><span style="color: rgb(128, 0, 0);">br</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">    post by: </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">{{ entry.author.username }}</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">br</span><span style="color: rgb(0, 0, 255);">/&gt;&lt;</span><span style="color: rgb(128, 0, 0);">br</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">style</span><span style="color: rgb(0, 0, 255);">="color: blue"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">{{ entry.content|safe }}</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">{% endif %}</span></div>   完成了这些之后，就要告诉django，怎么处理了，也就是要修改settings.py了：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> os<br />PROJECT_ROOT </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> os.path.abspath(</span><span style="color: rgb(128, 0, 128);">__file__</span><span style="color: rgb(0, 0, 0);">)<br />TEMPLATE_DIRS </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (<br />    os.path.join(PROJECT_ROOT, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">blog/templates</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">),<br />)</span></div>   添加前面两句，修改后面的TEMPLATE_DIRS，告诉django到哪里去找模板文件。<br />   重新启动服务器，输入http://localhost:8000/blog/，就可以看到一个简单的blog列表了，如果有的话。点击相应的标题，就会进入到具体的entry了。<br />   到这里位置，一个简单的blog就算是创建好了。我们的目的主要是要看他在其他的服务器上是否能够运行，好的，这里就要进行打包处理了。<br />   django-jython提供了一个war命令，会将我们的工程打包成war包，方便部署。不过django-jython有一个小小的bug。找到jython/Lib/site-packages/doj/manamgement/commands/war.py，在该文件的第228行，原来是：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> dest_relative_path[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">/</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:</span></div>   现在我们要将其改为：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> dest_relative_path[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> os.path.sep:</span></div>   因为在windows下得到的是'\\'而不是'/'，所以执行的时候会出错，可能开发者没有在windows下进行测试，所以出现了这样的情况。<br />   接着，我们需要链接postgresql的jdbc包，大家可以在这里下载<a href="http://jdbc.postgresql.org/download/postgresql-8.3-603.jdbc4.jar">postgres-jdbc</a>。下载后，将其放在项目目录下，开始打包：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">jython manage.py war </span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">include</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">java</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">libs</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">postgresql</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">8.3</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">603</span><span style="color: rgb(0, 0, 0);">.jdbc4.jar</span></div>   过一段时间，就可以在项目的上一级目录看到djangonjython.war<br />   到这里，java程序员就可以使用熟悉的java web server进行部署了！部署完成后，输入http://localhost:8080/djangonjython/blog/就可以进行访问了。<br />   这里给出一些在tomcat上运行的截图：<br /></font><div align="center"><font size="2" face="Courier New"><img src="http://www.blogjava.net/images/blogjava_net/glorywine/admin_page.PNG" alt="admin_page.PNG" width="519" border="0" height="489" /></font><br /></div><div align="center"><img src="http://www.blogjava.net/images/blogjava_net/glorywine/backend.PNG" alt="backend.PNG" width="769" border="0" height="574" /><br /></div><font size="2" face="Courier New">   怎么样，感觉还是不错吧。<br />   其实，Django on Jython还有很长的路要走，要支持更多的主流数据库，以及运行效率等问题。不过还是非常期待，希望能够快速的发展，毕竟Django的开发效率要远远高于Java。So let's look forward to it!<br /><br /><br /><br /><br /></font></div></div><img src ="http://www.blogjava.net/glorywine/aggbug/229275.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/glorywine/" target="_blank">glorywine</a> 2008-09-17 00:41 <a href="http://www.blogjava.net/glorywine/archive/2008/09/17/229275.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django Docs</title><link>http://www.blogjava.net/glorywine/archive/2008/08/24/223987.html</link><dc:creator>glorywine</dc:creator><author>glorywine</author><pubDate>Sun, 24 Aug 2008 07:17:00 GMT</pubDate><guid>http://www.blogjava.net/glorywine/archive/2008/08/24/223987.html</guid><wfw:comment>http://www.blogjava.net/glorywine/comments/223987.html</wfw:comment><comments>http://www.blogjava.net/glorywine/archive/2008/08/24/223987.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/glorywine/comments/commentRss/223987.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/glorywine/services/trackbacks/223987.html</trackback:ping><description><![CDATA[       做Django开发, 经常需要查看docs, 这就需要我到官网上去浏览. 如果遇到网络不好, 页面打开十分缓慢, 非常痛苦.<br />       其实, 在django的svn repository里, 有相应的docs, 不过是txt格式的. 如果浏览这个, 当然会非常的别扭.<br />       今天看到, django对docs进行了重构, 现在大家可以将这些文件下载到本地, 然后进行编译, 生成相应的文档, 浏览起来会非常方便, 舒服.<br /><br />       好, 我们简单的看一看该如何操作:<br />1. 从svn库中checkout:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">svn co http:</span><span style="color: rgb(0, 0, 0);">//</span><span style="color: rgb(0, 0, 0);">code.djangoproject.com</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">svn</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">django</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">trunk</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">docs</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);"> django_docs<br /></span></div>       这样, 在当前目录下就会有了django_docs了. 进去看一看, 恩, 重构后结构果然清晰多了,  过去是一股脑将所有的文件放在一起, 现在是分门别类, 不错.<br /><br />2. 安装Sphinx:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">easy_install Sphinx</span></div>        当然前提是python中已经安装了easy_install, 如果没有, 大家google一下, 非常容易. <br />        熟悉python的朋友应该知道, Sphinx是非常好的一个工具, 可以将简单文本格式转换为html格式, 当然, 文本内容需要按照Sphinx的语法进行编写. Python现在的在线documents就是使用Sphinx进行编译的.感兴趣的朋友可以到<a href="http://sphinx.pocoo.org/">Sphinx</a>了解更多内容.<br /><br />3. 编译:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">make html</span></div>        这里make提供了很多种格式, 因为我们只需要html, 所以加上这个选项就行了.<br />        到这里, 就完成了django_docs的编译工作, 在django_docs/_build/html下就是编译的结果. 重构后的文档的css也有了新的变化, 现在的非常像django_book的样式.<br />        OK, 以后我们就可以在本地进行文档的阅读了, 相信会给开发带来事半功倍的效果.<br />        <br />        这里我附上一份我编译好的, 供大家下载. <a href="/Files/glorywine/html.zip">django_docs<br /></a><img src ="http://www.blogjava.net/glorywine/aggbug/223987.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/glorywine/" target="_blank">glorywine</a> 2008-08-24 15:17 <a href="http://www.blogjava.net/glorywine/archive/2008/08/24/223987.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PyQt4 - (4) 布局管理</title><link>http://www.blogjava.net/glorywine/archive/2008/07/30/217842.html</link><dc:creator>glorywine</dc:creator><author>glorywine</author><pubDate>Wed, 30 Jul 2008 07:39:00 GMT</pubDate><guid>http://www.blogjava.net/glorywine/archive/2008/07/30/217842.html</guid><wfw:comment>http://www.blogjava.net/glorywine/comments/217842.html</wfw:comment><comments>http://www.blogjava.net/glorywine/archive/2008/07/30/217842.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/glorywine/comments/commentRss/217842.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/glorywine/services/trackbacks/217842.html</trackback:ping><description><![CDATA[
		<b>
				<font color="#0000ff" size="5">PyQt4中的布局管理</font>
		</b>
		<br />
		<br />
		<font size="2">布局是GUI程序开发中非常重要的一个环节, 而布局管理就是要规划如何在窗口中放置需要的部件. PyQt4中就有两种方法来完成布局任务,一个是绝对位置(absolute positioning), 另一个就是使用布局类(layout classes).</font>
		<br />
		<br />
		<b>
				<font color="#0000ff" size="5">绝对位置(absolute positioning)</font>
		</b>
		<br />
		<font size="2">这种方法就需要程序员在程序中指定每个部件的坐标位置和大小. 在这里有几个注意事项.<br /></font>
		<ul>
				<li>
						<font size="2">指定了坐标和大小的部件是不能够随着窗口大小的变化而变化的.</font>
				</li>
				<li>
						<font size="2">程序在不同的操作系统平台上也许会有所变化.</font>
				</li>
				<li>
						<font size="2">改变字体可能会引起布局的混乱.</font>
				</li>
				<li>
						<font size="2">如果需要改变当前的布局, 就需要重新编码, 这意味着非常大的工作量.</font>
				</li>
		</ul>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> absolute.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Absolute(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Communication</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Could\</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">t</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">, self)</span><span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(128, 0, 0);"></span><span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">15</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">care</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">35</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">40</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">less</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">55</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">65</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">And</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">115</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">65</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">then</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">135</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">45</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">you</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">115</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">25</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">kissed</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">145</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">        label </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">me</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">        label.move(</span><span style="color: rgb(0, 0, 0);">215</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">qb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Absolute()<br /></span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);">qb.show()<br /></span><span style="color: rgb(0, 128, 128);">43</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div>
		<br />
		<font size="2">在这里, 就是简单的调用move()方法来指定部件的放置坐标. 坐标的顶点就在窗口的左上角, x由左向右不断增大, y由上到下不断增大.</font>
		<br />
		<img src="http://www.blogjava.net/images/blogjava_net/glorywine/Communication.png" alt="Communication.png" border="0" width="252" height="173" />
		<br />
		<font color="#a9a9a9" size="2">Figure: absolute positioning</font>
		<br />
		<br />
		<b>
				<font color="#0000ff" size="5">Box Layout</font>
		</b>
		<br />
		<br />
		<font size="2">使用布局类进行布局管理是一件非常轻松的事情.  最基本的布局类就是QHboxLayout和VHBoxLayout. 他们将部件线性的水平或垂直排列. <br />这里假设我们要将两个按钮放在窗口的右下方, 要实现这样的布局, 使用一个QHBoxLayout和一个QVBoxLayout, 而其他的空间, 通过添加stretch factor来实现.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> boxlayout.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> BoxLayout (QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">boxlayout</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        ok </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">OK</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        cancel </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Cancel</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        hbox </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QHBoxLayout()<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        hbox.addStretch(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        hbox.addWidget(ok)<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        hbox.addWidget(cancel)<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">        vbox </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QVBoxLayout()<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">        vbox.addStretch(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        vbox.addLayout(hbox)<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">        self.setLayout(vbox)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">qb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> BoxLayout()<br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">qb.show()<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">ok </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">OK</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">)<br />cancel </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">Cancel</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">)</span>
		</div>
		<font size="2">创建两个按钮</font>
		<br />
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">hbox </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QHBoxLayout()<br />hbox.addStretch(</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">)<br />hbox.addWidget(ok)<br />hbox.addWidget(cancel)</span>
		</div>
		<font size="2">创建一个水平的box layout. 然后添加stretch factor和两个按钮.<br /></font>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<font size="2">
						<span style="color: rgb(0, 0, 0);">vbox </span>
						<span style="color: rgb(0, 0, 0);">=</span>
				</font>
				<span style="color: rgb(0, 0, 0);">
						<font size="2"> QtGui.QVBoxLayout()</font>
						<br />vbox.addStretch(</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">)<br />vbox.addLayout(hbox)</span>
		</div>
		<font size="2">创建最后的布局类, 将水平的布局类放入到垂直的布局类当中.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">self.setLayout(vbox)</span>
		</div>
		<font size="2">最后, 将窗口的布局设定为垂直布局.</font>
		<br />
		<img src="http://www.blogjava.net/images/blogjava_net/glorywine/boxlayout-1.png" alt="boxlayout-1.png" border="0" width="302" height="172" />
		<br />
		<font color="#a9a9a9" size="2">Figure: box layout</font>
		<br />
		<br />
		<b>
				<font color="#0000ff" size="5">QGridLayout</font>
		</b>
		<br />
		<br />
		<font size="2">最常用的布局类应当是QGridLayout, 他将窗口分成不同的行和列.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> gridlayout.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> GridLayout(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">grid layout</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        names </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Cls</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Bck</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Close</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">7</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">8</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">/</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">                 </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">4</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">5</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">6</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">*</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">2</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">3</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">                 </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">.</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        grid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QGridLayout()<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 0<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">        pos </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">            (0, 0), (0, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">), (0, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">), (0, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">            (</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, 0), (</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">            (</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, 0), (</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">            (</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">, 0), (</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">),<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">            (</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">, 0), (</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">            ]<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> names:<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">            button </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(i)<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> j </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:<br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">                grid.addWidget(QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">), 0, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">                grid.addWidget(button, pos[j][0], pos[j][</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">            j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> j </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">        self.setLayout(grid)<br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">qb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> GridLayout()<br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">qb.show()<br /></span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div>
		<font size="2">在这个例子中, 创建了很多按钮, 用来填充相应的grid.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">grid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QGridLayout()</span>
		</div>
		<font size="2">这一句就创建了grid layout.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> j </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">2</span>
				<span style="color: rgb(0, 0, 0);">:<br />    grid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> addWidget(QtGui.QLabel(</span>
				<span style="color: rgb(128, 0, 0);">''</span>
				<span style="color: rgb(0, 0, 0);">), 0, </span>
				<span style="color: rgb(0, 0, 0);">2</span>
				<span style="color: rgb(0, 0, 0);">)<br /></span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);">:<br />    grid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> addWidget(button, pos[j][0], pos[j][i])</span>
		</div>
		<font size="2">把部件放入grid, 调用addWidget()方法就行了, 参数就是要放入的部件以及相应的行数和列数.</font>
		<br />
		<img src="http://www.blogjava.net/images/blogjava_net/glorywine/grid%20layout.png" alt="grid layout.png" border="0" width="284" height="211" />
		<br />
		<font color="#a9a9a9" size="2">Figure: grid layout</font>
		<br />
		<br />
		<font size="2">需要说明一下, 部件也可以多行和多列. 下面的列子就演示了之一点.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> gridlayout2.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> GridLayout2(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">grid layout2</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        title </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Title</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        author </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Author</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        review </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLabel(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Review</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        titleEdit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLineEdit()<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        authorEdit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QLineEdit()<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        reviewEdit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QTextEdit()<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">        grid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QGridLayout()<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">        grid.setSpacing(</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">        grid.addWidget(title, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, 0)<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">        grid.addWidget(titleEdit, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">        grid.addWidget(author, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, 0)<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">        grid.addWidget(authorEdit, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">        grid.addWidget(review, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">, 0)<br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">        grid.addWidget(reviewEdit, </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">        self.setLayout(grid)<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">350</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">qb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> GridLayout2()<br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);">qb.show()<br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())<br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);"></span></div>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">grid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QGridLayout()<br />grid.setSpacing(</span>
				<span style="color: rgb(0, 0, 0);">10</span>
				<span style="color: rgb(0, 0, 0);">)</span>
		</div>
		<font size="2">创建布局类, 并设定部件之间的距离为10个像素.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">grid.addWidget(reviewEdit, </span>
				<span style="color: rgb(0, 0, 0);">3</span>
				<span style="color: rgb(0, 0, 0);">, </span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">, </span>
				<span style="color: rgb(0, 0, 0);">5</span>
				<span style="color: rgb(0, 0, 0);">, ,</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">)</span>
		</div>
		<font size="2">给布局中添加部件时, 可以指定占据的行数和占据的列数. 这个例子中, 我们让reviewEdit占据了5行.</font>
		<br />
		<img src="http://www.blogjava.net/images/blogjava_net/glorywine/grid%20layout2.png" alt="grid layout2.png" border="0" width="351" height="323" />
		<br />
		<font color="#a9a9a9" size="2">Figure: grid layout 2</font>
		<br />
		<br />
		<br />
		<br />
		<br />
<img src ="http://www.blogjava.net/glorywine/aggbug/217842.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/glorywine/" target="_blank">glorywine</a> 2008-07-30 15:39 <a href="http://www.blogjava.net/glorywine/archive/2008/07/30/217842.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PyQt4 - (3) 菜单和工具栏</title><link>http://www.blogjava.net/glorywine/archive/2008/07/26/206731.html</link><dc:creator>glorywine</dc:creator><author>glorywine</author><pubDate>Sat, 26 Jul 2008 04:08:00 GMT</pubDate><guid>http://www.blogjava.net/glorywine/archive/2008/07/26/206731.html</guid><wfw:comment>http://www.blogjava.net/glorywine/comments/206731.html</wfw:comment><comments>http://www.blogjava.net/glorywine/archive/2008/07/26/206731.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.blogjava.net/glorywine/comments/commentRss/206731.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/glorywine/services/trackbacks/206731.html</trackback:ping><description><![CDATA[
		<font color="#0000ff" size="5">
				<strong>主窗口</strong>
		</font>
		<br />
		<font size="2">QMainWindow类给我们提供了一个程序主窗口，使得我们能够创建一个标准的程序框架，包括了状态栏，工具栏和菜单栏。<br /></font>
		<br />
		<b>
				<font color="#0000ff" size="5">状态栏(Statusbar)</font>
		</b>
		<br />
		<font size="2">状态栏就是一种用来显示状态信息的小部件.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> statusbar.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MainWindow(QtGui.QMainWindow):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QMainWindow.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">statusbar</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        self.statusBar().showMessage(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Ready</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">main </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> MainWindow()<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">main.show()<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"></span></div>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">self.statusBar().showMessage(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">Ready</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">)</span>
		</div>
		<font size="2">这里需要注意的就是这一句,通过调用QMainWindow类的statusBar()方法,得到statusbar的句柄,然后调用showMessage()显示信息在statusbar上.非常容易吧!</font>
		<br />
		<br />
		<b>
				<font color="#0000ff" size="5">菜单栏(Menubar)</font>
		</b>
		<br />
		<font size="2">菜单是应用程序中最常见的一种组件.通常每个菜单都是完成不同的任务.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> menubar.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui, QtCore<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MainWindow(QtGui.QMainWindow):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QMainWindow.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">menubar</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        exit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QAction(QtGui.QIcon(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">icons/web.png</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        exit.setShortcut(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Ctrl+Q</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        exit.setStatusTip(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit application</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        self.connect(exit, QtCore.SIGNAL(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">triggered()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), QtCore.SLOT(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">close()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        self.statusBar()<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">        menubar </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.menuBar()<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">        file </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> menubar.addMenu(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">&amp;File</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        file.addAction(exit)<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">main </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> MainWindow()<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">main.show()<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);"></span></div>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">menubar </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> self.menuBar()<br />file </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> menubar.addMenu(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">&amp;File</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">)<br />file.addAction(exit)</span>
		</div>
		<font size="2">首先调用self.menuBar()创建菜单栏,接着调用addMenu()添加一个菜单,最后给菜单添加一个动作,就是这里的exit.</font>
		<br />
		<br />
		<font size="5">
				<b>
						<font color="#0000ff">工具栏(Toolbar)</font>
				</b>
		</font>
		<br />
		<font size="2">菜单可以将不同的命令按组分类,然而,有些常用的命令就可以通过工具栏来放置,这样更便于使用.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> toolbar.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui, QtCore<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MainWindow(QtGui.QMainWindow):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QMainWindow.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">toolbar</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        self.exit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QAction(QtGui.QIcon(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">icons/web.png</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        self.exit.setShortcut(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Ctrl+Q</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        self.connect(self.exit, QtCore.SIGNAL(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">triggered()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), QtCore.SLOT(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">close()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        self.toolbar </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.addToolBar(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        self.toolbar.addAction(self.exit)<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">main </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> MainWindow()<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">main.show()<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">self.exit </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QAction(QtGui.QIcon(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">icons/web.png</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">), </span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">Exit</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">, self)<br />self.exit.setShortcut(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">Ctrl+Q</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">)</span>
		</div>
		<font size="2">GUI程序是通过命令来控制的, 而这些命令可以由菜单,上下文菜单,工具栏或者快捷键来发出.  PyQt将命令抽象为actions. 一个action对象可以有text, 图标, 快捷键, 状态栏描述, "What's This?"描述和tooltip. 上面的例子,就定义了icon, tooltip和shortcut.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">self.connect(self.exit, QtCore.SIGNAL(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">triggered()</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">), QtCore.SLOT(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">close()</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">))</span>
		</div>
		<font size="2">这一句,将action的triggered()信号和预先定义的close()槽结合到一起.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">self.toolbar </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> self.addToolBar(</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(128, 0, 0);">Exit</span>
				<span style="color: rgb(128, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">)<br />self.toolbar.addAction(self.exit)</span>
		</div>
		<font size="2">然后就将定义好的action放入toolbar就可以了.</font>
		<br />
		<img src="http://www.blogjava.net/images/blogjava_net/glorywine/toolbar.png" alt="toolbar.png" border="0" height="170" width="250" />
		<br />
		<font color="#a9a9a9" size="2">Figure: toolbar<br /></font>
		<b>
				<font color="#0000ff" size="5">
						<br />一个完整的程序</font>
		</b>
		<br />
		<font size="2">最后这个部分,将menubar, toolbar和statusbar放在一起.</font>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> mainwindow.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui, QtCore<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MainWindow(QtGui.QMainWindow):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QMainWindow.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">350</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">mainwindow</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        textEdit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QTextEdit()<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        self.setCentralWidget(textEdit)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        exit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QAction(QtGui.QIcon(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">icons/web.png</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        exit.setShortcut(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Ctrl+Q</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        exit.setStatusTip(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit application</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">        self.connect(exit, QtCore.SIGNAL(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">triggered()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), QtCore.SLOT(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">close()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">        self.statusBar()<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">        menubar </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.menuBar()<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">        file </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> menubar.addMenu(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">&amp;File</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">        file.addAction(exit)<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">        toolbar </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.addToolBar(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Exit</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">        toolbar.addAction(exit)<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">main </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> MainWindow()<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">main.show()<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);"></span></div>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">textEdit </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> QtGui.QTextEdit()<br />self.setCentralWidget(textEdit)</span>
		</div>
		<font size="2">这里创建了一个edit部件,并将其设置为QMainWindow的中央部件.中央部件就是说要占据初菜单,工具栏,状态栏之外的所有空间.</font>
		<br />
		<br />
		<br />
		<br />
		<br />
<img src ="http://www.blogjava.net/glorywine/aggbug/206731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/glorywine/" target="_blank">glorywine</a> 2008-07-26 12:08 <a href="http://www.blogjava.net/glorywine/archive/2008/07/26/206731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PyQt4 - (2) 第一印象</title><link>http://www.blogjava.net/glorywine/archive/2008/06/21/206729.html</link><dc:creator>glorywine</dc:creator><author>glorywine</author><pubDate>Sat, 21 Jun 2008 15:58:00 GMT</pubDate><guid>http://www.blogjava.net/glorywine/archive/2008/06/21/206729.html</guid><wfw:comment>http://www.blogjava.net/glorywine/comments/206729.html</wfw:comment><comments>http://www.blogjava.net/glorywine/archive/2008/06/21/206729.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/glorywine/comments/commentRss/206729.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/glorywine/services/trackbacks/206729.html</trackback:ping><description><![CDATA[   <font size="2">在这个部分，我们开始学习一些PyQt4的基本功能，解释会比较详细，就像教孩子说话一样。<br /></font><br /><font style="background-color: rgb(0, 0, 255);" size="5"><font color="#0000ff"><font style="background-color: rgb(255, 255, 255);"><strong>最简单的example</strong></font><br /></font></font><br /><font size="2">    下面的示例会非常的简单，仅仅显示一个小窗口，但是将来我们可以通过这个小窗口做很多事情。我们可以对它进行缩放，最大化，最小化。而这些就需要比较多的代码了。但是开发人员已经帮我们完成了这些功能，因为这些功能在大多场合都会使用到，所以没有必要让我们重复。PyQt是一个高级别的工具库，如果使用级别相对较低的库开发下面的功能，就非常可能需要更多的代码了。<br /></font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 128, 128);"> 1</span><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span><span style="color: rgb(0, 128, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> simple.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span><span style="color: rgb(0, 128, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="color: rgb(0, 128, 128);"> 8</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);"> 9</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />widget </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QWidget()<br /></span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />widget.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />widget.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">simple</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />widget.show()<br /></span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />sys.exit(app.exec_())</span></div><br />    <font size="2">下面对代码进行分析<br /></font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui</span></div><font size="2">     这里，我们导入了必要的模块，PyQt4的基本模块都在QtGui中。<br /></font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)</span></div><font size="2">     每一个PyQt4程序都必须创建一个application对象，application类位于QtGui模块中。sys.argv传入命令行参数。Python脚本可以通过shell运行，这样我们就可以对程序的启动进行控制。<br /></font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 0);">widget </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QWidget()</span></div><font size="2">     QWidget是PyQt4中所有用户接口对象的基类，我们使用QWidget默认的构造方法来创建QWidget对象，默认的构造方法没有指定父控件，如果一个widget没有指定父控件，那么我们就称他为一个window。<br /></font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 0);">widget.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)</span></div>    <font size="2">resize()方法调整widget的大小，在这里我们设定宽度为250px，高度为150px。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 0);">widget.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">simple</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)</span></div>   
		<font size="2">setWindowTitle()方法设置了窗口的title，title在titlebar中显示。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 0);">widget.show()</span></div>   
		<font size="2">调用show()方法将窗口显示出来。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div>   
		<font size="2">最后，就会进入application的事件循环。事件处理就从这里开始，循环不断的从窗口接受需要处理的事件，然后将其分发给相应的时间处理方法。通过调用exit()方法或者销毁widget来终止事件循环，调用sys.exit()方法确保了程序可以明确的退出，也就是程序退出后会告知系统。<br />也许你会问，为什么exec_()方法最后会有一个下划线？问得非常好，这完全是因为exec是python的一个关键字，所以我们不得不使用exec_来代替。<br /></font><img src="http://www.blogjava.net/images/blogjava_net/glorywine/Icon.png" alt="Icon.png" border="0" height="174" width="251" /><br /><font color="#d3d3d3" size="1">图1：simple</font><br /><br /><font color="#0000ff"><b><font size="5">添加图标</font></b></font><br /><br />  
		<font size="2">应用程序的图标一般就是一个在程序标题栏左上角显示的小图片，下面的例子我们展示如何使用PyQt4来显示图标，同时引入一些新的方法。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> icon.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Icon(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setGeometry(</span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Icon</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        self.setWindowIcon(QtGui.QIcon(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">icons/web.png</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">icon </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Icon()<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">icon.show()<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div><font size="2">    程序使用的是类似C语言的过程式风格，Python同时支持过程式和面向对象式的编程风格。需要注意的是，在使用PyQt4时，意味着面向对象编程。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Icon(QtGui.QWidget):<br />    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br />        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span></div><font size="2">    面向对象编程中最重要的三个要素就是类(classes),数据(data)和方法(methods)。首先我们建立一个类叫做Icon，他从QtGui.QWidget继承而来。这就意味着，我们必须要两次调用构造方法，一个是Icon类的，另一个是父类的。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">self.setGeometry(</span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br />self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Icon</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)       self.setWindowIcon(QtGui.QIcon(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">icons/web.png</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))</span></div><font size="2">    上面三个方法都是从父类继承而来，即QtGui.QWidget。setGeometry()在这里做两件事情，一是指定了窗口在屏幕中的位置，二就是指定了窗口的大小。前面两个参数是x和y坐标，后面两个是窗口的width和height。最后一个方法给窗口指定了一个图标，我们需要首先创建一个QIcon对象，而QIcon接受一个参数指明图标的路径。</font><br /><img src="http://www.blogjava.net/images/blogjava_net/glorywine/simple.png" alt="simple.png" border="0" height="174" width="251" /><br /><font color="#d3d3d3" size="1">图2：Icon</font><br /><br /><font color="#0000ff"><b><font size="5">展示信息提示(tooltip)</font></b></font><br /><font size="2"><br />    我们可以使用PyQt4给任何的部件添加信息提示功能。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> tooltip.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtCore<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Tooltip(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setGeometry(</span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Tooltip</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        self.setToolTip(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">This is a &lt;b&gt;QWidget&lt;/b&gt; widget</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        QtGui.QToolTip.setFont(QtGui.QFont(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">OldEnglish</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">tooltip </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Tooltip()<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">tooltip.show()<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">app.exec_()</span></div><font size="2">    在这个例子中，我们给QWidget提供了信息提示。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">self.setToolTip(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">This is a &lt;b&gt;QWidget&lt;/b&gt; widget</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span></div><font size="2">    使用setTooltip()来创建一个tooltip，值得高兴的是，我们可以使用富文本格式(rich text formatting)。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">QtGui.QToolTip.setFont(QtGui.QFont(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Courier New</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">))</span></div><font size="2">    由于QToolTip默认的字体看起来很糟糕，所以我们对之进行一些改进。</font><br /><img src="http://www.blogjava.net/images/blogjava_net/glorywine/Untitled.png" alt="Untitled.png" border="0" height="178" width="255" /><br /><font color="#d3d3d3" size="1">图3：Tooltip<br /></font><br /><b><font color="#0000ff" size="5">关闭窗口</font></b><br /><br /><font size="2">    之前的例子中，我们关闭程序都是使用标题栏上的X按钮进行关闭的。下面的例子里，我们使用编程的方式来关闭窗口。而在这里，我们就要开始了解一些Qt中非常重要的概念了，信号(signals)和槽(slots)。<br />    下面一行是QPushButton的构造方法。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">QPushButton(string text, QWidget parent </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> None)</span></div><font size="2">    参数text是在按钮上显示的文本，parent是按钮的父容器。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> quitbutton.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui, QtCore<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> QuitButton(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setGeometry(</span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Icon</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        quit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Close</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        quit.setGeometry(</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">35</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        self.connect(quit, QtCore.SIGNAL(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">clicked()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), \<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">                     QtGui.qApp, QtCore.SLOT(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">quit()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">qb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QuitButton()<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">qb.show()<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">quit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QPushButton(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Close</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, self)<br />quit.setGeometry(</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">35</span><span style="color: rgb(0, 0, 0);">)</span></div><font size="2">     在QWidget上我们创建了一个按钮，就像在屏幕中创建一个QWidget一样。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">self.connect(quit, QtCore.SIGNAL(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">clicked()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">), \<br />             QtGui.qApp, QtCore.SLOT(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">quit()</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))</span></div><font size="2">     正如前面所说，PyQt4的事件处理时间里在信号(signals)和槽(slots)机制上的。如果我们点击了按钮，就发射了信号clicked()方法。槽可以是PyQt槽，也可以是任何python中可调用的方法。通过使用QtCore.QObject.connect()方法将信号和槽链接起来。在我们的例子中，使用的槽是PyQt中预先定义的quit()槽。就好象sender和receiver一样，在两个对象之间进行交流。在上面的例子里，sender就是按钮，receiver就是application对象。</font><br /><img src="http://www.blogjava.net/images/blogjava_net/glorywine/Picture%201.png" alt="Picture 1.png" border="0" height="171" width="250" /><br /><font color="#d3d3d3" size="1">图4：quit button</font><br /><font color="#0000ff"><b><font size="5"><br />对话框</font></b></font><br /><br /><font size="2">    默认情况下，如果我们点击了标题栏的X按钮，就会将窗口关闭，而有些时候，需要修改这样的行为。比如说在文本编辑器中，如个要关闭一个正在修改的文件，就会弹出一个对话框来确认。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> messagebox.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MessageBox(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setGeometry(</span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">300</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">message box</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> closeEvent(self, event):<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">        reply </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QMessageBox.question(self, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Message</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, \<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">                                           </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Are you sure to quit?</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,\<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">                                           QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> reply </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> QtGui.QMessageBox.Yes:<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">            event.accept()<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">            event.ignore()<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">            <br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">mb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> MessageBox()<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">mb.show()<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div><font size="2">    如果要关闭QWidget，QCloseEvent时间就会产生。要修改QWidget的默认行为，就需要重载父类中的closeEvent()方法。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">reply </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QMessageBox.question(self, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Message</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, \<br />                                   </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Are you sure to quit?</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,\<br />                                           QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)</span></div><font size="2">    在这里，显示了两个按钮的对话框，Yes和No。第一个string参数出现在标题栏上，第二个string参数显示在对话框中。在reply中保存了返回值。</font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> reply </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> QtGui.QMessageBox.Yes:<br />    event.accept()<br /></span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />    event.ignore()</span></div><font size="2">    对返回值进行比较，如果点击了Yes按钮，就接受事件，关闭窗口，相反就忽略事件。</font><br /><img src="http://www.blogjava.net/images/blogjava_net/glorywine/Picture%202.png" alt="Picture 2.png" border="0" height="164" width="275" /><br /><font color="#d3d3d3" size="1">图5：message box</font><br /><br /><font color="#0000ff"><b><font size="5">窗口居中</font></b></font><br /><br /><font size="2">    下面的代码展示了如何将创建的窗口在屏幕中居中。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> center.py</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> PyQt4 </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> QtGui<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Center(QtGui.QWidget):<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        QtGui.QWidget.</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, parent)<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        self.setWindowTitle(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">center</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        self.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        self.center()<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> center(self):<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        screen </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QDesktopWidget().screenGeometry()<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        size </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.geometry()<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        self.move((screen.width()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">size.width())</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, (screen.height()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">size.height())</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">            <br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">app </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QApplication(sys.argv)<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Center()<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">c.show()<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">sys.exit(app.exec_())</span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">self.resize(</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">)</span></div><font size="2">    将窗口大小调整为宽250px，高150px。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">screen </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> QtGui.QDesktopWidget().screenGeometry()</span></div><font size="2">    获得显示器的分辨率。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">size </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.geometry()</span></div><font size="2">    获得QWidget的大小。</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">self.move((screen.width()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">size.width())</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, (screen.height()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">size.height())</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)</span></div><font size="2">    将窗口移动到屏幕中央。</font><br /><img src ="http://www.blogjava.net/glorywine/aggbug/206729.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/glorywine/" target="_blank">glorywine</a> 2008-06-21 23:58 <a href="http://www.blogjava.net/glorywine/archive/2008/06/21/206729.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PyQt4 - (1) PyQt4简介</title><link>http://www.blogjava.net/glorywine/archive/2008/06/08/206726.html</link><dc:creator>glorywine</dc:creator><author>glorywine</author><pubDate>Sun, 08 Jun 2008 15:06:00 GMT</pubDate><guid>http://www.blogjava.net/glorywine/archive/2008/06/08/206726.html</guid><wfw:comment>http://www.blogjava.net/glorywine/comments/206726.html</wfw:comment><comments>http://www.blogjava.net/glorywine/archive/2008/06/08/206726.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/glorywine/comments/commentRss/206726.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/glorywine/services/trackbacks/206726.html</trackback:ping><description><![CDATA[
		<p style="FONT-SIZE: 18pt; COLOR: #3366ff">
				<strong style="FONT-SIZE: 18pt">关于这个教程<br /></strong>
				<font style="FONT-SIZE: 10pt; COLOR: #000000" size="3">
						<br />        这是一个PyQt4的简单教程，目的就是为了让大家对PyQt4有个初步的了解。</font>
		</p>
		<br />
		<span style="FONT-SIZE: 18pt">
				<strong style="COLOR: #3366ff">关于PyQt</strong>
		</span>
		<br />
		<span style="FONT-SIZE: 10pt">         PyQt是一个GUI库，是将Python和Qt结合在一起的产物。而Qt是目前比较成功和强大的GUI库。你可以通过访问<a href="http://www.riverbankcomputing.co.uk">www.riverbankcomputing.co.uk</a>了解更多关于PyQt的消息，目前主要的开发人员是 <strong>Phil Thompson</strong>.<br />         PyQt通过一组python模块来实现的，其中包括了超过300个类，尽6000个函数和方法。非常好的是，这个库是平台独立的，可以运行于当前大多操作系统中，包括Unix，Window和Mac。PyQt使用的是双许可证，开发人员可以在GPL和商业许可之间进行选择。过去，GPL仅适用于Unix，从PyQt4开始，GPL对所有的平台开放。<br />由于PyQt4包含了太多的类，所以开发人员将其分为不同的模块进行管理。<br /><img alt="" src="http://www.blogjava.net/images/blogjava_net/glorywine/modules.jpg" border="0" /><br /><span style="COLOR: #c0c0c0"><span style="FONT-SIZE: 10pt">图1：PyQt4模块</span></span></span>
		<br />
		<br />
		<span style="FONT-SIZE: 10pt">
				<span style="FONT-SIZE: 10pt">
						<strong>         QtCore</strong>主要负责一些非GUI的功能，包括时间(time)，文件(files)和文件夹(directories)，各种数据类型(data types)，流(stream)，urls，mime类型，线程(threads)和过程(processes)。<strong>QtGui</strong>模块负责图像组件和相关的类，包括常见的按钮(buttons)，窗口(windows)，状态栏(status bars)，工具栏(toolbars)，滑动条(sliders)，位图(bitmaps)，颜色(colors)和字体(fonts)等。<strong>QtNetwork</strong>则负责网络功能，通过该模块，可以完成具有TCP/IP和UDP交互功能的C/S应用，该模块使得网络开发非常的简单便携。<strong>QtXml</strong>包含了同xml文件交互的能力，模块实现了同SAX和DOM交互的APIs。<strong>QtSvg</strong>提供了显示SVG文件内容的方法。Scalabe Vector Graphics(SVG)是使用xml文件来描述二维图像的一种语言。<strong>QtOpenGL</strong>通过使用OpenGL来呈现3D和2D图像，该模块将Qt GUI库和OpenGL库无缝的结合在一起。<strong>QtSql</strong>提供了访问数据库的方法。<br /></span>
				<br />
				<br />
				<span style="FONT-SIZE: 18pt">
						<strong style="COLOR: #00ccff">
								<span style="COLOR: #3366ff">
										<span style="FONT-SIZE: 18pt">
												<strong>Python</strong>
										</span>
								</span>
								<br />
						</strong>
				</span>
				<br />
				<img alt="" src="http://www.blogjava.net/images/blogjava_net/glorywine/pythonlogo.png" border="0" />Python是单前非常成功的脚本语言，最开始是由<strong>Guido van Rossum</strong>进行开发的，在1991年放出了第一个版本。Python的灵感来源于ABC和Haskell。Python是一个高级的，跨平台的解释性语言。很多人喜欢将其称为动态语言。它非常容易学习，最明显的特征是，它不需要分号和括号，而是使用<strong>缩进</strong>。目前最稳定的版本应该是2.5.2，并且有着来自全球的志愿者进行维护。2007年，Python被评为年度语言。<br /><br /><br /><span style="FONT-SIZE: 18pt"><strong style="COLOR: #3366ff">Python小提示</strong></span><br />        如果想使用Python进行GUI开发，目前主要有三个选择，PyGTK，wxPython和PyQt，可以根据开发环境进行选择。还有一个叫做TkInter的，使用的不是非常广泛。</span>
<img src ="http://www.blogjava.net/glorywine/aggbug/206726.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/glorywine/" target="_blank">glorywine</a> 2008-06-08 23:06 <a href="http://www.blogjava.net/glorywine/archive/2008/06/08/206726.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>