您好
我有这个代码用于TKinter的数字时钟:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter
import time
def tick(time_old, clock):
# get the current local time from the PC
time_now = time.strftime('%H:%M:%S')
# if time string has changed, update it
if time_now != time_old:
time_old = time_now
clock.config(text = time_now)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
clock.after(200, tick, time_old, clock)
def main():
root = Tkinter.Tk()
clock = Tkinter.Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill=Tkinter.BOTH, expand=1)
tick("", clock)
root.mainloop()
if __name__ == "__main__":
main()
问题是:¿我如何将TKinter.label置于pygtk窗口(标签2)(或林间空地)中心?
窗口代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
class Principal:
def __init__(self):
self.gladefile = "ejemplo.glade"
self.glade = gtk.Builder()
self.glade.add_from_file(self.gladefile)
label2 = self.glade.get_object("label2")
window = self.glade.get_object("ventana")
window.set_keep_above(True)
window.show_all()
self.glade.connect_signals(self)
def on_ventana_delete_event(self, widget, event):
gtk.main_quit()
def on_boton_clicked(self, widget):
self.glade.get_widget('label1').set_text('change title label')
def on_boton2_clicked(self, widget):
gtk.main_quit()
if __name__ == "__main__":
try:
a = Principal()
gtk.main()
except KeyboardInterrupt:
pass
Glade代码:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="ventana">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Ventana Principal</property>
<property name="resizable">False</property>
<property name="window_position">center-always</property>
<property name="default_width">200</property>
<property name="default_height">100</property>
<property name="icon">Walker.png</property>
<signal name="delete-event" handler="on_ventana_delete_event" swapped="no"/>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="ypad">3</property>
<property name="label" translatable="yes">Acción</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="boton">
<property name="label" translatable="yes">Cambiar label</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<property name="xalign">0.54000002145767212</property>
<signal name="clicked" handler="on_boton_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="boton2">
<property name="label" translatable="yes">Salir</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="on_boton2_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
谢谢!
答案 0 :(得分:4)
如果我找到了你,那么这就是代码:
#!/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("Clock")
self.label = gtk.Label()
window.add(self.label)
window.set_border_width(25)
window.show_all ()
def update(self):
self.label.set_text(time.strftime('%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)
我更改了字体的属性
def update(self):
self.label.set_markup("<span font_family='arial'weight='heavy'><big>"
+ time.strftime('%H:%M:%S') +
"</big></span>")
return True #needed to keep the update method in the schedule