我剛剛開始使用聲卡 python 模塊的基本功能。但是,我無法讓我的 tkinter 窗口首先顯示,然后等到按下運行按鈕來執行“def run()”代碼。它總是先執行“def run()”代碼,然后打開窗口。我究竟做錯了什么?from tkinter import *import soundcard as scwindow = Tk()window.geometry("500x500")window.title("Virtual Soundcard")default_speaker = sc.default_speaker()default_mic = sc.default_microphone()def run(): with default_mic.recorder(samplerate=44100) as mic, \ default_speaker.player(samplerate=44100) as sp: for val in range(100): data = mic.record(numframes=None) sp.play(data)RunButton = Button(window, text ="Run", command = run())RunButton.pack()RunButton.place(x = 100, y = 250)window.mainloop()
1 回答

當年話下
TA貢獻1890條經驗 獲得超9個贊
更改RunButton = Button(window, text ="Run", command = run())
為
RunButton = Button(window, text ="Run", command = run)
基本上刪除()
命令參數,()
甚至在按下按鈕之前(即在代碼執行時)調用該函數。
希望這能解決這個錯誤。如有任何疑問或錯誤,請告訴我。
干杯
添加回答
舉報
0/150
提交
取消