将数字时钟放入 pygtk 窗口中

发布于 2024-11-27 11:28:49 字数 5919 浏览 2 评论 0原文

你好,

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-12-04 11:28:49

如果我没猜错的话,下面的代码可以解决这个问题:

#!/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()

If I got you right, here's the code that will do the trick:

#!/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()
清旖 2024-12-04 11:28:49

我改变了字体的属性

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

I changed the attributes of fonts

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文