#python #tkinter
#питон #tkinter
Вопрос:
я просто хочу добавить несколько кнопок в свой проект, поэтому я использую tkinter gui
, но это выдает ошибку:
TclError: can't invoke "button" command: application has been destroyed.
может кто-нибудь объяснить ошибку и почему это происходит.
код:
import speech_recognition as sr
import webbrowser
import ctypes
from time import ctime
from win32com.client import Dispatch
import time
import subprocess
from IPython.display import clear_output
import os
from tkinter import *
r = sr.Recognizer()
speak = Dispatch("SAPI.SpVoice")
time_n = ctime()
master = Tk()
def startjarvis(t_run):
while t_run > 0:
def record_audio(ask = False):
with sr.Microphone() as source:
if ask:
print(ask)
#print there was here
audio = r.listen(source)
voice_data = ''
try:
voice_data = r.recognize_google(audio)
#before: print(voice_data)
except sr.UnknownValueError:
print('Did not understand')
except sr.RequestError:
print('speech server down')
return voice_data
def respond(voice_data):
if 'what is your name' in voice_data:
speak.speak('my name is Jarvis')
elif 'where do you live' in voice_data:
speak.speak("I am in Iron Man's suit")
elif 'Jarvis' in voice_data:
speak.speak('yes sir')
#---------------------------------------------------------------------------------------------------------
elif "what is the time" in voice_data:
print(ctime())
speak.speak(time_n[0:16])
elif "is the time" in voice_data:
print(ctime())
speak.speak(time_n[0:16])
#---------------------------------------------------------------------------------------------------------
elif 'search' in voice_data:
speak.speak('What do you want to search?')
search = record_audio('What do you want to search?')
url = 'https://google.com/search?q=' search
webbrowser.get().open(url)
speak.speak('Here is what i found')
elif 'find location' in voice_data:
location = record_audio('What is the location?')
url = 'https://google.nl/maps/place/' location '/amp;amp;'
webbrowser.get().open(url)
speak.speak('Here is what i found')
elif 'what is my location' in voice_data:
webbrowser.open('https://www.google.com/search?q=what is my location nowamp;rlz=1C1GIGM_enIN847IN851amp;oq=what amp;aqs=chrome.0.69i59l2j69i57j69i59j69i60l2j69i61j69i60.2695j0j7amp;sourceid=chromeamp;ie=UTF-8')
speak.speak('Here is what i found')
speak.speak('I have placed your location adress on top of your search results')
#---------------------------------------------------------------------------------------------------------
elif 'open Gmail' in voice_data:
webbrowser.open('https://mail.google.com/mail/u/0/?tab=rmamp;ogbl#inbox')
elif 'open my Google classrooms' in voice_data:
webbrowser.open('https://classroom.google.com/')
elif 'open WhatsApp' in voice_data:
webbrowser.open('web.whatsapp.com')
elif 'send a WhatsApp message' in voice_data:
speak.speak('What is the number of the person?')
num = record_audio('What is the number of the person?')
speak.speak('What message do you want to send?')
search = record_audio('What message do you want to send?')
speak.speak("please type Time to send the message 24 hour format (hour): ")
time = int(input("please type Time to send the message 24 hour format (hour): "))
speak.speak("please type Time to send the message 24 hour format (minute): ")
time_min = int(input("please type Time to send the message 24 hour format (minute): "))
url = 'https://web.whatsapp.com'
pywhatkit.sendwhatmsg(" 91" num, search, time,time_min)
time.sleep(10)
speak.speak("message sent")
#---------------------------------------------------------------------------------------------------------
elif 'dictate' in voice_data:
speak.speak('Please enter text')
text = input('Text: ')
speak.speak(text)
#---------------------------------------------------------------------------------------------------------
elif 'open calculator' in voice_data:
subprocess.Popen('C:\Windows\System32\calc.exe')
#---------------------------------------------------------------------------------------------------------
elif 'what is the working directory' in voice_data:
o = os.getcwd()
print(o)
speak.speak(f'{o} is the current working directory')
elif 'what is there in this directory' in voice_data:
o = os.listdir()
print(o)
speak.speak(f'{o} is there in the current working directory')
elif 'list my files' in voice_data:
speak.speak('Type the path')
file_path = input('path: ')
for folder, sub_folders, files in os.walk(file_path):
print(f"Currently looking at {folder}")
print('n')
print('t The subfolders are:')
for sub_fold in sub_folders:
print(f"t t Subfolders: {sub_fold}")
print('n')
print("The files are:")
for f in files:
print(f"t File: {f}")
print('n')
#---------------------------------------------------------------------------------------------------------
else:
url = 'https://google.com/search?q=' voice_data
webbrowser.open(url)
print('how can i help you ?')
speak.speak('how can i help you?')
voice_data = record_audio()
respond(voice_data)
t_run = t_run - 1
time.sleep(3)
clear_output()
time.sleep(2)
def times_to_run():
def run_1():
startjarvis(1)
def run_2():
startjarvis(2)
def run_3():
startjarvis(3)
speak.speak("plaese click the button having number of times you want me to run")
button_1 = Button(master, text="1 Time", command=run_1)
button_1.pack()
button_2 = Button(master, text="2 Times", command=run_2)
button_2.pack()
button_3 = Button(master, text="3 Times", command=run_3)
button_3.pack()
#---------------------#---------------------#-------------#
#last cell below
button = Button(master, text="Start Jarvis", command=times_to_run)
button.pack()
mainloop()
я использую jupyter notebook
и получаю ошибку в последней ячейке.
Заранее спасибо.
Комментарии:
1. Ошибка означает, что вы вызываете виджет окна, но вы его уже уничтожили.
2. но я не уничтожил его. я добавил только 3 кнопки
3. Попробуйте запустить приведенный выше код в обычном редакторе кода или в режиме ОЖИДАНИЯ
4. когда я использую код в spyder, но в нем не указаны имена модулей tkinter,
5. Использовать в каком-то IDLE или IDE, а не в spyder