在python代码中创建的窗口中更改字体
在python代码中创建的窗口中更改字体 我有一些可以工作的python代码,在窗口中显示MMDDYYYY HH:MM:ss。我想在日期和时间上方添加额外的文本,同时增加字体大小(数据和时间MMDDYYYY HH:MM:ss)。我试图添加不同的文本和字体大小,但这会阻止我的代码工作在python代码中创建的窗口中更改字体,python,pygtk,Python,Pygtk,在python代码中创建的窗口中更改字体 我有一些可以工作的python代码,在窗口中显示MMDDYYYY HH:MM:ss。我想在日期和时间上方添加额外的文本,同时增加字体大小(数据和时间MMDDYYYY HH:MM:ss)。我试图添加不同的文本和字体大小,但这会阻止我的代码工作 #!/usr/bin/env python # -*- coding: utf-8 -*- import pygtk pygtk.require('2.0') import gtk import time cl
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygtk
pygtk.require('2.0')
import gtk
import time
class Clock:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", lambda w: gtk.main_quit())
window.set_title("mmddyyyyhhmmss-Clock")
self.label = gtk.Label()
window.add(self.label)
window.set_border_width(10)
window.show_all ()
def update(self):
self.label.set_text(time.strftime('%m/%d/%Y-%H%M%S'))
return True #needed to keep the update method in the schedule
def main():
gtk.main()
if __name__ == "__main__":
clock = Clock()
gtk.timeout_add(200, clock.update) #add to the main loop scheduled tasks
main()
使用标记很可能会满足您的需要。首先,在标签上启用它。然后使用
设置标记
,而不是使用设置文本
:
self.label.set_use_markup(True)
t = time.strftime('%m/%d/%Y-%H%M%S')
self.label.set_markup('<span size="18000">The time:</span> ' + t)
self.label.set\u use\u标记(True)
t=time.strftime(“%m/%d/%Y-%H%m%S”)
self.label.set_标记('time:'+t)
作为参考:GTK使用的标记语言是。我添加了您的建议,但似乎没有任何东西改变我做错了什么?def update(self):self.label.set#u use_markup(True)t=time.strftime(“%m/%d/%Y-%H%m%S”)self.label.set#u markup(“%The time:”+t)return True#需要将update方法保留在scheduleforone中,字体大小太小(由于Pango使用了千分之一的点数,将其更改为18000)。这应该行得通,实际上。。。我再看一遍,上面是正确的。您可以尝试使用不同的标签(例如,将
span
替换为b
)。或者,将set\u markup
函数放在\u init\u\u
中:更新可能有问题?set\u text
也应该在执行标签后使用标记。set\u使用标记(True)
感谢您使用