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.
CodePudding user response:
all you need to do is add these two functions every time you want the window to update
window.update_idletasks()
window.update()
CodePudding user response:
Programs written for event-driven GUI toolkits like tkinter
are significantly different from standard Python scripts.
Once you have created a window, filled it with widgets and have initialized global data, you need to start the mainloop
.
Without a running mainloop
, there is no interaction with the GUI.
In essence, your program consists of a bunch of functions that are called from the mainloop, in response to the user operating controls, or timers expiring.
(I'm leaving out complications like using threading
out on purpose for the sake of simplicity.)
I don't think that pyttsx3
was written with event-driven GUIs in mind. So I suspect you will have to run it in a separate thread or process. Both threads and processes have their pros and cons.
If you are using a process to run pyttsx3
, you have to explicitly send the data to the GUI process using interprocess communication. On the other hand, the sound collection process cannot interfere with the GUI.
You could probably relatively easily test it separately from the GUI.
If you use threads you might have issues with responsiveness because in CPython only one thread at a time can be executing Python bytecode.
On the other hand, transferring the data to the GUI is trivial since both live in the same address space.
In my experience calling tkinter
functions or methods from a second thread is possible if two conditions are met;
- You are using Python 3 and
- the
tcl
andtk
used were built with support for threads enabled.