c - 如何在 gtk+ 中以恒定速度移动图像部件?

标签 c gtk

我想以恒定的速度将图像小部件从一个位置移动到另一个位置。 如何在 C 中执行此操作?

最佳答案

我不确定 Gtk 是否是动画的最佳选择,但无论如何:

(python 代码,因为未指定语言)

from gi.repository import Gtk, GObject
from math import sqrt


def move_widget(widget, target_x, target_y, speed):
    # get the widget's current position
    fixed= widget.get_parent()
    x= fixed.child_get_property(widget, 'x')
    y= fixed.child_get_property(widget, 'y')

    # calculate its movement
    vector= (target_x-x, target_y-y)
    length= sqrt(vector[0]**2 + vector[1]**2)/speed
    vector= (vector[0]/length, vector[1]/length)
    # make sure the widget doesn't move too far
    x= max(target_x, x+vector[0]) if target_x<x else min(target_x, x+vector[0])
    y= max(target_y, y+vector[1]) if target_y<y else min(target_y, y+vector[1])

    # move the widget
    fixed.move(widget, x, y)

    # return whether or not we've reached the destination
    return target_x!=x or target_y!=y

w= Gtk.Window()

f= Gtk.Fixed()
# create a random image...
i= Gtk.Image.new_from_icon_name('go-next', Gtk.IconSize.BUTTON)
# create some space to move the image
f.set_size_request(800, 800)
f.put(i, 100, 50)
w.add(f)

w.connect('delete-event', Gtk.main_quit)
w.show_all()
# every 1000 milliseconds, move the image closer to the destination
# in this case: destination: (300,300) speed: 10 pixels (per 1000 milliseconds)
GObject.timeout_add(1000, move_widget, i, 300, 300, 10)
Gtk.main()

关于c - 如何在 gtk+ 中以恒定速度移动图像部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553572/

相关文章:

c - 删除数组中相同的项

c - Squeak 和 Esterel 的其他现代免费类似物是什么?

python - 用于 gtk 应用程序的交互式 python 控制台

python - 停止 Python 程序,直到用户执行操作

c++ - 是否可以将 GTK+ 与 C++ 一起使用?

c - 如何知道 Linux 系统调用是否可重启?

c - 我需要 for 循环的解释

windows - 在 Windows 上编译 GTK+ 应用程序?

c - GCC 中的简单错误陷阱

python-2.7 - import pygtk 有效,但不能 import gtk