1

im new with GTK and I want to know how to show an image at (x, y) when i clicked on the window. I put a image.show() and an image.hide() but nothing appear...

from gi.repository import Gtk
import time

def callback(window, event):
    print ('Clicked at x=', event.x, "and y=", event.y)
    image.show()
    time.sleep(0.2)
    image.hide() 

image = Gtk.Image()
image.set_from_file("C:\\Users\\alimacher\\FF0000.png")

window = Gtk.Window()

window.set_title('Dalle Test')

window.set_size_request(320, 240)

window.connect('button-press-event', callback)
window.connect('destroy', lambda w: Gtk.main_quit())
window.show_all()
Gtk.main()

Thank you.

2 Answers2

0

Due to the Gtk main loop, you can't use time.sleep. Instead use timeouts like this :

from gi.repository import GLib
....
image.show()
GLib.timeout_add(200, image.hide)

Besides that, you didn't add the image to the window by using window.add(image)

theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • You missed that the image wasn't added to the window. And that absolute positioning of a widget needs a Fixed container. – Dan D. Sep 25 '19 at 11:33
  • Nice catch, @DanD. – theGtknerd Sep 25 '19 at 11:35
  • Thanks but, the problem is not the time, it works like that, i just want to show the image when i clicked and hiding it 0.2s later... –  Sep 25 '19 at 11:43
  • The problem is the `time.sleep()`. Try my latest version, I proved it works on my computer. I don't use time.sleep with Gtk, **ever**. – theGtknerd Sep 25 '19 at 12:07
  • Oh yeah i just try it. Thank you !! –  Sep 25 '19 at 13:18
  • @theGtknerd One last thing, do you know how can I show it at some coordinates ? (As you seems to be comfortable with GTK I take advantage ahah) –  Sep 25 '19 at 13:31
  • Sorry for the delay, I was away. Please post a new question, link it back here and I will answer... – theGtknerd Sep 25 '19 at 18:47
  • No need for an additional question. That was already part of this one. – Dan D. Sep 25 '19 at 22:12
0

Consider the following which is the program I thought you meant to write. It shows the image where you click and then makes it disappear 0.2 seconds latter. It would be more interesting with a longer delay.

The EventBox is needed as neither Window or the Fixed emits button-press-event although they are Widgets. This might have changed in a latter version than I have so it might be possible to omit it. But the code didn't work without it on my machine.

Calling show on the EventBox and Fixed was redundant as the window.show_all() will show them as they are part of the tree at that time. But the show call on the Image is not unless you are using the version of GTK where widgets are shown by default instead of hidden. As the Image does not exist at that time.

from gi.repository import Gtk, GLib

window = Gtk.Window()
window.set_title('Dalle Test')
window.set_size_request(320, 240)

eventbox = Gtk.EventBox()
window.add(eventbox)

fixed = Gtk.Fixed()

eventbox.add(fixed)

def callback(window, event, *data):
    print('Clicked at x=', event.x, "and y=", event.y)
    image = Gtk.Image()
    image.show()
    image.set_from_file("FF0000.png")
    image.set_size_request(64,64)
    fixed.put(image, int(event.x), int(event.y))

    def remove():
        fixed.remove(image)
    GLib.timeout_add(200, remove)

eventbox.connect('button-press-event', callback)

window.connect('destroy', lambda w: Gtk.main_quit())
window.show_all()
Gtk.main()
Dan D.
  • 73,243
  • 15
  • 104
  • 123