私はいくつかのウィジェットを設定したウィンドウを持っていますが、ユーザー入力(たとえばボタンをクリック)した後でウィジェットの情報を更新したいと思っています。また、イベントハンドラでウィンドウに新しいウィジェットを追加できるようにしたい。 私の質問の下の簡単なバージョンで試してみました。明らかにそれは動作しません。ウィンドウ内のウィジェットを更新し、新しいウィジェットを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()
デフォルトでウィジェットは非表示になっています。 – andlabs