如何在窗口中更新窗口小部件并在PyGObject中向窗口添加新窗口小部件?

时间:2017-06-27 21:58:17

标签: gtk pygobject

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

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()

2 个答案:

答案 0 :(得分:0)

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

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注释,默认情况下隐藏小部件。

答案 1 :(得分:0)

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

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中。