我在类似的错误消息上看到了其他一些帖子,但找不到可以解决我的问题的解决方案.
我用TkInter稍微涉足并创建了一个非常简单的UI.该守则如下─
from tkinter import *
root = Tk()
def grabText(event):
print(entryBox.get())
entryBox = Entry(root, width=60).grid(row=2, column=1, sticky=W)
grabBtn = Button(root, text="Grab")
grabBtn.grid(row=8, column=1)
grabBtn.bind('<Button-1>', grabText)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我启动并运行UI.当我单击Grab按钮时,我在控制台上收到以下错误:
C:\Python> python.exe myFiles\testBed.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "myFiles\testBed.py", line 10, in grabText
if entryBox.get().strip()=="":
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
错误追溯到entryBox.
我敢肯定有人可能以前处理过这个问题.任何帮助表示赞赏.
尝试运行docs中给出的asyncio hello world代码示例时:
import asyncio
async def hello_world():
print("Hello World!")
loop = asyncio.get_event_loop()
# Blocking call which returns when the hello_world() coroutine is done
loop.run_until_complete(hello_world())
loop.close()
Run Code Online (Sandbox Code Playgroud)
我收到错误:
RuntimeError: Event loop is closed
Run Code Online (Sandbox Code Playgroud)
我使用的是python 3.5.3.
我想从 Tkinter 按钮命令调用异步函数,或者想让该函数异步运行,以便 tkinter 窗口响应。
我已经command = open_async在 Button 中尝试过,其open_async声明如下async def open_async():,但它给了我一个错误“RuntimeWarning:coroutine 'open_async'从未等待 self.tk.mainloop(n)”。
如果我只是使用def async():,代码仍然会同步运行,因此 UI 将没有响应。(async()内部也包含一些其他异步操作,使用asyncio等)。
有人知道如何解决这个问题吗?我也不想创建线程。谢谢
问题是我的解决方案是否是一种使用来自另一个线程的数据更新 Tkinter-GUI 的保存和 pythonic 方式?是Lock必需的吗?或者怎么能在Queue这里提供帮助?此示例运行良好,但原始应用程序需要处理复杂得多的数据。
请专注于AsyncioThread.create_dummy_data()最小的工作示例。该示例有两个线程。一个运行Tkinter -mainloop,第二个线程运行asyncio -loop。异步循环模拟获取一些数据并tkinter.Label用这些数据刷新一些数据。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# restrict to Python3.5 or higher because of asyncio syntax
# based on </sf/answers/3354408991/>
from tkinter import *
import asyncio
import threading
import random
class AsyncioThread(threading.Thread):
def __init__(self, asyncio_loop, theWindow):
self.asyncio_loop = asyncio_loop
self.theWindow = theWindow
self.maxData = len(theWindow.varData)
threading.Thread.__init__(self)
def run(self):
self.asyncio_loop.run_until_complete(self.do_data())
async def do_data(self):
""" Creating and starting 'maxData' asyncio-tasks. """ …Run Code Online (Sandbox Code Playgroud) python asynchronous tkinter python-multithreading python-asyncio