我創建了一個圖形用戶界面,其中有一個按鈕系結到一個名為 voice_to_dictation() 的函式。此功能記錄音頻并將其轉錄為聽寫。現在,為了在 main() 中運行其他后續程式,我必須通過單擊“關閉 (X)”按鈕手動關閉此視窗。無論如何,在函式 Speech_to_dictation() 完成執行后,視窗會自動關閉/銷毀嗎?
from tkinter import *
import SpeechToDictation as Std
import pyautogui
import time
def speech_to_dictation():
speech_inst = Std.SpeechToDictation()
dictation.append(speech_inst.read_audio())
def user_speech_to_dictation():
main_window = Tk()
button = Button(text='Recording')
button.pack()
# I want this main_window to be closed as soon as speech_to_dictation function finished executing
button.config(command=speech_to_dictation)
main_window.mainloop()
class Test(Frame):
def __init__(self, parent=None, picks=[]):
Frame.__init__(self, parent)
self.vars = []
self.checkboxes = []
Label(self, text='Lab Works').grid(row=0, padx=10, pady=10)
i = 1
for pick in picks:
var = IntVar()
chk = Checkbutton(self, text=pick, variable=var)
chk.grid(row=i, pady=4, padx=10)
self.vars.append(var)
self.checkboxes.append(chk)
i = 1
def state(self):
return map((lambda var: var.get()), self.vars)
def full_screen(window):
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry("%dx%d" % (width, height))
def allstates():
print(list(lng.state()))
def make_test(window):
full_screen(window=root)
window.grid(row=1, column=0)
Button(root, text='Quit', command=root.quit).grid(row=10, padx=10, pady=10)
Button(root, text='Peek', command=allstates).grid(row=12, padx=10, pady=10)
if __name__ == '__main__':
# store dictation at dictation[]
dictation = []
user_speech_to_dictation()
is_string_complete = dictation[0]['text'][:8]
if is_string_complete == 'complete':
start_time = time.time() # returns number of seconds passed since epoch
max_loop_time = 1 # 1 seconds
while True:
if (time.time() - start_time) >= max_loop_time:
root = Tk()
lng = Test(root, ['Blood Count', 'Lipid Panel', 'Hemoglobin A1C'])
make_test(window=lng)
root.state('zoomed')
root.update()
x_position, y_position = pyautogui.position()
print(x_position, y_position)
max_loop_time = time.time() - start_time max_loop_time
# Coordinate of each boxes: they seem wrong
locations = [(53, 158), (84, 228), (36, 302)]
blood_count_string = dictation[0]['text'][9:]
if blood_count_string == 'blood count':
x_coordinate = locations[0][0]
y_coordinate = locations[0][1]
pyautogui.click(x_coordinate, y_coordinate)
allstates()
root.destroy()
# quit the program after the window is destroyed
if max_loop_time > 2:
break
uj5u.com熱心網友回復:
對于您的問題,只需傳遞main_window到speech_to_dictation()并銷毀main_windowinside speech_to_dictation():
def speech_to_dictation(win):
speech_inst = Std.SpeechToDictation()
dictation.append(speech_inst.read_audio())
win.destroy() # destroy the window
def user_speech_to_dictation():
main_window = Tk()
button = Button(text='Recording')
button.pack()
# I want this main_window to be closed as soon as speech_to_dictation function finished executing
button.config(command=lambda: speech_to_dictation(main_window))
main_window.mainloop()
uj5u.com熱心網友回復:
使用sys.exit()
退出 Python。這是通過引發 SystemExit 例外來實作的,因此執行 try 陳述句的 finally 子句指定的清理操作,并且可以在外部級別攔截退出嘗試。
當 proses 完成時,只需像這樣呼叫這個函式: sys.exit()
不要忘記import sys首先(它是內置的)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/345017.html
