在python代码中创建的Window中更改字体

时间:2011-11-22 08:56:06

标签: python pygtk

在用python代码创建的Window中更改字体。

我有一些工作的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

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()

1 个答案:

答案 0 :(得分:1)

使用标记很可能会满足您的需求。首先,在标签上启用它。然后使用set_text

而不是set_markup
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)

作为参考:GTK使用的标记语言为Pango

相关问题