简体   繁体   中英

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.

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. 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?

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. This is how it looks like:

在此输入图像描述

Inside the window class, I add the Frame to the grid:

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:

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):

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; it already contains a widget of type GtkImage

Any help is appreciated!

You can use 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). Or, if you already have the Gtk.Image there, you can use:

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.

In general, you can use the documentation for C and change the idiom to Python. Also, you can check the demos available in PyGObject , in particular, the demo for handling images .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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