Gtk 如何在PyGObject中更新窗口小部件并向窗口添加新的小部件?
我有一个窗口设置了一些小部件,但我希望这些小部件的信息在一些用户输入(例如,点击按钮)后更新。此外,我希望事件处理程序能够向窗口添加新的小部件。 我附上了一份我的问题的以下简单版本的尝试。但显然,它不起作用Gtk 如何在PyGObject中更新窗口小部件并向窗口添加新的小部件?,gtk,pygobject,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, wind
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()
您的代码结构格式不正确。这个代码对我有用:
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 comment,小部件在默认情况下是隐藏的。您的代码结构格式不好。这个代码对我有用:
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 comment,小部件在默认情况下是隐藏的。如前所述,您的代码逻辑不好。您必须尝试了解如何设计应用程序。无论如何,我已经设法使您的代码能够满足您的问题:
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中。正如前面所说,您的代码逻辑不好。您必须尝试了解如何设计应用程序。无论如何,我已经设法使您的代码能够满足您的问题:
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中。小部件默认隐藏。小部件默认隐藏。这是我们如何向窗口添加新小部件的方法,但我们如何修改现有小部件(请参见原始问题)?@Billjoe抱歉,我误解了您的请求。试试我编辑过的代码。在这种情况下,这是一个标签,但我希望有一个更一般的情况。假设我有一个盒子,里面有一些变量,然后我改变其中一个变量。现在我想更新这个框。我该怎么做?@Billjoe你能解释一下这个例子中哪些不是“一般性的”吗?在我的实际代码中,我有一个小部件,它使用图形工具显示一个图形,其中一个参数是“图形”g。首先,我的GUI让这个小部件显示一个空白的图形。然后通过文件开启器小部件,我们选择一个文件并将其转换为一个新的“图形”。因此g发生了变化。因此,我如何在graph小部件中显示新的图形(我必须从这个新的“g”计算),而不完全替换窗口(删除旧窗口并使用这个更新的小部件创建一个新窗口)。我猜set_label似乎是一个特殊的功能,因为它会更新并显示标签……这就是我们如何向窗口添加新的小部件,但我们如何修改现有的小部件(参见原始问题)?@Billjoe抱歉,我误解了您的请求。试试我编辑过的代码。在这种情况下,这是一个标签,但我希望有一个更一般的情况。假设我有一个盒子,里面有一些变量,然后我改变其中一个变量。现在我想更新这个框。我该怎么做?@Billjoe你能解释一下这个例子中哪些不是“一般性的”吗?在我的实际代码中,我有一个小部件,它使用图形工具显示一个图形,其中一个参数是“图形”g。首先,我的GUI让这个小部件显示一个空白的图形。然后通过文件开启器小部件,我们选择一个文件并将其转换为一个新的“图形”。因此g发生了变化。因此,我如何在graph小部件中显示新的图形(我必须从这个新的“g”计算),而不完全替换窗口(删除旧窗口并使用这个更新的小部件创建一个新窗口)。我猜set_标签似乎是一个特殊的功能,因为它会更新并显示标签。。。