TKinter应用程序崩溃问题

时间:2016-05-25 17:41:58

标签: python timer tkinter

我运行的程序在计时器运行时出现问题。每次“开始”命中时,应用程序崩溃。有什么想法吗?

##-- Imports --##

import time
import openpyxl as xl
from Tkinter import *

##-- Classes --##

class App(Frame):

    def startTimer(self):
        self.check = True
        self.now = time.strftime("%H:%M:%S")

        while self.check == True:
            self.timer.configure(text = self.now)
            time.sleep(1)

    def initUI(self):
        self.parent.title("Emma's Time Manager")
        self.pack(fill = BOTH, expand = 1)

    def initWidget(self):

        ##Create button definitions##
        self.buttonwidth = 12
        self.quit = Button(self, text = "Quit", comman = self.quit, width = self.buttonwidth)
        self.newClient = Button(self, text = "Add Client", command = lambda:self.newClientFunc(), width = self.buttonwidth)
        self.timeStart = Button(self, text = "Start", command = lambda:self.startTimer(), width = self.buttonwidth)
        self.timeEnd = Button(self, text = "End", command = lambda:self.endTimer(), width = self.buttonwidth)
        self.saveBut = Button(self, text = "Save", command = lambda:self.saveFunc(), width = self.buttonwidth)
        self.viewClient = Button(self, text = "View Client", command = lambda:self.viewCliFunc(), width = self.buttonwidth)


        ##Create lable definitions##
        self.timer = Label(self, text = "00:00:00") ##self.timer used as display for timer##

        ##Create the listbox for Client Selection##
        self.wb = xl.load_workbook("clients.xlsx")
        self.clientNames = self.wb.get_sheet_names()
        self.clivar = StringVar(self)
        self.clivar.set(self.clientNames[0])
        self.clilist = apply(OptionMenu, (self, self.clivar) + tuple(self.clientNames))

        ##Create Entry Box to describe work information##
        self.info = Entry(self, width = 50)


        ##Create GUI for widgets##
        self.clilist.grid(row = 0, column = 0)
        self.timer.grid(row = 0, column = 1)
        self.timeStart.grid(row = 0, column = 2)
        self.timeEnd.grid(row = 0, column = 3)
        self.info.grid(row = 1, column = 0, columnspan = 4)
        self.newClient.grid(row = 2, column = 0)
        self.viewClient.grid(row = 2, column = 1)
        self.saveBut.grid(row = 2, column = 2)
        self.quit.grid(row = 2, column = 3)


    def __init__(self, parent):
        Frame.__init__(self, parent, background = "light blue")
        self.parent = parent
        self.initUI()
        self.initWidget()

def main():
    try:
        xl.load_workbook("clients.xlsx")
    except:
        temp = xl.Workbook()
        temp.save("clients.xlsx")
        temp.remove_sheet(temp.get_sheet_by_name("Sheet"))

    root = Tk()     
    bob = App(root)
    root.mainloop()

main()

请注意,大部分程序尚未完成。我似乎无法让这个计时器正常运行。

1 个答案:

答案 0 :(得分:0)

看起来你没有办法摆脱while循环。您需要将self.check设置为False,或将break设置为循环。

while self.check == True:
    self.timer.configure(text = self.now)
    time.sleep(1)