python Gtk3 return 单击按钮时的值

python Gtk3 return value when button clicked

我有一个主 python 程序 A.py 调用 GUI Gtk python 程序 B.py 来显示 window。 我希望这个 window 是颜色按钮,当我点击一个按钮时,主要 A.py 代码恢复一个值,即 RGB 颜色值。

A.py

import B
c = B.gui_color()
print(c)

B.py

class W(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="colors")
        self.box = Gtk.Box(spacing=0, homogeneous=True)
        self.add(self.box)

        colors = j.load("colors.json")

        for c in colors:
            b = Gtk.Button()
            b.connect("clicked", self.return_color, c["value"])
            # x257 to get the GTK color
            b.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(c["value"][0] * 257, c["value"][1] * 257, c["value"][2] * 257))
            self.box.pack_start(b, True, True, 0)

    def return_color(self, widget, *color):
        self.close()
        return color[0]

def gui_color():
    w = W()
    w.connect("destroy", Gtk.main_quit)
    w.show_all()
    Gtk.main()

程序一切正常,我的 window 有多个颜色按钮,但我不知道如何恢复我点击的颜色。 return_color 中的 return 操作不会 return 到 A.py 程序。我该如何执行?我应该将 stdout 与打印一起使用吗?我明确指出,在单击之后,我想执行其他根本不需要 GUI 的操作。

return_color 中的 return color[0] 之前执行 self.selected_color = color[0],在 Gtk.main() 之后执行 return w.selected_color