Python 3: pass filename variable from Gtk.FileChooser to __init__ function
<p>I'm writing a Gtk program that does stuff with images. I got app window with menu and connected one of the buttons to Gtk.FileChooser that gets a filename (i can open it with Gtk.Image() but cant do much with such object afaik). The problem is I don't know how to pass the filename to my <strong>init</strong> function so I can open image from that filename using opencv (need to be able to draw with mouse on that image thats why opencv3). Here's the code structure Im using:</p>

<pre><code>class main_win(Gtk.Window):
 def __init__(self):
 '''menu stuff and box with widgets, few labels'''

  def FileChooser(self):
   dialog = Gtk.FileChooserDialog("Open a File Image", self, Gtk.FileChooserAction.OPEN,
                                   (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                    Gtk.STOCK_OPEN, Gtk.ResponseType.OK))


    response = dialog.run()
    if response == Gtk.ResponseType.OK:
       path = dialog.get_filename()
    elif response == Gtk.ResponseType.CANCEL:
       pass
    dialog.destroy()
</code></pre>

<p>Ideally I would prefere to put this inside init:</p>

<pre><code>img = cv2.imread(path,0)
cv2.imshow('image',img)
</code></pre>

<p>Then afaik i could do stuff with the image in opencv window like for example getting pixel  (from a drawn area with mouse or just a single pixel by mouse click) statistics into gtk.label or drawing plots.</p>

<p>Im fairly new to python, so maybe I'm asking sth super easy or super stupid;p
Thanks in advance ;).</p>

<p>Well, maybe you have a legitimate reason for using OpenCV to simply display an image (because for example you use that library elsewhere in your code) but you can use GTK+ 3 facilities for that.</p>

<pre><code>import os, re

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

class main_win(Gtk.Window):

    def __init__(self):
        '''menu stuff and box with widgets, few labels'''

        super().__init__(title="Image show")

        main_column = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
        self.add(main_column)

        button1 = Gtk.Button(label="Get Image")
        button1.connect("clicked", self.FileChooser)
        main_column.pack_start(button1, True, True, 0)

        self.embedded_image = Gtk.Image()
        main_column.pack_start(self.embedded_image, True, True, 0)


    def FileChooser(self, widget):
        dialog = Gtk.FileChooserDialog("Open a File Image", self, Gtk.FileChooserAction.OPEN,
                                       (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                        Gtk.STOCK_OPEN, Gtk.ResponseType.OK))


        response = dialog.run()
        if response == Gtk.ResponseType.OK:
           path = dialog.get_filename()
           self.embedded_image.set_from_file(path)
           self.show_all()

        dialog.destroy()

main_windows = main_win()
main_windows.connect("delete-event", Gtk.main_quit)
main_windows.show_all()
Gtk.main()      
</code></pre>

<p>You can use a <a href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Image.html" rel="nofollow"><code>Gtk.Image()</code></a> to put an image anywhere in your interface. You can update it using <a href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Image.html#Gtk.Image.set_from_file" rel="nofollow"><code>.set_from_file()</code></a>.</p>

python variables scope gtk3 opencv3.0