如何使用简单的python在GUI中制作时钟?

时间:2017-10-11 20:02:22

标签: python simpy

import simpy 
from tkinter import *
import time

root = Tk()
time1 = ''
clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill=BOTH, expand=1)
def clock(env,tick):
    while True:
         print(env.now)
         yield env.timeout(tick())
def tick():
    time2 = time.strftime('%H:%M:%S')
    clock.config(text=time2)
    clock.after(200, tick)


env = simpy.Environment()
env.process(clock(env,tick()))
env.run(until=root.mainloop(  ))

但它给了我一个错误

文件" D:\ python \ guiclock.py",第18行,勾选 clock.config(text = time2)AttributeError:' function'对象没有属性' config'

2 个答案:

答案 0 :(得分:2)

你重新定义了时钟。首先,你说“时钟,你是一个标签。”后来你说,“时钟,你是一个功能。”。

答案 1 :(得分:0)

以下是我在tkinter制作时钟的方法:

from Tkinter import *
import simpy

SIM_TIME = 100

class Clock:
    def __init__(self, canvas, x1, y1, x2, y2, tag):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.canvas = canvas
        self.train = canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2, fill="#fff")
        self.time = canvas.create_text(((self.x2 - self.x1)/2 + self.x1), ((self.y2 - self.y1)/2 + self.y1), text = "Time = "+str(tag)+"s")
        self.canvas.update()

    def tick(self, tag):
        self.canvas.delete(self.time)
        self.time = canvas.create_text(((self.x2 - self.x1)/2 + self.x1), ((self.y2 - self.y1)/2 + self.y1), text = "Time = "+str(tag)+"s")
        self.canvas.update()

def create_clock(env):
    clock = Clock(canvas, 500,225,700,265, env.now)
    while True:
        yield env.timeout(1)
        clock.tick(env.now)

animation = Tk()
im = PhotoImage(file="image.gif")
canvas = Canvas(animation, width = 800, height = 310)
canvas.create_image(0,0, anchor=NW, image=im)
animation.title("Sim Title")
canvas.pack()


env = simpy.Environment()
env.process(create_clock(env))
env.run(until=SIM_TIME)
相关问题