亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用參數調用函數后的 tkinter 導致函數掛起

使用參數調用函數后的 tkinter 導致函數掛起

翻閱古今 2021-09-24 15:19:22
這是一個生命游戲應用程序,并使用 after 來減緩細胞在出生、生命、死亡等階段的動畫。之后的幾個問題:1.我一直在廣泛研究 Tkinter 并且這篇文章捕獲了我發現的最清晰的關于 after 參數的說明,但是,我的 Tkinter 應用程序不起作用 - 根據已經收到的評論,它可能不是之后,但這就是癥狀所在出現?2.當我不把參數放在后面的括號里時,基本上后面似乎根本不起作用(例如 widget.after(200, self.my_function, parameter 1, parameter 2, ....) 't 迭代。但是,當我執行相同操作但包含它按預期迭代的參數時(例如,widget.after(200, self.my_function(parameter 1, parameter 2, ....))。3.然而,當用括號中的參數運行時,后掛起。下面代碼中的列表包含 81 個項目,并非巧合的是,該函數掛起 16.2 秒...代碼如下: def color_cells(           self,            cells,            repeater,            temp_cells=0,            counter=0,            after_id=None           ):    global paused    if temp_cells != 0:        self.repeat_colors(temp_cells)        temp_cells.clear()        temp_cells = 0        for key in cells:            if cells[key][0] == 'emerging':                cells[key] = 'active', colors['active'][1]            if cells[key][0] == 'dying':                cells[key] = 'inactive', colors['inactive'][1]        counter = 0        if repeater and not paused:            print("repeater is ", repeater, " and paused is ",paused)            self.id_changes(cells)        else:            self.closeoutGame()布萊恩,你曾要求提供錯誤示例。為了顯示錯誤,我向調用函數添加了一些打印語句,然后解釋了 16.2 秒不活動時間段的開始位置: starting repeat colors and the temps cells len is  81  and the counter is  0 starting repeat colors and the temps cells len is  81  and the counter is  1 starting repeat colors and the temps cells len is  81  and the counter is  2 ... starting repeat colors and the temps cells len is  81  and the counter is  79 starting repeat colors and the temps cells len is  81  and the counter is  80 starting repeat colors and the temps cells len is  81  and the counter is  81 ...hangs for the 16.2 seconds, equal to 200 ms x 81 iterations我是一個業余愛好者,沒有接受過正規培訓,所以我確信我在這里忽略了一些基本的東西,包括如何最好地自己研究。但我感謝任何建議。
查看完整描述

4 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

你的問題是由root.after(200, self.repeat_colors(temp_cells, counter)). 相反,您需要將您的self.repeat_colors作為 lambda傳遞。

所以發生的事情是self.repeat_colors(temp_cells, counter)立即調用而不是等待 200 秒。因此,而是創建一個 lambda 函數,該函數將等到設定的時間激活。

請記住 lambda 表達式,如果您有一個更改的值,您需要在 lambda 中定義它。所以對于計數器,你需要做一些類似x=counter的事情,所以 lambda 肯定會使用正確的更新值。這通常會影響諸如創建 lambda 表達式的循環之類的東西,在這種特殊情況下可能無關緊要,但在重要時練習是一個好習慣。

改變這個:

root.after(200, self.repeat_colors(temp_cells, counter))

對此:

root.after(200, lambda x=counter: self.repeat_colors(temp_cells, x))


查看完整回答
反對 回復 2021-09-24
  • 4 回答
  • 0 關注
  • 252 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號