[英]Change Style / Font of the Label of a Gtk.Button
Environment: Python 3.4.3, GTK+ 3 环境:Python 3.4.3,GTK + 3
Hey there, I am trying to write a Gtk-application with a Gtk.ToggleButton beeing able to change the font of his Label. 嘿,我正在尝试使用Gtk.ToggleButton编写一个Gtk应用程序,以便能够更改其Label的字体。 Here is my example, I would like to make the Label "active", which you get, when clicking the button, to be strong .
这是我的示例,我想使Label为“ active”,当您单击按钮时,它会变得很牢固 。
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()
When I try to use an external Gtk.Label to put on the Button Python says: TypeError: Must be string, not Label . 当我尝试使用外部Gtk.Label放置按钮时,Python说: TypeError:必须为字符串,而不是Label 。 In this case the function looks like this:
在这种情况下,函数如下所示:
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')
Seems, like the Buttonlabel just accepts strings. 似乎像Buttonlabel只是接受字符串。 I also thought about converting a text to a picture and put this on the Button, but there should be an easier way...
我还考虑过将文本转换为图片并将其放在Button上,但是应该有一种更简单的方法...
Thank you for ideas! 谢谢你的想法!
Okay, thanks to @andlabs I got the right code now. 好的,感谢@andlabs,现在我得到了正确的代码。 I can get the label of the Gtk.Button by using get_child() and then use set_markup() with html-like styleconfigurations:
我可以通过使用get_child()获得Gtk.Button的标签,然后将set_markup()与类似html的styleconfiguration一起使用:
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()
Thank you for your fast help! 感谢您的快速帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.