简体   繁体   English

python Gtk3 - 将按钮的 label 设置为默认值 None

[英]python Gtk3 - Set label of button to default value of None

I am trying to reset a label of a button to its initial (default) value of None , which does not work as expected.我正在尝试将按钮的 label 重置为其初始(默认)值None ,这无法按预期工作。 Here's the minimal example:这是最小的例子:

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


class GUI(Gtk.Window):

    def __init__(self):
        super().__init__()
        self.connect('destroy', Gtk.main_quit)
        self.set_position(Gtk.WindowPosition.CENTER)

        self.grid = Gtk.Grid(hexpand=True, vexpand=True)
        self.add(self.grid)
        button = Gtk.Button(hexpand=True, vexpand=True)
        self.grid.attach(button, 0, 0, 1, 1)
        button.connect('clicked', self.on_button_clicked)

    def on_button_clicked(self, button: Gtk.Button) -> None:
        print(label := button.get_label(), type(label))
        button.set_label(label)


def main() -> None:

    win = GUI()
    win.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()

Result:结果:

$ python example.py 
None <class 'NoneType'>
Traceback (most recent call last):
  File "/home/neumann/example.py", line 21, in on_button_clicked
    button.set_label(label)
TypeError: Argument 1 does not allow None as a value

What's the correct way to do this?执行此操作的正确方法是什么?

Note: Before somebody suggests it: I do not want to set the label to an empty string, since that will change the size of the button, which is noticeable on a larger grid of buttons:注意:在有人建议之前:我不想将 label 设置为空字符串,因为这会改变按钮的大小,这在较大的按钮网格上很明显:

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


class GUI(Gtk.Window):

    def __init__(self):
        super().__init__()
        self.connect('destroy', Gtk.main_quit)
        self.set_position(Gtk.WindowPosition.CENTER)

        self.grid = Gtk.Grid(hexpand=True, vexpand=True)
        self.add(self.grid)
        
        for x in range(3):
            button = Gtk.Button(hexpand=True, vexpand=True)
            button.connect('clicked', self.on_button_clicked)
            self.grid.attach(button, x, 0, 1, 1)

    def on_button_clicked(self, button: Gtk.Button) -> None:
        print(label := button.get_label(), type(label))
        button.set_label('')


def main() -> None:

    win = GUI()
    win.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()

I'm not sure what your use case is, but you can try adding a GtkLabel child and set the string there:我不确定你的用例是什么,但你可以尝试添加一个GtkLabel孩子并在那里设置字符串:

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


class GUI(Gtk.Window):

    def __init__(self):
        super().__init__()
        self.connect('destroy', Gtk.main_quit)
        self.set_position(Gtk.WindowPosition.CENTER)

        self.grid = Gtk.Grid(hexpand=True, vexpand=True)
        self.add(self.grid)

        for x in range(3):
            button = Gtk.Button(hexpand=True, vexpand=True)
            label = Gtk.Label()
            button.add(label)
            button.connect('clicked', self.on_button_clicked)
            self.grid.attach(button, x, 0, 1, 1)

    def on_button_clicked(self, button: Gtk.Button) -> None:
        label = button.get_child()
        print(text := label.get_label(), type(text))
        label.set_label('')
        # or hide it if you want
        # label.hide()


def main() -> None:

    win = GUI()
    win.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()

GtkButton may be creating the internal GtkLabel child only when a label is set (which should be a valid string). GtkButton可能仅在设置label时才创建内部GtkLabel子项(这应该是一个有效的字符串)。 And since the hexpand and vexpand for the GtkButton are set to True , they may be getting propagated to the internal GtkLabel .由于GtkButtonhexpandvexpand设置为True ,它们可能会传播到内部GtkLabel

If you simply want all the buttons to have same width and height, you may only need grid.set_row_homogeneous() and grid.set_column_homogeneous()如果您只是希望所有按钮具有相同的宽度和高度,您可能只需要grid.set_row_homogeneous()grid.set_column_homogeneous()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM