Python Gtk 显示和隐藏图像
Python Gtk show and hide image
我是 GTK 的新手,我想知道如何在单击 window 时在 (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()
谢谢。
由于 Gtk 主循环,您无法使用 time.sleep。而是使用这样的超时:
from gi.repository import GLib
....
image.show()
GLib.timeout_add(200, image.hide)
除此之外,您没有使用 window.add(image)
将图像添加到 window
考虑以下我认为您打算编写的程序。它会在您单击的位置显示图像,然后在 0.2 秒后使其消失。延迟时间长点会更有趣
EventBox 是必需的,因为 Window 或 Fixed 都不会发出按钮按下事件,尽管它们是 Widget。这可能在较晚的版本中发生了变化,因此可能会忽略它。但是如果没有它,代码就无法在我的机器上运行。
在 EventBox 和 Fixed 上调用 show 是多余的,因为 window.show_all()
将显示它们,因为它们当时是树的一部分。但是 Image 上的 show 调用不是,除非您使用的是 GTK 版本,在该版本中,小部件默认显示而不是隐藏。由于当时Image不存在
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()
我是 GTK 的新手,我想知道如何在单击 window 时在 (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()
谢谢。
由于 Gtk 主循环,您无法使用 time.sleep。而是使用这样的超时:
from gi.repository import GLib
....
image.show()
GLib.timeout_add(200, image.hide)
除此之外,您没有使用 window.add(image)
考虑以下我认为您打算编写的程序。它会在您单击的位置显示图像,然后在 0.2 秒后使其消失。延迟时间长点会更有趣
EventBox 是必需的,因为 Window 或 Fixed 都不会发出按钮按下事件,尽管它们是 Widget。这可能在较晚的版本中发生了变化,因此可能会忽略它。但是如果没有它,代码就无法在我的机器上运行。
在 EventBox 和 Fixed 上调用 show 是多余的,因为 window.show_all()
将显示它们,因为它们当时是树的一部分。但是 Image 上的 show 调用不是,除非您使用的是 GTK 版本,在该版本中,小部件默认显示而不是隐藏。由于当时Image不存在
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()