线程对象不能被解释为整数

时间:2015-05-05 07:01:58

标签: python multithreading python-3.x tkinter

尝试做一些动画电视" snow"在Tkinter(' Snow'已经过时,我知道) 我在标题中得到的错误是我无法解释的。如果我以通常的方式调用它,例程就可以工作。

import tkinter
from random  import randint
import threading

x,y =640, 480
rm=256*256*256

def do_snow(img):
  while True:  
    s=(" ".join((("{"+" ".join("#{:06x}".format(randint(0, rm))
          for i in range(x)))+"}" for j in range(y))))
    img.put(s)
    print('x')

t = tkinter.Tk()
img = tkinter.PhotoImage(width=x,height=y)
c = tkinter.Label(t,image=img);c.pack()
x=threading.Thread(target=do_snow ,args=(img,))
x.daemon = True
x.start() 
t.mainloop()

错误转储是:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python34\lib\threading.py", line 920, in _bootstrap_inner
    self.run()
  File "C:\Python34\lib\threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\AGUAL\Google Drive\python\tkinter\do_snow.py", line 12, in do_snow
    for i in range(x)))+"}" for j in range(y))))
  File "C:\Users\AGUAL\Google Drive\python\tkinter\do_snow.py", line 12, in <genexpr>
    for i in range(x)))+"}" for j in range(y))))
TypeError: 'Thread' object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:2)

修复变量名称:

x,y =640, 480
#...
x=threading.Thread(target=do_snow ,args=(img,))

更好的是,不要将模块全局变量用于所有事情。