2 回答

TA貢獻1829條經驗 獲得超9個贊
考慮以下是我認為您打算編寫的程序。它會顯示您單擊的圖像,然后在 0.2 秒后使其消失。延遲時間更長會更有趣。
EventBox 是必需的,因為 Window 或 Fixed 都不會發出按鈕按下事件,盡管它們是小部件。這可能在我的后一個版本中發生了變化,因此可以省略它。但是在我的機器上沒有它,代碼就無法工作。
在 EventBox 和 Fixed 上調用 show 是多余的,window.show_all()因為它們會顯示它們,因為它們當時是樹的一部分。但是除非您使用的是 GTK 版本,其中小部件默認顯示而不是隱藏,否則圖像上的顯示調用不會。由于當時圖像不存在。
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()

TA貢獻1818條經驗 獲得超11個贊
由于 Gtk 主循環,您不能使用 time.sleep。而是使用這樣的超時:
from gi.repository import GLib
....
image.show()
GLib.timeout_add(200, image.hide)
除此之外,您沒有使用window.add(image)
添加回答
舉報