【问题标题】:python Gtk3 return value when button clicked单击按钮时python Gtk3返回值
【发布时间】:2020-04-25 00:28:16
【问题描述】:

我有一个主 python 程序 A.py,它调用 GUI Gtk python 程序 B.py 来显示一个窗口。 我希望这个窗口是颜色按钮,当我单击一个时,主 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()

程序一切正常,我的窗口有多个颜色按钮,但是我不知道如何恢复我点击的颜色。 return_color 中的返回操作不会返回到 A.py 程序。我该怎么做?我应该将标准输出与打印一起使用吗?我确切地说,在点击之后,我想做一些根本不需要 GUI 的其他操作。

【问题讨论】:

  • return color[0] 之前做self.selected_color = color[0],在Gtk.main() 之后做return w.selected_color
  • 已解决。非常感谢stovfl。在 2 分钟内解决了我的问题。非常感谢。
  • 如果这样可以解决问题,那么您应该将其写为答案。
  • @yoarch “我该怎么做?”:写在本页底部的文本框中,然后点击[Post Your Answer]。阅读How do I write a good answer?

标签: python user-interface events return gtk


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-01-11
  • 2020-01-20
  • 2023-03-18
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多