【问题标题】:How to get button text in PyGObject?如何在 PyGObject 中获取按钮文本?
【发布时间】:2017-10-16 19:20:53
【问题描述】:

如何在 GTK 中打印按钮文本?

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.button = Gtk.Button("Hello")
        self.button.connect('pressed',self.print_button_name)
        self.add(self.button)

    def print_button_name(self,widget):
        print(MainWindow.button.name)  # I want to print button text here

win = MainWindow()
win.show_all()
win.connect('delete-event',Gtk.main_quit)
Gtk.main()

我正在使用 python3 和 PyGObject,我想打印按钮文本。在这种情况下,按钮文本是“Hello”。

我该怎么做?

【问题讨论】:

    标签: python gtk3 pygobject


    【解决方案1】:

    您使用的是 MainWindow 类而不是实例属性。

    将回调方法改为:

    def print_button_name(self,widget):
        print(self.button.get_label())  # This will print correctly
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多