将数字时钟放入 pygtk 窗口中
你好,
我有一个带有 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 窗口的中心(在label2)(或空地)?
窗口代码:
#!/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
空地代码:
<?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>
谢谢!
Hello,
I have this code for a digital clock with 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()
The question is: ¿How I could put the TKinter.label centered in a pygtk window (in label2) (or glade)?
window code:
#!/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 code:
<?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>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没猜错的话,下面的代码可以解决这个问题:
If I got you right, here's the code that will do the trick:
我改变了字体的属性
I changed the attributes of fonts