数字时钟在python 3和tkinter中的状态栏中

时间:2013-03-28 18:36:41

标签: python tkinter statusbar clock

我想放这个数字时钟:

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)

有办法吗? 感谢所有想要帮助的人,我很感激:)

3 个答案:

答案 0 :(得分:2)

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

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

另外,请记住对Python 3.0使用Tkinter(Capital T),对Python 3.0使用tkinter(小写t)。

答案 1 :(得分: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()

答案 2 :(得分: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(  )