我是一名高中程序設計專業的學生,我有一個小問題。我一直負責在Tkinter編寫一個簡單的遊戲,在那裏冰柱從天花板上掉下來,你必須用鼠標避免它。夠簡單。但是,我遇到了一個問題。每當我在Tkinter應用程序中運行循環時,它都不會打開。我已經嘗試過使用time.sleep()每隔0.5秒暫停一次的for循環,只要循環結束,窗口就會打開。我需要做些什麼特殊的事情來讓循環在Tkinter中工作?沿着Tkinter應用程序運行一個循環
from Tkinter import *
import time
import random
class App:
def __init__(self, parent):
self.frame = Frame(root, bg= '#1987DF', width=800, height=800)
self.frame.bind("<Motion>", self.motionevent)
self.frame.pack()
#self.run()
def randhex(self):
b = "#"
for i in range(1, 7):
a = random.randint(0, 15)
if a == 10:
a = "A"
elif a == 11:
a = "B"
elif a == 12:
a = "C"
elif a == 13:
a = "D"
elif a == 14:
a = "E"
elif a == 15:
a = "F"
b = b+str(a)
return b
def motionevent(self, event):
xpos, ypos, bg = event.x, event.y, self.randhex()
str1 = "X : %d Y : %d BG : %s" % (xpos, ypos, bg)
root.title(str1)
x,y, delta = 100, 100, 10
self.frame.config(bg=bg)
def run(self):
for i in range(0, 10):
time.sleep(.5)
print 'i'
self.frame.config(bg=self.randhex())
root = Tk()
app = App(root)
root.mainloop()
目前它所要做的就是在鼠標移動時更改背景。當init中的行表示self.run()未註釋時,它將打印'i'10次,然後窗口打開。幫幫我?
可能是回答:http://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop – schlenk 2011-06-08 20:17:52