更新标签文本时出现问题

发布于 2024-09-14 10:58:14 字数 982 浏览 2 评论 0原文

环境:

  • 使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2024-09-21 10:58:14

on_button1_clicked 的 widget 参数是 gtk.Button,而不是 gtk.Label。 gtk.Button 有一个名为 set_label() 的便捷 API 方法。

仅当 Gtk.Button 的子级是 gtk.Label 时才有效。这是在 Glade-3 中创建新按钮时的默认设置,但如果您更改了按钮的内容,则这将不起作用,并且您需要引用 gtk.Label 小部件本身。

编辑(更新标签的代码):

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")
        self.label = builder.get_object("label1") # get reference to the label
        builder.connect_signals(self)   

    def on_button1_clicked(self, widget):
        #widget.set_label("Hello World!") this would set the button's text
        self.label.set_text("Hello World!") # this sets the label's text

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

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")
        self.label = builder.get_object("label1") # get reference to the label
        builder.connect_signals(self)   

    def on_button1_clicked(self, widget):
        #widget.set_label("Hello World!") this would set the button's text
        self.label.set_text("Hello World!") # this sets the label's text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文