简体   繁体   English

如何用Python在Gtk3中显示png图像?

[英]How to show a png image in Gtk3 with Python?

First of all, it is important to mention that I'm learning Python and Gtk+ 3 , so I'm not an advanced programmer in these languages. 首先,重要的是要提到我正在学习PythonGtk + 3 ,所以我不是这些语言的高级程序员。

I'm trying to make a graphical interface in Gtk3 for a Python script that creates a png image, and I'd like to display it, but the PyGobject documentation is so scarce that I haven't found a way to do that. 我正在尝试在Gtk3中创建一个用于创建png图像的Python脚本的图形界面,我想显示它,但是PyGobject文档非常稀缺,我还没有找到办法。 So far, my interface looks like this: 到目前为止,我的界面看起来像这样:

在此输入图像描述

The buttons and text entries are arranged in a grid, and I'd like to keep empty the big space (represented by the big button) to the right until the script finishes building the image, and then show it in that area. 按钮和文本条目排列在一个网格中,我希望将大空间(由大按钮表示)清空,直到脚本完成构建图像,然后在该区域显示它。 The code is here . 代码在这里

Is there a way to do that using Python in Gtk3? 有没有办法在Gtk3中使用Python?

Thanks in advance, 提前致谢,

Germán. 德语。

EDIT 编辑

Taking a look at the demos pointed out by @gpoo I discovered the Frame widget, and I implemented it in my GUI. 看一下@gpoo指出的演示,我发现了Frame小部件,我在GUI中实现了它。 This is how it looks like: 这是它的样子:

在此输入图像描述

Inside the window class, I add the Frame to the grid: 在窗口类中,我将Frame添加到网格中:

self.frame_rgb = Gtk.Frame(label='RGB image')
self.frame_rgb.set_label_align(0.5, 0.5)
self.frame_rgb.set_shadow_type(Gtk.ShadowType.IN)
self.grid.attach_next_to(self.frame_rgb, self.label_img_name,
                         Gtk.PositionType.RIGHT, 3, 8)

I also connect the Run button to a callback function, so that when I click on it, my script creates and then displays the png image: 我还将Run按钮连接到回调函数,这样当我单击它时,我的脚本会创建然后显示png图像:

self.button_run = Gtk.Button(stock=Gtk.STOCK_EXECUTE)
self.button_run.connect('clicked', self.on_button_run_clicked)
self.grid.attach_next_to(self.button_run, self.entry_b_img,
                         Gtk.PositionType.BOTTOM, 1, 1)

Finally, my callback function is (no calculations yet, only render the image to the Frame for testing purposes): 最后,我的回调函数是(没有计算,只是为了测试目的将图像渲染到Frame):

def on_button_run_clicked(self, widget):
    self.img = Gtk.Image.new_from_file('astro-tux.png')
    self.frame_rgb.add(self.img)

but I got the following error when I click the Run button: 但是当我单击“运行”按钮时出现以下错误:

(makeRGB.py:2613): Gtk-WARNING **: Attempting to add a widget with type GtkImage to a GtkFrame, but as a GtkBin subclass a GtkFrame can only contain one widget at a time; (makeRGB.py:2613):Gtk-WARNING **:尝试将类型为GtkImage的小部件添加到GtkFrame,但作为GtkBin子类,GtkFrame一次只能包含一个小部件; it already contains a widget of type GtkImage 它已经包含一个GtkImage类型的小部件

Any help is appreciated! 任何帮助表示赞赏!

You can use Gtk.Image. 你可以使用Gtk.Image。 If you generate a file, you could use: 如果生成文件,则可以使用:

img = Gtk.Image.new_from_file('/path/to/my_file.png')

and add img to the container ( GtkGrid in your case). 并将img添加到容器中(在您的情况下为GtkGrid )。 Or, if you already have the Gtk.Image there, you can use: 或者,如果您已经拥有Gtk.Image ,则可以使用:

img.set_from_file('/path/to/my_file.png')

Instead of ...from_file you can use from_pixbuf , and you can create a Gdk.Pixbuf from a stream. 您可以使用from_pixbuf代替...from_file ,并且可以从流创建Gdk.Pixbuf

In general, you can use the documentation for C and change the idiom to Python. 通常,您可以使用C文档并将习惯用法更改为Python。 Also, you can check the demos available in PyGObject , in particular, the demo for handling images . 此外,您可以检查PyGObject可用演示 ,特别是用于处理图像演示

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

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