2013-03-28 312 views
0

我希望把这个数字时钟:数字时钟在Python 3和Tkinter的

import sys  
from tkinter import * 
import time 

root = Tk() 
time1 = '' 
clock = Label(root, font=('times', 20, 'bold'), bg='green') 
clock.pack(fill=BOTH, expand=1) 

def tick(): 
    global time1 
    # get the current local time from the PC 
    time2 = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    if time2 != time1: 
     time1 = time2 
     clock.config(text=time2) 
     # calls itself every 200 milliseconds 
     # to update the time display as needed 
     # could use >200 ms, but display gets jerky 
    clock.after(200, tick) 

tick() 
root.mainloop() 

在此状态栏:

status = Label(mGui, text="v1.0", bd=1, relief=SUNKEN, anchor=W) 
status.pack(side=BOTTOM, fill=X) 

有没有办法做到这一点? 谢谢大家谁想要帮助,我感谢它:)

+0

可能的重复[如何使用tkinter创建一个计时器](http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter) – nbro

回答

0

Tkinter noob在这里,但我不认为你可以把时钟标签内的状态标签。不过您可以通过侧把它们放在一起:

import sys  
from tkinter import * 
import time 

def tick(): 
    global time1 
    # get the current local time from the PC 
    time2 = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    if time2 != time1: 
     time1 = time2 
     clock.config(text=time2) 
     # calls itself every 200 milliseconds 
     # to update the time display as needed 
     # could use >200 ms, but display gets jerky 
    clock.after(200, tick) 

root = Tk() 
time1 = '' 

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W) 
status.grid(row=0, column=0) 

clock = Label(root, font=('times', 20, 'bold'), bg='green') 
clock.grid(row=0, column=1) 

tick() 
root.mainloop() 
+0

为什么你选择更新每一个200ms的?这是一个绝对的确定性,时间每秒只会改变一次,因为你只能改变到秒的粒度。 –

0

通常情况下,我做一个状态出来的帧,然后收拾我想在框架中显示力所能及的事情。例如,您的时钟可能打包在右侧,您的状态标签可能打包在左侧。然后,您可以将整个状态栏框放在GUI的底部。

通常我宁愿给使用面向对象式的例子,但这里有适合你的问题从代码的例子:

import sys  
from tkinter import * 
import time 

root = Tk() 

statusbar = Frame(root) 
statusbar.pack(side="bottom", fill="x", expand=False) 

time1 = '' 
clock = Label(root, font=('times', 20, 'bold'), bg='green') 

def tick(): 
    global time1 
    # get the current local time from the PC 
    time2 = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    if time2 != time1: 
     time1 = time2 
     clock.config(text=time2) 
     # calls itself every 200 milliseconds 
     # to update the time display as needed 
     # could use >200 ms, but display gets jerky 
    clock.after(200, tick) 

tick() 

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W) 
status.pack(in_=statusbar, side=LEFT, fill=BOTH, expand=True) 
clock.pack(in_=statusbar, side=RIGHT, fill=Y, expand=False) 

root.mainloop() 
+0

好的,谢谢你的回答,你知道任何其他(更好)的方式把时钟放在状态栏的角落吗? – TheQuiteStupidMan

+0

@ user2221042:你如何定义“更好”?这个解决方案有什么问题? –

+0

我的意思是更优雅的解决方案与其他时钟...目前我想让状态栏看起来像在Windows操作系统,但这将工作得很好,我需要;) – TheQuiteStupidMan

1

什么是if语句?这是没有必要的,因为clock.after语句在clock.after()函数中直接调用tick()函数,它将更新您的时间字符串。

import sys  
from Tkinter import * 
import time 

def tick(): 
    # get the current local time from the PC 
    time_string = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    clock.config(text=time_string) 
    clock.after(200, tick) 

root = Tk() 
clock = Label(root, font=('times', 20, 'bold'), bg='green') 
clock.grid(row=0, column=1) 
tick() 
root.mainloop() 

此外,请记住使用Tkinter(首都T)为Python 2.7和tkinter(小写吨)用于Python 3.0。