python - 更改 Gtk.Button 标签的样式/字体

标签 python python-3.x gtk gtk3

环境:Python 3.4.3,GTK+ 3

嘿,
我正在尝试使用 Gtk.ToggleButton 编写一个 Gtk 应用程序,能够更改其标签的字体。这是我的例子,我想让标签“事件”,当你点击按钮时,它是 .

from gi.repository import Gtk

class TOGGLE_WINDOW:

    def delete_event(self, widget, event, data=None):
        print('delete event occurred')
        return False

    def destroy(self, widget, data=None):
        Gtk.main_quit()

    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_title('Toggle Button Font Change')
        self.window.connect('delete_event', self.delete_event)
        self.window.connect('destroy', self.destroy)

        self.box = Gtk.HBox()
        self.box.set_size_request(400, 50)

        self.toggle = Gtk.ToggleButton(label = 'inactive')
        self.toggle.connect('toggled', self.on_toggled, 'toggle')

        self.box.pack_start(self.toggle, True, True, 0)
        self.window.add(self.box)

        self.window.show_all()

    def on_toggled(self, event, widget):
        state = self.toggle.get_active()

        if state == True:
            self.toggle.set_label('active')
        else:
            self.toggle.set_label('inactive')

    def main(self):
        Gtk.main()

if __name__=='__main__':
    run = TOGGLE_WINDOW()
    run.main()

当我尝试使用外部 Gtk.Label 来放置按钮时,Python 说:
类型错误:必须是字符串,而不是标签。
在这种情况下,函数如下所示:
def on_toggled(self, event, widget):
    state = self.toggle.get_active()

    self.label = Gtk.Label()
    self.label.set_markup('<b>active</b>')

    if state == True:
      self.toggle.set_label(self.label)
    else:
      self.toggle.set_label('inactive')

似乎 Buttonlabel 只接受字符串。
我也想过将文本转换为图片并将其放在按钮上,但应该有更简单的方法......

谢谢你的想法!

最佳答案

好的,感谢 @andlabs我现在得到了正确的代码。
我可以使用 get_child() 获取 Gtk.Button 的标签,然后使用 set_markup() 和类似 html 的样式配置:

from gi.repository import Gtk

class TOGGLE_WINDOW:

    def delete_event(self, widget, event, data=None):
        print('delete event occurred')
        return False

    def destroy(self, widget, data=None):
        Gtk.main_quit()

    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_title('Toggle Button Font Change')
        self.window.connect('delete_event', self.delete_event)
        self.window.connect('destroy', self.destroy)

        self.box = Gtk.HBox()
        self.box.set_size_request(400, 50)

        self.toggle = Gtk.ToggleButton(label = 'inactive')
        self.toggle.connect('toggled', self.on_toggled, 'toggle')

        self.box.pack_start(self.toggle, True, True, 0)
        self.window.add(self.box)

        self.window.show_all()

    def on_toggled(self, event, widget):
        state = self.toggle.get_active()

        if state == True:
            # here I get the Label and set its markup
            self.label = self.toggle.get_child()
            self.label.set_markup('<b>active</b>')  
        else:
            self.toggle.set_label('inactive')

    def main(self):
        Gtk.main()

if __name__=='__main__':
    run = TOGGLE_WINDOW()
    run.main()

感谢您的快速帮助!

关于python - 更改 Gtk.Button 标签的样式/字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251937/

相关文章:

python - 如何通过长时间运行的插槽保持 UI 响应

python-3.x - 在Windows 10上的Docker Toolbox上运行Splash

python - pandas 在每个组中找到满足特定条件的行的索引并为这些行分配值

python-3.x - 将 Pandas 数据帧保存到 S3 的最快方法是什么?

c++ - 检索 Gtk::Widget 的相对位置:get_allocation() 不起作用

c++ - 如何从 python SWIG 包装器向下转换 c++ 对象?

python - selenium - 无法在 'evaluate' : The string is not a valid XPath expression 上执行 'Document'

python - 在使用 Glade 创建的 Python GTK3 程序中导入 CSS 文件

python - 在 gtk3+ for python 中如何获取选定的菜单项或选定的菜单项的索引?

python - 将 C 代码转换为 Python 时语法无效