【问题标题】:How do I update widgets in the window and add new widgets to the window in PyGObject?如何更新窗口中的小部件并将新的小部件添加到 PyGObject 中的窗口?
【发布时间】:2017-06-27 21:58:16
【问题描述】:

我有一个窗口设置了一些小部件,但我希望在一些用户输入(例如按钮单击)后更新这些小部件的信息。另外,我希望事件处理程序能够向窗口添加新的小部件。 我附上了我的问题的以下简单版本的尝试。显然它不起作用。

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk

class Button(Gtk.Box):

    def __init__(self, message, label, window_grid):
        Gtk.Box.__init__(self, spacing=6)
        self.set_border_width(10)
        self.label = label
        self.window_grid = window_grid

        button = Gtk.Button.new_with_label(message)
        button.connect("clicked", self.on_click)
        self.pack_start(button, True, True, 0)

    def on_click(self, widget):
        # Change/update a label in the window_grid
        self.label = LabelBox("Changed the label")
        self.label.queue_draw()
        # Add a new label to the window_grid
        new_label = LabelBox("New label")
        self.window_grid.attach(new_label, 0, 2, 1, 1)

class LabelBox(Gtk.Box):

    def __init__(self, message):
        Gtk.Box.__init__(self, spacing=6)
        self.set_border_width(10)
        label = Gtk.Label(message)
        self.pack_start(label, True, True, 0)

win = Gtk.Window()
window_grid = Gtk.Grid()

label = LabelBox("This is a label")
button = Button("Test", label, window_grid)

window_grid.attach(label, 0, 0, 1, 1)
window_grid.attach(button, 0, 1, 2, 2)

win.add(window_grid)

win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

【问题讨论】:

  • 小部件默认隐藏。

标签: gtk pygobject


【解决方案1】:

您的代码结构格式不正确。这段代码对我有用:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import sys

class GUI:
    def __init__(self):

        self.win = Gtk.Window()
        self.window_grid = Gtk.Grid()
        box = Gtk.Box()

        button = Gtk.Button.new_with_label("Test")
        button.connect("clicked", self.on_click)

        self.label = Gtk.Label("This is a label")

        self.window_grid.attach(self.label, 0, 0, 1, 1)
        self.window_grid.attach(button, 0, 1, 2, 2)

        self.win.add(self.window_grid)

        self.win.connect("delete-event", Gtk.main_quit)
        self.win.show_all()

    def on_click(self, widget):
        # Change/update a label in the window_grid
        self.label.set_label('Label changed')
        label = Gtk.Label("Another label")
        self.window_grid.attach(label, 2, 1, 2, 2)
        self.win.show_all()


def main():
    app = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())

还要记住@andlabs 的评论,小部件默认是隐藏的。

【讨论】:

  • 这就是我们可以向窗口添加新小部件的方式,但是我们如何修改现有小部件(参见原始问题)?
  • @Billjoe 抱歉,我误解了您的要求。试试我编辑的代码。
  • 这适用于带有标签的这种情况,但我希望有一个更一般的情况。假设我有一个盒子,里面有一些变量,然后我更改了其中一个变量。现在我想更新盒子。我该怎么做?
  • @Billjoe 您如何解释该示例的“不一般”之处?是什么阻止了您对盒子使用这种方法?
  • @jku 在我的实际代码中,我有一个小部件,它使用图形工具显示图形,其中一个参数是“图形”g。一开始,我的 GUI 让这个小部件显示一个空白图表。然后通过一个 file_opener 小部件,我们选择一个文件并将其转换为一个新的“图形”。因此 g 发生了变化。因此,我将如何在图形小部件中显示新图形(我必须从这个新的“g”计算)而不完全替换窗口(删除旧窗口并使用这个更新的小部件创建一个新窗口)。我猜 set_label 似乎是一个特殊功能,因为它还会更新并显示标签...
【解决方案2】:

如前所述,您的代码逻辑不好。您必须尝试了解如何设计您的应用程序。无论如何,我已经设法让您的代码适合您的问题:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class Button(Gtk.Box):

    def __init__(self, message, label, window_grid):
        Gtk.Box.__init__(self, spacing=6)
        self.set_border_width(10)
        self.label = label
        self.window_grid = window_grid

        button = Gtk.Button.new_with_label(message)
        button.connect("clicked", self.on_click)
        self.pack_start(button, True, True, 0)

    def on_click(self, widget):
        # Change/update a label in the window_grid
        self.label.label.set_text("Changed the lable")
        # Add a new label to the window_grid
        new_label = LabelBox("New label")
        self.window_grid.attach(new_label, 0, 2, 1, 1)
        new_label.show_all()

class LabelBox(Gtk.Box):

    def __init__(self, message):
        Gtk.Box.__init__(self, spacing=6)
        self.set_border_width(10)
        self.label = Gtk.Label(message)
        self.pack_start(self.label, True, True, 0)

win = Gtk.Window()
window_grid = Gtk.Grid()

label = LabelBox("This is a label")
button = Button("Test", label, window_grid)

window_grid.attach(label, 0, 0, 1, 1)
window_grid.attach(button, 0, 1, 1, 1)

win.add(window_grid)
win.connect("destroy", Gtk.main_quit)
win.show_all()

Gtk.main()

与你的比较,看看你犯了哪些错误。无需将标签和按钮包裹在 GtkBox 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2014-12-28
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多