更新标签文本时出现问题
环境:
- 使用Glade3构建界面。
- 后端是使用 GTK+ Builder 库用 Python 编写的。
-
虽然我知道更新标签文本所需的方法(label.set_text("string")),但我在 python 代码中获取标签对象时遇到了问题。
这是我的代码的样子:
#!/usr/bin/python
# Filename: HelloPython.py
# Author: Andrew Hefley Carpenter
# Date: 18 August 2010
import sys
import gtk
class HelloPython:
def on_window_destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("HelloPython.xml")
self.window = builder.get_object("window")
builder.connect_signals(self)
def on_button1_clicked(self, widget):
print "Hello World!"
widget.set_label("Hello World!")
#I'd like to update
if __name__ == "__main__":
editor = HelloPython()
editor.window.show()
gtk.main()
最终目标:我想在回调到由“on_button1_clicked”处理的“Object Y”(在本例中为button1)之后使用它的set_text方法更新“Object X”
Environment:
- Built interface using Glade3.
- Backend is written in Python using the GTK+ Builder library.
-
Although I know the method I need to use to update a label's text (label.set_text("string")), I'm having trouble obtaining the label object in the python code.
Here's what my code looks like:
#!/usr/bin/python
# Filename: HelloPython.py
# Author: Andrew Hefley Carpenter
# Date: 18 August 2010
import sys
import gtk
class HelloPython:
def on_window_destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("HelloPython.xml")
self.window = builder.get_object("window")
builder.connect_signals(self)
def on_button1_clicked(self, widget):
print "Hello World!"
widget.set_label("Hello World!")
#I'd like to update
if __name__ == "__main__":
editor = HelloPython()
editor.window.show()
gtk.main()
End goal: I want to update "Object X" using it's set_text method after the callback to "Object Y" (in this case button1) as handled by "on_button1_clicked"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
on_button1_clicked 的 widget 参数是 gtk.Button,而不是 gtk.Label。 gtk.Button 有一个名为 set_label() 的便捷 API 方法。
仅当 Gtk.Button 的子级是 gtk.Label 时才有效。这是在 Glade-3 中创建新按钮时的默认设置,但如果您更改了按钮的内容,则这将不起作用,并且您需要引用 gtk.Label 小部件本身。
编辑(更新标签的代码):
The widget parameter to on_button1_clicked is a gtk.Button, not a gtk.Label. gtk.Button has a convenience api method called set_label().
This only works if the child of Gtk.Button is a gtk.Label. This is the default when creating a new button in Glade-3, but if you've changed the contents of the button, this will not work and you'll need a reference to the gtk.Label widget itself.
EDIT (code to update the label):