0

我想把这个数字时钟:

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)

有没有办法做到这一点?感谢所有想要帮助的人,我很感激:)

4

3 回答 3

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 2.7 中使用Tkinter(Capital T),tkinter在 Python 3.0 中使用 (小写 t)。

于 2014-09-24T10:00:37.357 回答
0

Tkinter noob here,但我认为您不能将时钟标签放在状态标签内。但是,您可以将它们并排放置:

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()
于 2013-03-28T19:40:17.043 回答
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(  )
于 2013-03-28T20:15:44.397 回答