如何使 popover.set_modal(True) 在 python/Budgie 小程序中正常工作?

如何使 popover.set_modal(True) 在 python/Budgie 小程序中正常工作?

在适用于 budgie 桌面的小程序中,有一个 Budgie.Popover.new() 对象。它通过面板中的按钮打开和关闭。我设置了 Budgie.Popover.set_modal(True),但它不起作用。我必须单击按钮才能关闭弹出窗口。当我单击弹出窗口外部时,什么也不会发生。下面是小程序的片段,显示了弹出窗口的创建和另一个按钮操作。

#create a simple button for the applet
    self.button = Gtk.Button()
    self.button.set_relief(Gtk.ReliefStyle.NONE)
    self.button.set_tooltip_text("User Name")
    self.img = Gtk.Image.new_from_icon_name("preferences-desktop-personal", Gtk.IconSize.BUTTON)
    self.img1 = Gtk.Image.new_from_pixbuf(pixbuf)
    self.button.add(self.img)
    self.add(self.button)
    self.show_all()

    # create a popover
    self.popover = Budgie.Popover.new(self.button)
    self.popover.set_modal(True)
    self.popover.set_size_request(50, 130)

按钮:

#applet button signal
    self.button.connect("clicked", self.on_button_clicked)

    def on_button_clicked(self, button):

    if self.popover.get_visible():

        self.popover.hide()

    else:

        self.popover.show_all()

这是:

#!/usr/bin/env python3

import pickle
import subprocess
import os
import pwd
import gi.repository
gi.require_version('Budgie', '1.0')
gi.require_version('AccountsService', '1.0')
from gi.repository import Budgie, GObject, Gtk, AccountsService, GLib, 
Gio, GdkPixbuf


file = os.path.expanduser('~/states.p')
class UserName(GObject.GObject, Budgie.Plugin):

        __gtype_name__ = "UserName"

    def __int__(self):
        GObject.Object.__init__(self)

    def do_get_panel_widget(self, uuid):

        return UserNameApplet(uuid)

class UserNameApplet(Budgie.Applet):

    def __init__(self, uuid):

        Budgie.Applet.__init__(self)

    # get avatar
        current_user = GLib.get_user_name()
        bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
        result = bus.call_sync('org.freedesktop.Accounts',
                           '/org/freedesktop/Accounts',
                           'org.freedesktop.Accounts',
                           'FindUserByName',
                           GLib.Variant('(s)', (current_user,)),
                           GLib.VariantType.new('(o)'),
                           Gio.DBusCallFlags.NONE,
                           -1,
                           None)
        (path,) = result.unpack()

        result = bus.call_sync('org.freedesktop.Accounts',
                           path,
                           'org.freedesktop.DBus.Properties',
                           'GetAll',
                           GLib.Variant('(s)', 
                           ('org.freedesktop.Accounts.User',)),
                           GLib.VariantType.new('(a{sv})'),
                           Gio.DBusCallFlags.NONE,
                           -1,
                           None)
    (props,) = result.unpack()
    #print(props['IconFile'])

    pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(props['IconFile'], 22, 22, False)

    username = Gtk.Label(pwd.getpwuid(os.getuid())[4])

    #create a simple button for the applet
    self.button = Gtk.Button()
    self.button.set_relief(Gtk.ReliefStyle.NONE)
    self.button.set_tooltip_text("User Name")
    self.img = Gtk.Image.new_from_icon_name("preferences-desktop-personal", Gtk.IconSize.BUTTON)
    self.img1 = Gtk.Image.new_from_pixbuf(pixbuf)
    self.button.add(self.img)
    self.add(self.button)
    self.show_all()

    # create a popover
    self.popover = Budgie.Popover.new(self.button)
    self.popover.set_modal(True)
    self.popover.set_size_request(50, 130)

    # create a box
    self.box1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
    self.box1.set_spacing(10)

    self.popover.add(self.box1)

    #create the user box with avatar
    self.boxy = Gtk.Box()
    self.box1.pack_start(self.boxy, True, True, 0)
    self.boxy.pack_start(username, True, True, 5)
    self.boxy.add(self.img1)

    # create a separator
    separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
    self.box1.add(separator)

    #create a box with label and a switch
    box2 = Gtk.Box()
    self.box1.add(box2)
    self.switch = Gtk.Switch()
    self.switch.connect("notify::active", self.on_switch_toggled)
    label = Gtk.Label("\tDisplay Full Name\t")
    label.set_justify(Gtk.Justification.LEFT)
    box2.pack_start(label, False, False, 5)
    box2.pack_start(self.switch, False, False, 0)

    #create a separator
    separator1 = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
    self.box1.add(separator1)

    # create a eventbox and a label
    box3 = Gtk.EventBox()
    label1 = Gtk.Label("User Settings")
    label1.set_justify(Gtk.Justification.LEFT)
    box3.add(label1)
    self.box1.pack_start(box3, False, False, 0)
    box3.connect("button-press-event", self.event_press1)

    # create a separator
    separator2 = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
    self.box1.add(separator2)

    # create a eventbox and a label
    box4 = Gtk.EventBox()
    label2 = Gtk.Label("Log Out")
    label2.set_justify(Gtk.Justification.LEFT)
    box4.add(label2)
    self.box1.pack_start(box4, False, False, 0)
    box4.connect("button-press-event", self.event_press2)

.plugin 文件:

[Plugin]
Loader=python3
Module=UserName
Name=username
Description=username
Authors=chris
Copyright=Copyright © 2018
Website=some_website
Icon=preferences-desktop-personal

我做错了什么?如何让它工作?

提前致谢。

相关内容