运行程序时GUI窗口崩溃?

时间:2020-06-10 07:04:58

标签: python tkinter

我使用过Tkinter,如果我运行该程序,则会在空白屏幕上打开窗口,并在几秒钟内显示python已经停止工作。但是,如果我运行任何其他简单的tkinter项目,它将运行完美。我已经发布了部分代码,请说是否有其他替代方法。

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import smtplib
import pyaudio
import os
from tkinter import *
import random
import roman



MASTER='Kabi'

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)

window=Tk()

global var
global var1

var=StringVar()
var1=StringVar()

var.set('Starting Jarvis...')
window.update()

def speak(text):
    engine.say(text)
    engine.runAndWait()

def wish():
    hour=int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak('good morning '+MASTER)
    elif hour>=12 and  hour<16:
        speak('Good afternoon '+MASTER)
    elif hour>=16 and hour<19:
        speak('Good evening '+MASTER)
    else:
        speak("Good night "+MASTER)
    speak('I am Jarvis your ai assistant, How may i help you?')

#main program starts
def command():
    r=sr.Recognizer()
    with sr.Microphone() as source:
        speak('Listening...')
        audio=r.listen(source)
    try :
        query=r.recognize_google(audio, language='en-in')
        var1.set(f'User:{query}\n')
        window.update()
    except Exception as ex:
        speak(MASTER+ 'Can you say that again please?')
        query=None
    return query

speak("Starting Jarvis...")
wish()
query=command()

if 'wikipedia' in query.lower():
    speak('Searching wikipedia...')
    query=query.replace("wikipedia" , '')
    results=wikipedia.summary(query , sentences=2)
    var.set(results)
    window.update()
    speak(results)
    query = command()

elif 'the time' in query.lower():
    strtime=datetime.datetime.now().strftime('%H:%M:%S')
    var.set(f'{MASTER} the tiime is {strtime}')
    window.update()
    speak(f'{MASTER} the tiime is {strtime}')
    query = command()

elif 'exit' in query.lower():
    exit()
else:
    speak('Sorry I can not understand, Can yu say that again?')

    query=command()

def update(ind):
    frame = frames[(ind)%100]
    ind += 1
    label.configure(image=frame)
    window.after(100, update, ind)

label2 = Label(window, textvariable = var1, bg = '#FAB60C')
label2.config(font=("Courier", 20))
var1.set('User Said:')
label2.pack()

label1 = Label(window, textvariable = var, bg = '#ADD8E6')
label1.config(font=("Courier", 20))
var.set('Welcome')
label1.pack()

frames = [PhotoImage(file='Assistant.gif',format = 'gif -index %i' %(i)) for i in range(100)]
window.title('JARVIS')

label = Label(window, width = 500, height = 500)
label.pack()
window.after(0, update, 0)

window.mainloop()

0 个答案:

没有答案
相关问题