简体   繁体   English

Glade还是没有林间空地:使用PyGtk的最佳方式是什么?

[英]Glade or no glade: What is the best way to use PyGtk?

I've been learning python for a while now with some success. 我已经学习了一段时间的python并取得了一些成功。 I even managed to create one or two (simple) programs using PyGtk + Glade. 我甚至设法使用PyGtk + Glade创建了一个或两个(简单)程序。

The thing is: I am not sure if the best way to use GTK with python is by building the interfaces using Glade. 问题是:我不确定使用GTK和python的最佳方法是使用Glade构建接口。

I was wondering if the more experienced ones among us (remember, I'm just a beginner) could point out the benefits and caveats of using Glade as opposed to creating everything in the code itself (assuming that learning the correct gtk bindings wouldn't exactly be a problem). 我想知道我们中更有经验的人(记住,我只是一个初学者)可以指出使用Glade的好处和注意事项,而不是在代码本身中创建所有内容(假设学习正确的gtk绑定不会完全是个问题)。

I would say that it depends: if you find that using Glade you can build the apps you want or need to make than that's absolutely fine. 我会说这取决于:如果你发现使用Glade你可以构建你想要或需要制作的应用程序,那绝对没问题。 If however you actually want to learn how GTK works or you have some non-standard UI requirements you will have to dig into GTK internals (which are not that complicated). 但是,如果您真的想了解GTK的工作原理或者您有一些非标准的UI要求,那么您将不得不深入研究GTK内部(这并不复杂)。

Personally I'm usually about 5 minutes into a rich client when I need some feature or customization that is simply impossible through a designer such as Glade or Stetic . 就个人而言,当我需要一些功能或定制时,我通常需要大约5分钟才能进入富客户端,而Glade或Stetic等设计师根本不可能。 Perhaps it's just me. 也许这只是我。 Nevertheless it is still useful for me to bootstrap window design using a graphical tool. 尽管如此,使用图形工具引导窗口设计对我来说仍然很有用。

My recommendation: if making rich clients using GTK is going to be a significant part of your job/hobby then learn GTK as well since you will need to write that code someday. 我的建议:如果使用GTK创建富客户端将成为您工作/业余爱好的重要组成部分,那么也要学习GTK,因为有一天您需要编写该代码。

PS I personally find Stetic to be superior to Glade for design work, if a little bit more unstable. PS我个人觉得Stetic在设计工作方面要优于Glade,如果有点不稳定的话。

Use GtkBuilder instead of Glade, it's integrated into Gtk itself instead of a separate library. 使用GtkBuilder而不是Glade,它集成到Gtk本身而不是单独的库中。

The main benefit of Glade is that it's much, much easier to create the interface. Glade的主要好处是创建界面要容易得多。 It's a bit more work to connect signal handlers, but I've never felt that matters much. 连接信号处理程序需要做多少工作,但我从未觉得这很重要。

I started out using glade, but soon moved to just doing everything in code. 我开始使用glade,但很快就开始使用代码执行所有操作。 Glade is nice for simple things, and it's good when you're learning how GTK organizes the widgets (how things are packed, etc). Glade非常适合简单的事情,当你学习GTK如何组织小部件(如何打包等等)时,它是很好的。 Constructing everything in code, however, you have much more flexibility. 但是,在代码中构建所有内容,您可以获得更大的灵活性。 Plus, you don't have the glade dependency. 另外,你没有林间空地依赖性。

Glade is very useful for creating interfaces, it means you can easily change the GUI without doing much coding. Glade对于创建界面非常有用,这意味着您可以轻松更改GUI而无需进行太多编码。 You'll find that if you want to do anything useful (eg build a treeview) you will have to get familiar with various parts of the GTK documentation - in practice finding a good tutorial/examples. 您会发现,如果您想要做任何有用的事情(例如构建树视图),您将不得不熟悉GTK文档的各个部分 - 在实践中找到一个好的教程/示例。

I usually start with Glade until I come to a point where it doesn't have the features I need, eg creating a wizard. 我通常从Glade开始,直到我没有我需要的功能,例如创建向导。 As long as I'm using the standard widgets that Glade provides, there's really no reason to hand-code the GUI. 只要我使用Glade提供的标准小部件,就没有理由手动编写GUI代码。

The more comfortable I become with how Glade formats the code, the better my hand-coding becomes. 我对Glade如何格式化代码越熟悉,我的手工编码就越好。 Not to mention, it's real easy to use Glade to make the underlying framework so you don't have to worry about all the initializations. 更不用说,使用Glade来构建底层框架真的很容易,所以你不必担心所有的初始化。

If you're writing a traditional GUI application which reuses a lot of standard components from GTK+ (buttons, labels, containers etc.) I'd personally go with Glade + Kiwi (a convenience framework for building GTK+ GUI applications). 如果你正在编写一个传统的GUI应用程序,它重复使用GTK +的许多标准组件(按钮,标签,容器等),我个人会选择Glade + Kiwi(构建GTK + GUI应用程序的便利框架)。

The single greatest advantage to using Glade is that it greatly reduces layout/packing code. 使用Glade的最大好处是它大大减少了布局/打包代码。 Here's an extremely simply example which already shows the issues with manually laying out a GUI (without using any helper functions): 这是一个非常简单的示例,它已经显示了手动布局GUI(不使用任何辅助函数)的问题:

container = gtk.HBox()
label = gtk.Label(str="test")
container.add(label)

For more examples take a look here . 有关更多示例,请查看此处 Even if you're writing a complicated custom widget you can always create a placeholder in Glade and replace that after instantiation. 即使您正在编写复杂的自定义窗口小部件,也可以始终在Glade中创建占位符,并在实例化后替换它。

It shouldn't be all too long now for the Glade team to release a new version of the designer (3.6.0). 现在,Glade团队发布一个新版本的设计师(3.6.0)应该不会太久。 This new version will add support for GtkBuilder, which replaces libglade (the actual library that transforms the Glade XML files into a widget tree). 这个新版本将添加对GtkBuilder的支持,后者取代了libglade(将Glade XML文件转换为小部件树的实际库)。 The new Glade designer also once again adds support for defining catalogs (sets of widgets) in Python, so you can easily add your own custom widgets. 新的Glade设计器再次添加了对在Python中定义目录(小部件集)的支持,因此您可以轻松添加自己的自定义小部件。

I recommend using Glade for rapid development, but not for learning. 我建议使用Glade进行快速开发,但不能用于学习。 Why? 为什么? because some times you will need to tune up some widgets in order to work as you want they to work, and if you don't really know/understand the properties attributes of every widget then you will be in troubles. 因为有些时候你需要调整一些小部件才能按照你想要的方式工作,如果你真的不知道/不了解每个小部件的属性属性那么你就会遇到麻烦。

For quick and simple screens I use Glade. 对于快速简单的屏幕,我使用Glade。 But for anything that needs finer levels of control, I create a custom classes for what I actually need (this is important, because it's too easy to get carried away with generalisations). 但是对于任何需要更高级别控制的东西,我都会根据实际需要创建一个自定义类(这很重要,因为它很容易被一般化带走)。

With a skinny applications specific classes, I can rapidly change the look and feel application wide from a single place. 通过一个瘦的应用程序特定的类,我可以从一个地方快速改变应用程序的外观和感觉。 Rather like using CSS to mantain consistency for web sites. 而是使用CSS来保持网站的一致性。

First, start to put this in perspective. 首先,开始对此进行透视。

You will be using GTK. 您将使用GTK。 This is a huge C library built in 1993 using the best traditions of 1970s coding style. 这是一个巨大的C库,建于1993年,采用了20世纪70年代编码风格的最佳传统。 It was built to help implement the GIMP, a Photoshop competitor wanna-be with user interface blunders of legend. 它是为了帮助实现GIMP而建立的,这是一个Photoshop竞争对手想要的用户界面错误的传奇。 A typical gui field might have forty or more parameters, mostly repetitive, having getters and setters. 典型的gui字段可能具有四十个或更多参数,大多数是重复的,具有吸气剂和设定器。 There will be pain. 会有痛苦。

The GTK itself manages a complete dynamic type system in C using GObject. GTK本身使用GObject在C中管理一个完整的动态类型系统。 This makes debugging a special joy that requires manually walking through arrays of pointers to methods full of generic argument lists with implicit inheritance. 这使得调试成为一种特殊的乐趣,需要手动遍历指向充满具有隐式继承的通用参数列表的方法的指针数组。 You will also be jumping through Pango libraries when you least expect it, eg, using a Pango constant for where in a label the ellipsis go when the page is small. 当你最不期望它时,你也会跳过Pango库,例如,当页面很小时,使用Pango常量来表示省略号在标签中的位置。 Expect more pain. 期待更多的痛苦。

By now, you are probably vowing to wrap all your GTK interactions in a Model-View-Controller architecture specific to your application. 到目前为止,您可能会发誓将所有GTK交互包装在特定于您的应用程序的模型 - 视图 - 控制器体系结构中。 This is good. 这很好。

Using Glade, or gtkBuilder, or Stetic, will help coral the huge coupling problem of forty parameters to a function. 使用Glade,或gtkBuilder,或Stetic,将有助于将40个参数的巨大耦合问题转化为函数。 Glade provides a basic GUI builder to drag and drop components together. Glade提供了一个基本的GUI构建器,用于将组件拖放到一起。 The parameters and inherited parameters are somewhat separated out. 参数和继承的参数有些分离。 The output of Glade is .glade XML file which you will then read in, attach your callbacks ("signal handlers") to identically named functions, and query or update the in-memory version of that XML to get widgets that you then use pyGTK to manipulate. Glade的输出是.glade XML文件,然后您将读入该文件,将您的回调(“信号处理程序”)附加到具有相同名称的函数,并查询或更新该XML的内存版本以获取您随后使用pyGTK的小部件操纵。 Glade itself is a creaky and not well maintained. 格莱德本身是一个吱吱作响,维护得不好。

Using pyGTK gives you annoyingly fine grained control in order to build your GUI. 使用pyGTK为您构建GUI提供了令人烦恼的细粒度控制。 This will be verbose, copy-and-paste code. 这将是详细的复制粘贴代码。 Each attribute will be a separate function call. 每个属性都是一个单独的函数调用。 The attribute setter does not return anything, so chaining the calls is out of the question. 属性setter不返回任何内容,因此链接调用是不可能的。 Usually, your IDE will give only minimal help on what functions mean and you will be constantly referring to DevHelp or some other tool. 通常,您的IDE只会提供有关函数含义的最小帮助,您将不断引用DevHelp或其他工具。

One would almost expect GTK GUIs were meant to fail. 人们几乎可以期待GTK GUI失败。

Personally I would recommend coding it out instead of using Glade. 我个人建议编码而不是使用Glade。 I'm still learning python and pyGtk but I will say that writing out the UI by hand gave me a lot of insight on how things work under the hood. 我还在学习python和pyGtk,但我会说用手工编写UI让我对如何在幕后工作有很多见解。

Once you have it learned I'd say to give glade, or other UI designers a try but definitely learn how to do it the "hard" way first. 一旦你学会了我就会说要给林间空地或其他UI设计师一试,但绝对要先学会如何以“硬”的方式去做。

您可以使用glade-2进行设计,并使用glade2py.py生成纯pygtk代码,它使用pygtkcompat来支持gtk3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM