我是 GTK 新手,我想知道当我单击窗口时如何在 (x, y) 处显示图像。 我放置了一个 image.show() 和一个 image.hide() 但没有出现...
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()
谢谢。
最佳答案
考虑以下内容,这是我认为您打算编写的程序。它会在您单击的位置显示图像,然后在 0.2 秒后使其消失。如果延迟更长一点,会更有趣。
需要事件框,因为窗口或固定窗口都不会发出按钮按下事件,尽管它们是小部件。这可能在比我更高的版本中发生了变化,因此可以省略它。但如果没有它,代码在我的机器上就无法工作。
在 EventBox 和 Fix 上调用 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()
关于Python Gtk 显示和隐藏图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58095891/