GTK 和 PYGTK 的区别

发布于 2024-09-27 15:07:25 字数 543 浏览 1 评论 0 原文

许多程序员以这种方式导入 gtk 和 pygtk:

import gtk
import pygtk

我仅使用 gtk 创建了一个简单的程序并且它有效:

import gtk

window = gtk.Window()
window.set_size_request(800, 700)
window.set_position(gtk.WIN_POS_CENTER)
window.connect("destroy", gtk.main_quit)

button = gtk.Button("Vai")
button.set_size_request(30, 35)
button.connect("clicked", naviga)
testo = gtk.Entry()

h = gtk.HBox()
h.pack_start(testo)
h.pack_start(button)

window.add(h)
window.show_all()
gtk.main()

所以...问题是: GTK 和 PYGTK 有什么区别?

many programmers import both gtk and pygtk in this way:

import gtk
import pygtk

I have created a simple program using only gtk and it works:

import gtk

window = gtk.Window()
window.set_size_request(800, 700)
window.set_position(gtk.WIN_POS_CENTER)
window.connect("destroy", gtk.main_quit)

button = gtk.Button("Vai")
button.set_size_request(30, 35)
button.connect("clicked", naviga)
testo = gtk.Entry()

h = gtk.HBox()
h.pack_start(testo)
h.pack_start(button)

window.add(h)
window.show_all()
gtk.main()

So... the question is: what is the difference from GTK and PYGTK ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-10-04 15:07:25

pygtkpython-gobject提供。
gtkpython-gtk2提供。

pygtk 提供了 pygtk.require 函数,允许您要求安装特定版本的 gtk(或更高版本)。例如,

import pygtk
pygtk.require('2.0')

只能导入 gtk,但如果其他人的 gtk 版本较旧,您的程序可能无法在其他人的计算机上按预期运行。

pygtk is provided by python-gobject.
gtk is provided by python-gtk2.

pygtk provides the pygtk.require function which allows you to require that a certain version of gtk (or better) is installed. For example

import pygtk
pygtk.require('2.0')

importing gtk only is possible, but your program may not work as expected on someone else's machine if their version of gtk is older.

不奢求什么 2024-10-04 15:07:25

我对 pygtkgtk 感到困惑,因为我的测试应用程序与 gtk 配合得很好。

我现在意识到 gtk 意味着 PyGTK,包含绑定的 Python 模块到 Gtk+ 2.x 库。 pygtk 是另一个模块,也是 PyGTK 的一部分,它可以(但是不需要)用于确保 Gtk+ 库的特定版本。

gtk 和 pytk 模块仅适用于 Python 2。对于 Python 3,您需要通过 gi GObject Introspection 模块。

Python GObject Introspection 模块(又名 gigi.repository)是 C 库之间的中间件层(使用 GObject)和语言绑定。本质上,它从底层 C 源代码自动生成 Python 绑定。 GObjectGLib 对象系统,其中 GLib 为用 C 语言编写的库和应用程序提供核心应用程序构建块。 GObject 提供GTK+ 具有面向对象的基于 C 的 API 和自动透明 API 绑定到其他编译或解释语言(例如 Python) 。

GTK3+ 库使用 Gobject 但 GTK2+ 库不使用。这意味着您不能将 Gtk2+ 与 Python 版本 3 一起使用。

对于 Python 2,您可以使用

import pygtk
pygtk.require('2.0')
import gtk

或仅

import gtk

对于 Python 3,您需要

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk

您可以验证版本:

对于 Gtk+ 2(Python 2 或 3)

print("PyGTK %d.%d.%d Gtk+ %d.%d.%d" % (gtk.pygtk_version + gtk.gtk_version))

对于 Gtk+ 3(仅限 Python 3)

print("Gtk %d.%d.%d" % (Gtk.get_major_version(),
                                         Gtk.get_minor_version(),
                                         Gtk.get_micro_version()))

I was confused by pygtk vs gtk because my test app worked fine with gtk.

I now realise that gtk means PyGTK, the Python module that contains bindings to the Gtk+ 2.x library. pygtk is another module, also part of PyGTK, that can (but doesn't need to) be used to ensure a certain version of the Gtk+ library.

The gtk and pytk modules apply to Python 2 only. For Python 3 you need to use Gtk+ through the gi GObject Introspection module.

The Python GObject Introspection module (aka gi or gi.repository) is a middleware layer between C libraries (using GObject) and language bindings. Essentially it autogenerates Python bindings from the underlying C source code. GObject is the GLib Object system, where GLib provides the core application building blocks for libraries and applications written in C. GObject provides GTK+ with object-oriented C-based APIs and automatic transparent API bindings to other compiled or interpreted languages (such as Python).

The GTK3+ library uses Gobject but the GTK2+ library does not. This means that you can not use Gtk2+ with Python version 3.

For Python 2 you'd use

import pygtk
pygtk.require('2.0')
import gtk

or just

import gtk

For Python 3 you need

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk

You can verify versions with:

For Gtk+ 2 (Python 2 or 3)

print("PyGTK %d.%d.%d Gtk+ %d.%d.%d" % (gtk.pygtk_version + gtk.gtk_version))

For Gtk+ 3 (Python 3 only)

print("Gtk %d.%d.%d" % (Gtk.get_major_version(),
                                         Gtk.get_minor_version(),
                                         Gtk.get_micro_version()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文