如何在Tkinter的事件循環(huán)旁邊運(yùn)行自己的代碼?我弟弟剛開始編程,為了他的科學(xué)博覽會(huì)項(xiàng)目,他正在模擬天空中的一群鳥。他的大部分代碼都已經(jīng)寫好了,而且運(yùn)行得很好,但是鳥兒需要移動(dòng)每時(shí)每刻.然而,Tkinter占用了它自己的事件循環(huán)的時(shí)間,所以他的代碼無法運(yùn)行。做root.mainloop()運(yùn)行,并繼續(xù)運(yùn)行,它只運(yùn)行事件處理程序。是否有一種方法可以讓他的代碼與main循環(huán)一起運(yùn)行(沒有多線程,這是令人困惑的,這應(yīng)該保持簡單),如果是的話,是什么呢?現(xiàn)在,他想出了一個(gè)丑陋的黑客,把他的move()功能<b1-motion>,因此,只要他按住按鈕,搖動(dòng)鼠標(biāo),它就能工作。但一定有更好的辦法。
6 回答

HUH函數(shù)
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
after
Tk
from tkinter import *root = Tk()def task(): print("hello") root.after(2000, task) # reschedule event in 2 secondsroot.after(2000, task)root.mainloop()
after
def after(self, ms, func=None, *args): """Call function once after given time. MS specifies the time in milliseconds. FUNC gives the function which shall be called. Additional parameters are given as parameters to the function call. Return identifier to cancel scheduling with after_cancel."""

絕地?zé)o雙
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
update
mainloop
def task(): # do something root.update()while 1: task()
添加回答
舉報(bào)
0/150
提交
取消