我的小弟弟正在进入编程,而在他的科学博览会项目中,他正在模拟天空中的一群鸟.他已经完成了大部分代码编写工作,并且工作得很好,但是鸟类需要每时每刻都在移动.
然而,Tkinter占用了自己的事件循环的时间,所以他的代码不会运行.做root.mainloop()运行,运行和继续运行,它运行的唯一事情是事件处理程序.
有没有办法让他的代码与mainloop一起运行(没有多线程,这很混乱,这应该保持简单),如果是这样,它是什么?
现在,他想出了一个丑陋的黑客,把他的move()功能绑在一起<b1-motion>,所以只要他按住按钮并摆动鼠标,就可以了.但必须有一个更好的方法.
我打电话的时候
self.client = ThreadedClient()
Run Code Online (Sandbox Code Playgroud)
在我的Python程序中,我收到错误
"RuntimeError:主线程不在主循环中"
我已经做了一些谷歌搜索,但我在某种程度上犯了错误...有人可以帮助我吗?
完整错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 530, in __bootstrap_inner
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 483, in run
File "/Users/Wim/Bird Swarm/bird_swarm.py", line 156, in workerGuiThread
self.root.after(200, self.workerGuiThread)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 501, in after
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1098, in _register
RuntimeError: main thread is not in main loop
Run Code Online (Sandbox Code Playgroud)
类别:
class ThreadedClient(object):
def __init__(self):
self.queue = Queue.Queue( )
self.gui = GuiPart(self.queue, self.endApplication)
self.root = self.gui.getRoot()
self.running = True
self.GuiThread = threading.Thread(target=self.workerGuiThread) …Run Code Online (Sandbox Code Playgroud) 我有一个Python脚本,它使用Tkinter作为GUI.我的小脚本应该每隔X秒创建一个Toplevel小部件.当我运行我的代码时,第一个Toplevel小部件成功创建,但当它尝试创建第二个时,程序崩溃.
我正在做的是使用after方法每隔5秒与root的mainloop一起调用函数startCounting.每次调用此函数时,我都会将Toplevel小部件对象附加到列表中并启动一个新线程,希望它将运行新的主循环.
如果有人能解决这个问题,我将非常感激.顺便说一下,这只是我目前用来解决我的问题的一个小脚本,这使我无法继续我的真实学校项目.
代码:
import threading,thread
from Tkinter import *
def startCounting():
global root
global topLevelList
global classInstance
topLevelList.append (Toplevel())
topLevelList[len(topLevelList)-1].title("Child")
classInstance.append(mainLoopThread(topLevelList[len(topLevelList)-1]))
root.after(5000,startCounting)
class mainLoopThread(threading.Thread):
def __init__(self,toplevelW):
self.toplevelW = toplevelW
threading.Thread.__init__(self)
self.start()
def run(self):
self.toplevelW.mainloop()
global classInstance
classInstance = []
global topLevelList
topLevelList = []
global root
root = Tk()
root.title("Main")
startCounting()
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 我正在使用flask,matplotlib来保存图像和tensorflow来创建会话.我运行以下代码时遇到上述错误.烧瓶路线是否在单独的线程上运行?如何在主线程上运行fig.saveFig代码片段.非常感谢
@app.route('/open', methods = ['GET', 'POST'])
def sendOutput():
global loss,a2,xP,yP,scale,sess,fig
test_X,test_Y = own_model.getEvaluateData(scale)
cost,ans = sess.run([loss,a2],feed_dict={xP:test_X,yP:test_Y})
d = np.array(ans) - np.array(test_Y)
val = hist(d,100)
sess.close()
fig.saveFig('abc.png') //Errror on this line
Run Code Online (Sandbox Code Playgroud)