导致窗口崩溃的Tkinter时钟

时间:2018-04-06 01:44:05

标签: python-3.x time tkinter clock

我的tkinter窗口中的时钟导致整个窗口崩溃。我知道这一点,因为当我评论它时,它完美无缺。有什么方法可以调整或重组代码以防止崩溃?崩溃通常发生在运行时5-10秒。

以下是整个计划

时钟/日期变量和update_clock()函数是有问题的。

from tkinter import *
import tkinter as tk
from PIL import Image, ImageTk
import time


def dateconverter():
    global today
    if today[0].lower() == '0' and today[1].lower() == '1':
        today = 'January'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '2':
        today = 'February'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '3':
        today = 'March'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '4':
        today = 'April'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '5':
        today = 'May'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '6':
        today = 'June'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '7':
        today = 'July'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '8':
        today = 'August'+' '+time.strftime("%d")
    if today[0].lower() == '0' and today[1].lower() == '9':
        today = 'September'+' '+time.strftime("%d")
    if today[0].lower() == '1' and today[1].lower() == '0':
        today = 'October'+' '+time.strftime("%d")
    if today[0].lower() == '1' and today[1].lower() == '1':
        today = 'November'+' '+time.strftime("%d")
    if today[0].lower() == '1' and today[1].lower() == '2':
        today = 'December'+' '+time.strftime("%d")

    return today

class Window(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.master = master
        self.init_window()
    def init_window(self):
        self.master.title('Car Display')
        self.pack(fill=BOTH,expand=1)
        load = Image.open('/Users/jakeglunk/Desktop/928BG_Vert.png')
        render = ImageTk.PhotoImage(load)
        img = Label(self, image=render)
        img.image = render
        img.place(x=-2,y=-2)
        batteryimg = ''
        battery = Label(self, image=batteryimg, highlightthickness = 0, bd = 0, width = 0)
        battery.image = batteryimg
        battery.place(x=1000,y=30)
        global percentage
        percentage = 12
        battlevel = Label(self, text=str(percentage)+'%', bg='#242424', font= ("System",40), fg='white')
        battlevel.place(x=900,y=35)

    def battery_window():
        battwindow = tk.Toplevel(root)
        load2 = Image.open('/Users/jakeglunk/Desktop/928Battery_Vert.png')
        render2 = ImageTk.PhotoImage(load2)
        img2 = Label(battwindow, image=render2)
        img2.image = render2
        img2.pack(fill=BOTH,expand=1)
        battwindow.geometry('1080x1920')
        battwindow.overrideredirect(1)

    def motor_window():
        battwindow = tk.Toplevel(root)
        load2 = Image.open('/Users/jakeglunk/Desktop/928Motor_Vert.png')
        render2 = ImageTk.PhotoImage(load2)
        img2 = Label(battwindow, image=render2)
        img2.image = render2
        img2.pack(fill=BOTH,expand=1)
        battwindow.geometry('1080x1920')
        battwindow.overrideredirect(1)

    clock = Label(self, text='time', bg='#242424', font = ("System", 55), fg='white')
    clock.place(x=2,y=0)
    date = Label(self, text= 'date', bg= '#242424', font = ("System", 40), fg='white')
    date.place(x=2,y=65)

    battbuttimg = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/BattButtx264.png'))
    battbutton = Button(self, image=battbuttimg, bg = '#4d4d4d',command=battery_window)
    battbutton.image = battbuttimg
    battbutton.place(x=408,y=305)

    batttext = Label(self, text='Battery', bg='#4d4d4d', font = ("System", 50), fg='white')
    batttext.place(x=458,y=585)

    mapbuttimg = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/Mapsx264.png'))
    mapsbutt =  Button(self, image=mapbuttimg)
    mapsbutt.image = mapbuttimg
    mapsbutt.place(x=72,y=305)

    mapstext = Label(self, text='Maps', bg='#4d4d4d', font = ("System", 50), fg='white')
    mapstext.place(x=140,y=585)

    motorbuttimg = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/MotorButtx264.png'))
    motorbutt = Button(self, image=motorbuttimg, command=motor_window)
    motorbutt.image = motorbuttimg
    motorbutt.place(x=744,y=305)

    motortext = Label(self, text='Motor', bg='#4d4d4d', font = ("System", 50), fg='white')
    motortext.place(x=810,y=585)


    def update_clock():
        now = time.strftime("%H:%M")
        clock.configure(text=now)
        clock.after(1000, update_clock)
        global today
        today = time.strftime("%m/%d")
        dateconverter()
        date.configure(text=today)
        date.after(1000, update_clock)
    update_clock()

    def batterymeter():
        global percentage
        lowbattery = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/LowBatt.png'))
        halfbattery = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/HalfBatt.png'))
        mediumbattery = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/MedBatt.png'))
        fullbattery = ImageTk.PhotoImage(Image.open('/Users/jakeglunk/Desktop/FullBatt.png'))
        if percentage <= 25:
            battery.configure(image=lowbattery)
            battery.image = lowbattery
        if percentage <= 50 and percentage > 25:
            battery.configure(image=halfbattery)
            battery.image = halfbattery
        if percentage <= 75 and percentage > 50:
            battery.configure(image=mediumbattery)
            battery.image = mediumbattery
        if percentage > 75:
            battery.configure(image=fullbattery)
            battery.image = fullbattery
        battery.after(1000, batterymeter)
    batterymeter()


root = Tk()
root.geometry('1080x1920')
root.overrideredirect(1)
app = Window(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

我不知道这是否是唯一的问题,但有一个问题是您从update_clock开始两次调用update_clock。你打电话给它一次,它会在&#34;&#34;之后添加两个工作。队列。一项工作在运行时就会结束,并且还会增加两项工作。现在有三个。接下来又增加了两个,所以有四个。一个是关闭但你再添加两个,所以有五个,依此类推,然后每秒发生两次。