Search code examples
pythontkinterartificial-intelligenceexecutionassistant

Tkinter window will update after the code has finished


When I run the code the window will appear after the code finished and if I add the main loop at the start the code won't run until I close the window. I want the window to update every time I add a label variable in my code. I searched on multiple docs but they all seem to give the same answer and it did not work.

import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import tkinter
import speech_recognition as sr
from notifypy import Notify

window = tkinter.Tk()
window.title("GUI")
window.geometry('500x500')



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

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

def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<=12:
        speak("good morning sir")
        lab1 = tkinter.Label(window,text="Good morning sir").pack()
    elif hour>=12 and hour<=18:
        speak("good afternoon sir")
        lab2 = tkinter.Label(window,text="Good afternoon sir").pack()
    elif hour>=18 and hour<=22:
        speak("good evening sir")
        lab3 = tkinter.Label(window,text="Good evening sir").pack()
    else:
        speak("good night sir")
        lab4 = tkinter.Label(window,text="Good night sir").pack()
    lab5 = tkinter.Label(window,text="I am D bot,how may I help you").pack()
    speak("I am D bot,how may I help you")

def takecommand():
    r = sr.Recognizer()
    with sr.Microphone() as sourse:
        lab6 = tkinter.Label(window,text="listning...").pack()
        r.pause_threshold = 1
        audio = r.listen(sourse)
    try:
        lab7 = tkinter.Label(window,text="recognizing...").pack()
        query = r.recognize_google(audio,language='en-in')
        # print(query)
        lab8 = tkinter.Label(window,text=query).pack()
    except Exception as e:
        lab9 = tkinter.Label(window,text="Good morning").pack()
        lab10 = tkinter.Label(window,text="say that again please").pack()
        speak("say that again please")
        takecommand().lower
        return "none"
    return query

def wiki():
    # if 'wikipedia'  in query:
            lab11 = tkinter.Label(window,text="searching wikipedia").pack()
            speak('searching wikipedia...')
            results = wikipedia.summary(query, sentences=2)
            lab12 = tkinter.Label(window,text="according to wikipedia").pack()
            speak("according to wikipedia")
            # print(results)
            lab13 = tkinter.Label(window,text=results).pack()
            speak(results)
            lab14 = tkinter.Label(window,text="check the notification for more details").pack()
            speak('check the notification for more details')
            notification = Notify()
            notification.title = "check out this website for more details"
            notification.message = 'https://en.wikipedia.org/wiki/Main_Page'
            notification.icon='G:\code projects\python\D bot\drone_115355.ico'
            notification.application_name="D bot"
            notification.send()


if __name__=="__main__":
    wishme()
    while True:
    # if 1:
        query = takecommand().lower()
        # query = "play music"

        if 'open youtube' in  query:
            webbrowser.get(using=None).open_new_tab("https://youtube.com/")
# C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe
        # elif "close" in query:
            # break
        elif 'open amazon' in  query:
            webbrowser.get(using=None).open_new_tab("https://www.amazon.com/")
        elif 'open gmail' in  query:
            webbrowser.get(using=None).open_new_tab("https://mail.google.com/mail/u/0/#inbox")
        elif 'open google' in  query:
            google = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
        elif 'open chrome' in  query:
            google = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
            os.startfile(google)
        elif 'open stack overflow' in  query:
            webbrowser.get(using=None).open_new_tab("https://stackoverflow.com/")
        elif "what's the time" in query:
            strtime = datetime.datetime.now().strftime('%H:%M:%S')
            # print('the time is '+strtime)
            lab15 = tkinter.Label(window,text="Hello sir,nice to meet you,how may i help you"+strtime).pack()
            speak('the time is '+strtime)
window.mainloop()

I am so sorry the mainloop was not shown in the code. I have now edited the code.


Solution

  • all you need to do is add these two functions every time you want the window to update

            window.update_idletasks()
            window.update()