Glade Ubuntu-背景图像上的透明按钮

Glade Ubuntu-背景图像上的透明按钮

我正在尝试制作一个 Ubuntu 应用程序。我需要在 Glade 中的背景图像的特定位置放置透明按钮,这样按钮单击时图像就会发生变化。

到目前为止我还没能做到这一点。我的按钮根本就没落在图像上。此外,我也没能使按钮完全透明(即按下时可见)。

答案1

  1. 对于固定按钮位置,最好使用GtkFixedGtkLayout(带滚动)
  2. 对于按钮透明度,对我来说它的工作正常。如果不行,请尝试:
    • Border ReliefNone用于平面按钮
    • 取消选中Focus on Click以避免任何焦点突出显示

例子:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLayout" id="layout1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">59</property>
            <property name="height_request">30</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="opacity">0</property>
          </object>
          <packing>
            <property name="x">87</property>
            <property name="y">64</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

第一次,我以为你指的是桌面背景。所以我就把它留下来,也许对其他人有帮助。

我不确定 Quickly 在 (Ubuntu 13.04) 中使用了哪个版本的 PyGTK。最好使用 0.1 不透明度进行测试。Glade 没有提供设置keep_below属性的选项。

PyGTK 3

添加:

    <property name="opacity">0.0</property>
    <property name="decorated">False</property>

builder_example.glade

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <!-- <property name="keep_below">True</property> -->
    <property name="opacity">0.0</property>
    <property name="decorated">False</property>
    <!-- <property name="position">gtk.WIN_POS_CENTER_ALWAYS</property> -->
    <signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
    <child>
      <object class="GtkButton" id="button1">
        <property name="label" translatable="yes">button</property>
        <property name="use_action_appearance">False</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="use_action_appearance">False</property>
        <signal name="pressed" handler="onButtonPressed" swapped="no"/>
      </object>
    </child>
  </object>
</interface>

我已经对这些进行了评论,因为它们引发了错误(我不确定 Glade 中是否还有其他方法)

    <!-- <property name="keep_below">True</property> -->
    <!-- <property name="position">gtk.WIN_POS_CENTER_ALWAYS</property> -->

因此,我将它们添加到.py 文件中以便动态应用:

window.set_keep_below(True)
window.set_position(Gtk.WindowPosition.CENTER_ALWAYS)

builder_example.py

from gi.repository import Gtk

class Handler:
    def onDeleteWindow(self, *args):
        Gtk.main_quit(*args)

    def onButtonPressed(self, button):
        print("Hello World!")

builder = Gtk.Builder()
builder.add_from_file("builder_example.glade")
builder.connect_signals(Handler())

window = builder.get_object("window1")
window.set_keep_below(True)
window.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
window.show_all()

Gtk.main()

PyGTK 2

添加:

        self.window.set_keep_below(True)
        self.window.set_opacity(0.0)
        self.window.set_decorated(False)
        self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)

helloworld.py

#!/usr/bin/env python

# example helloworld.py

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

    # This is a callback function. The data arguments are ignored
    # in this example. More on callbacks below.
    def hello(self, widget, data=None):
        print "Hello World"

    def delete_event(self, widget, event, data=None):
        # If you return FALSE in the "delete_event" signal handler,
        # GTK will emit the "destroy" signal. Returning TRUE means
        # you don't want the window to be destroyed.
        # This is useful for popping up 'are you sure you want to quit?'
        # type dialogs.
        print "delete event occurred"

        # Change FALSE to TRUE and the main window will not be destroyed
        # with a "delete_event".
        return False

    def destroy(self, widget, data=None):
        print "destroy signal occurred"
        gtk.main_quit()

    def __init__(self):
        # create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        # Add these
        self.window.set_keep_below(True)
        self.window.set_opacity(0.0)
        self.window.set_decorated(False)
        self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)

        # When the window is given the "delete_event" signal (this is given
        # by the window manager, usually by the "close" option, or on the
        # titlebar), we ask it to call the delete_event () function
        # as defined above. The data passed to the callback
        # function is NULL and is ignored in the callback function.
        self.window.connect("delete_event", self.delete_event)

        # Here we connect the "destroy" event to a signal handler.
        # This event occurs when we call gtk_widget_destroy() on the window,
        # or if we return FALSE in the "delete_event" callback.
        self.window.connect("destroy", self.destroy)

        # Sets the border width of the window.
        self.window.set_border_width(10)

        # Creates a new button with the label "Hello World".
        self.button = gtk.Button("Hello World")

        # When the button receives the "clicked" signal, it will call the
        # function hello() passing it None as its argument.  The hello()
        # function is defined above.
        self.button.connect("clicked", self.hello, None)

        # This will cause the window to be destroyed by calling
        # gtk_widget_destroy(window) when "clicked".  Again, the destroy
        # signal could come from here, or the window manager.
        self.button.connect_object("clicked", gtk.Widget.destroy, self.window)

        # This packs the button into the window (a GTK container).
        self.window.add(self.button)

        # The final step is to display this newly created widget.
        self.button.show()

        # and the window
        self.window.show()

    def main(self):
        # All PyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event).
        gtk.main()

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

参考:

相关内容