Why my tkinter code is not executing before my ttsx3 code

56 views Asked by At

here is my code:

from tkinter import*
import pyttsx3
import datetime


win=Tk()
win.configure(background="black")
win.geometry("500x700")
win.resizable(width=False,height=False)

#-----------------------------------------------------------------------------------------------------------------------------------------------
sys=pyttsx3.init()
voice_id="HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\TTS_MS_Cortana"
sys.setProperty("voice",voice_id)
def say(audio):
    sys.say(audio)
    sys.runAndWait()
    return

def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        t1=Label(win,text="Good Morning, sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
        t2=Label(win,text="What can I do for you today?",font=("calibri",18),bg="black",fg="light green").grid(column=0,row=2,sticky=W)
        say("Good Morning ,sir.")
        say("what can I do for you today?")
        
    elif hour>=12 and hour<18:
        t1=Label(win,text="Good Afternoon, sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
        t2=Label(win,text="What can I do for you today?",font=("calibri",15),bg="black",fg="light green").grid(column=0,row=2,sticky=W)
        say("Good Afternoon ,sir.")
        say("what can I do for you today?")
        
    elif hour>=18 and hour<21:
        t1=Label(win,text="Good Evening, sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
        t2=Label(win,text="What can I do for you today?",font=("calibri",18),bg="black",fg="light green").grid(column=0,row=2,sticky=W)
        say("Good evening,sir.")
        say("what can I do for you today?")
    else:
        t1=Label(win,text="Hello, sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
        t2=Label(win,text="What can I do for you today?",font=("calibri",18),bg="black",fg="light green").grid(column=0,row=2,sticky=W)
        say("Hello,sir.")
        say("what can I do for you today?")

wishme()

win.mainloop()

what I am trying to make is to open my tkinter window first and then to wish me good morning or evening etc. But It is wishing me first then opening the tkinter window. Can anyone help me with it ?

2

There are 2 answers

0
theo On

First, set up all of your labels then have all of the calls to the say() function in the wishme() function. tkinter's win.after(100,wishme) will run wishme() after 100ms separately to the mainloop.

def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        say("Good Morning ,sir.")
    elif hour>=12 and hour<18:
        say("Good Afternoon ,sir.")
    elif hour>=18 and hour<21:
        say("Good evening,sir.")
    else:
        say("Hello,sir.")
    say("what can I do for you today?")


win.after(100,wishme)
win.mainloop()
0
This is Nahid khan On

it says this:

Traceback (most recent call last):
  File "c:/Python37/win2.py", line 51, in <module>
    wishme()
  File "c:/Python37/win2.py", line 40, in wishme
    t1=Label(win,text="Good Evening, sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
  File "C:\Python37\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Python37\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "label" command: application has been destroyed

if i swap