test Blog
Happy living

使用PyGTK和Glade创建用户界面

老鼠 posted @ 2009年4月22日 00:31 in Python with tags pygtk glade 界面 , 4959 阅读

原文:
(奇怪, 我把类型选为翻译, 咋看上去啥也没有哪!!)
   在花了一些时间使用Tkinter创建用户界面后发现它很简单, 但是在如何把界面和程序代码连接起来上费了些周折. 我决定试一试使用另外的工具包来创建界面. 经过一段时间的搜寻后我决定使用PyGTK和Glade.
  使我下决心使用这两个技术的原因是:一. 它们是跨平台的 二. Glade能够我把代码和界面分离开来的愿望.
  如果你之前从没听说过Glade, 它就是一个在GTK+和GNOME下的用户界面生成器. 它生成描述用户界面的xml文件.
   pyGTK的网站上这样描述PyGTK:
PyGTK为GTK+库提供了一个在Python程序中便于使用的封装, 它用来负责一些繁杂的细节诸如:管理内存和类型转换. 当把它与PyORBit和gnome-python结合起来时,它可以用来写具有完备功能的Gnome应用程序.
  现在我们从这里开始. 我正在我刚安装的Debian系统上写这些. 如果你也使用Debian或是一个基于Debian的发行版, 那么取得PyGTK和Glade就很简单:
apt-get install python-gtk2 python-glade2
   现在让我们创建我们第一个简单的界面, 这是你第一次启动Glade时的界面:  
  

我们需要做的就是按下Glade面板的上的”Window” 按钮来创建主窗口. 然后就可以编辑这个窗口的属性了:


   我们把这个窗口命名为MainWindow, 然后再把它的标题设置成”Hello World!”
   如果你已经习惯使用集成的界面设计工具像VisualStudio, 可能在最初使用Glade的时候会有些感到奇怪. 特别是当你实际上并不是把控件放到你想放到屏幕上的一些地方而是把它们”打包”. 但更奇怪的是(至少是对我来讲)大多数界面设计工具都是这样做的,而另外的一些程序像Visual studio实际上才是奇怪的.
   无论如何我们回到向导上来, 接下来要做的事是添加一个标签来告诉用户去点击按钮(当然我们可以把文字放到按钮上,但是只有一个控件有多少乐趣啊?!). 由于GTK使用容器来打包器件, 我们要做的第一件事就是添加一个容器. 因为我们要把标签放到按钮上,所以使用了一个有两行的纵向盒子(Vertical Box). 你可以通过首先点击它在Glade面板上图标然后再点击一些主窗口把它添加上去. 这时候会有一个小对话框弹出来问你需要多少行, 现在我们需要2行.
   接下来就是通过点击Glade面板上的标签按钮然后再点击刚才加上去的窗口的第一行把标签添加上去. 标签的名字就使用默认的名字”label1”, 把它的文字(text)修改为”Please click on the button!”. 修改标签上的文字可以在属性窗口里完成, 你可能还没有注意到这个窗口, 它可以显示并允许修改当前选中的控件.
   再接下来就是再使用和添加标签相同的方法把按钮添加到第二行去(当然不能是第一行了). 我们把这个按钮命名为btnHelloworld,把它上面的文字修改为”Click me!”.
    现在我们需要设置工程选项了. 我把这个工程命名为PyHelloWorld,并把它保存在”my projects/PyHelloWorld”文件夹中:
  
Click here to open new window
CTRL+Mouse wheel to zoom in/out
   然后你就可以在PyHelloWorld目录里看到有两个文件被创建, 一个是扩展名为.gladep的glade的工程文件,另一个是扩展名为.glade的glade界面XML文件.
   现在我们需要创建一个python程序,它装载这个glade文件然后显示它. 正面我就在这个目录里创建了一个名为PyHelloWorld.py的文件:
  

    现在我们首先需要导入在这个工程里需要的所有库:
#!/usr/bin/env python
import sys
try:
        import pygtk
          pygtk.require("2.0")
except:
          pass
try:
        import gtk
          import gtk.glade
except:
        sys.exit(1)
    接下来我们要做的就是创建主要的类. 我把它命名为HelloWorldGTK. 我们通过写它的__init__函数来装载glade文件:
class HellowWorldGTK:
        """This is an Hello World GTK application"""
        def __init__(self):
               
                #Set the Glade file
                self.gladefile = "pyhelloworld.glade"  
                self.wTree = gtk.glade.XML(self.gladefile)
               
                #Get the Main Window, and connect the "destroy" event
                self.window = self.wTree.get_widget("MainWindow")
                if (self.window):
                        self.window.connect("destroy", gtk.main_quit)
   上面的代码里, 我们做的第一件事(定义类以后)是指定我们要使用的glade文件并使用它创建一个gtk.glade.XML对象. 正面是pyGTK2参考里对这个对象的描述:
这个对象代表一个XML接口描述的实例. 当一个此类对象被创建时, 会读XML文件,接口也会被创建出来. Gtk.glade.XML对象提供了一个可以通过在xml描述中赋给物件的名称访问这些物件的接口.
  此gtk.glade.XML对象也可以用来关联在XML描述中已命名的信号和信号处理器. Glade库(libglade)也提供了一个可以在程序的符号表中查找信号处理器名称并自动的把尽可能多的信号处理器关联起来的接口.
   因此当创建完gtk.glade.XML对象后我们就需要创建并装载主界面.
  接下来就是实例化主窗口并把它的销毁事件与get.main_quit()函数关联起来.这样就会在关闭主窗口时退出这个程序. 否则的话在主窗口关闭后程序还会继续运行(这显然不是我们所希望的).
  这些就是HelloWorldGTK类完成的工作. 接下来我们就需要创建一个这个类的实例然后开始GTK的主循环:
if __name__ == "__main__":
        hwg = HellowWorldGTK()
        gtk.main()
   下面就是程序的结果, 现在来说还算是相当简单. 如果你运行这个文件,你会看到这个小的GTK窗口, 现在除了在你关闭窗口时正常退出外它什么也不能做:
  

to be continue....(太晚了,回家先)


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5462/showart_150996.html

जनसुनवाई पोर्टल 说:
2022年10月26日 22:53

Samadhan portal is named as Integrated Grievance Redressal System (jansunwai up 2022). This system is launched by Uttar Pradesh Chief minister for providing opportunity to the people of UP to convey their complaints without going to the government offices. जनसुनवाई पोर्टल This is to make the authorities accountable to the people in all matters and facilities provided for them by the government. This portal is known as JANSUNWAI portal.

Indusind Net Banking 说:
2022年11月02日 22:20

Indusind bank limited an Indian origin new generation bank offering banking and financial services to all Indian citizens and non-citizens. The bank was established in 1994 in Pune by the union finance minister Manmohan Singh. Indusind Net Banking Registration It's the best new-generation bank in the country offering modern-day services such as internet banking. Indusind bank services enable customers to access funds and transfer funds online through indusnet bank services.

mi tv samsung no se 说:
2023年7月31日 00:03

Es posible que el Wi-Fi no funcione en su televisor Samsung pero esté funcionando en otros dispositivos, o puede estar conectado al punto de acceso de su teléfono y acceder a Internet de esa manera, mi tv samsung no se conecta a wifi pero se niega a conectarse a Internet a través de su enrutador. Esta guía simple lo ayudará a volver a conectar su televisor Samsung en línea si no se conecta a Internet pero otros dispositivos pueden hacerlo o si Wi-Fi no funciona en absoluto.

Punjab 7th Class Sy 说:
2023年8月03日 18:54

PSEB 7th Class Exam Date Sheet 2024 Available at Official Website, Every Year This Elementary School Final Exam Conducted Month of April, so Students Download Punjabi Class new Syllabus 2024 Regular Redding and Fallow for Final Exam Better Performance,Students we are Providing here the Latest Updated PSEB Class Syllabus 2024, Punjab Board Students who entered 7th Class This year Punjab 7th Class Syllabus 2024 must go Through This Latest Syllabus to Understand the course Structure for the Upcoming Part of the Current Academic Session.one of the most Important Tools that help in knowing the Course Description.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter