【发布时间】:2016-02-12 07:58:27
【问题描述】:
我想让我的 HMI 中的按钮透明,但我找不到任何可以做到这一点的功能!
使用下面的代码,我只能使背景透明,组件不能。
Button = gtk_button_new_with_label ("MyButton");
gtk_window_set_opacity(GTK_Widget(Button), 0.3);
你能帮帮我吗?
【问题讨论】:
我想让我的 HMI 中的按钮透明,但我找不到任何可以做到这一点的功能!
使用下面的代码,我只能使背景透明,组件不能。
Button = gtk_button_new_with_label ("MyButton");
gtk_window_set_opacity(GTK_Widget(Button), 0.3);
你能帮帮我吗?
【问题讨论】:
在这种情况下,问题很可能是在阅读此函数的 python 文档时依赖于操作系统(我知道你使用 C,但这不应该是问题),它提到了一些关于窗口系统功能的评论。
set_opacity(不透明度)[source]
参数:
- opacity (float) – 所需的不透明度,介于 0 和 1 之间
请求
self部分呈现 透明,不透明度 0 表示完全透明,1 表示完全透明 不透明。 (不透明度值被限制在 [0,1] 范围内。)。这有效 在顶级小部件和子小部件上,虽然有一些 限制:对于顶级小部件,这取决于窗口的功能 系统。在 X11 上,这仅对带有 合成管理器运行。见
Gtk.Widget.is_composited()。在 Windows 它应该始终工作,尽管设置了窗口的不透明度 窗口显示后会导致它在 Windows 上闪烁一次。对于子小部件,如果任何受影响的小部件具有本机,则它不起作用 窗口,或禁用双缓冲。
因此,假设您仍然想要一个透明按钮,您当然可以根据Gtk.Image 结合EventBox 和button-press-event 和button-release-event 信号来制作自定义按钮。您可以在其中使用任何您喜欢的图像和不透明度。
由于我更喜欢 Python,所以这个例子是用 Python 编写的,但用 C 语言重现它应该相当容易:
class ImageButton(Gtk.EventBox):
def __init__(self):
super(Gtk.EventBox, self).__init__()
# Load the images for the button
self.button_image = Gtk.Image.new_from_icon_name("edit-delete", Gtk.IconSize.MENU)
self.button_pressed_image = Gtk.Image.new_from_icon_name("edit-delete-symbolic", Gtk.IconSize.MENU)
# Add the default image to the event box
self.add(self.button_image)
# Connect the signal listeners
self.connect('realize', self.on_realize)
self.connect('button-press-event', self.on_button_pressed)
self.connect('button-release-event', self.on_button_released)
def update_image(self, image_widget):
self.remove(self.get_child())
self.add(image_widget)
self.button_pressed_image.show()
def on_realize(self, widget):
hand_pointer = Gdk.Cursor(Gdk.CursorType.HAND1)
window = self.get_window()
window.set_cursor(hand_pointer)
def on_button_pressed(self, widget, event):
self.update_image(self.button_pressed_image)
def on_button_released(self, widget, event):
self.update_image(self.button_image)
【讨论】: