- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 python 并不是特别陌生,但我只是认为这个程序无法正常工作可能是有原因的。我已经写了一个类似的,这是从中派生的,并且仍然可以正常工作。它是一个程序,用于绘制一组 ping 响应的平均时间图,以查看一天中的时间是否存在任何模式。出处如下
from Tkinter import *
import matplotlib
import time
import os, sys, threading, Queue
matplotlib.use('TkAgg')
from numpy import arange, array, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import Tkinter
import sys
class App(object):
def __init__(self):
self.q = Queue.Queue()
self.q2 = Queue.Queue()
self.avvals=[]
self.addlist=['bbc.co.uk', 'google.co.uk', 'nhgs.co.uk', 'bing.co.uk', 'msn.com']
self.addlistlen = len(self.addlist)
self.root = Tkinter.Tk()
self.root.wm_title("Connection Speed")
self.frame = Tkinter.Frame(self.root)
self.frame.pack(side='top', expand=1, fill='both',)
self.frame2 = Tkinter.Frame(self.frame)
self.frame2.pack(side='bottom', expand=1, fill='both',)
self.f = Figure(figsize=(5,4), dpi=100)
self.a = self.f.add_subplot(111)
self.gframe = Tkinter.Frame(self.frame)
self.gframe.pack(side='top', expand=1, fill='both',)
self.canvas = FigureCanvasTkAgg(self.f, master=self.gframe)
self.canvas.show()
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)
self.lt = threading.Thread(target=self.loop())
def getping(self, add):
pingaling = os.popen("ping -n 2 "+str(add).strip())
sys.stdout.flush()
line = pingaling.read()
if line:
try:
line = line.split('Maximum = ')[1]
time = line.split('ms, Average')[0]
self.q.put(int(time))
except:
self.q.put(None)
def gpthread(self, *a):
t = threading.Thread(target=self.getping, args=a)
t.isDaemon = True
t.start()
def loop(self):
while 1:
for x in self.addlist:
self.gpthread(x)
while self.q.qsize<self.addlistlen:
pass
tl = []
for u in range(self.addlistlen):
temp = self.q.get()
if temp != None:
tl.append(temp)
if len(tl)>0:
self.update(sum(tl)/len(tl))
else:
self.update(None)
def update(self, val):
self.a.clear()
self.avvals.append(val)
self.a.plot(self.avvals, linewidth=0.5, color = 'b')
self.canvas.draw()
a = App()
try:
a.root.mainloop()
except:
a.root.destroy()
我可能不需要底部尝试..除了但我把它放进去检查它是否会有所作为。我还没有机会在另一台计算机上尝试它,但我的其他脚本运行良好,所以......我根本无法理解为什么它会卡住、停止响应,并且如果我通过任何方法退出它,我都会收到一条错误消息致命的 Python 错误:PyEval NULL tstate或者非常相似的东西。现在它甚至没有扩大!它只是直接没有响应!
最佳答案
改变
self.lt = threading.Thread(target=self.loop())
到
self.lt = threading.Thread(target=self.loop)
target=self.loop()
在将结果传递给 threading.Thread
之前调用 loop
方法 .
传递target=self.loop
将方法对象传递给threading.Thread
而不调用它。这让 threading.Thread
在新线程中调用该方法。
下面是一些代码,它使用线程来 ping 一些 ip,并在动画 matplotlib 条形图中显示平均 ping 时间,嵌入在 Tkinter 窗口中:
import Tkinter
import threading
import subprocess
import Queue
import shlex
import re
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg
import atexit
import numpy as np
pingers=[]
def cleanup():
print('terminating ping subprocesses...')
for pinger in pingers:
pinger.proc.terminate()
atexit.register(cleanup)
class Pinger(threading.Thread):
def __init__(self,app,queue):
threading.Thread.__init__(self)
self.app=app
self.queue=queue
def run(self):
# One ping subprocess is started by each Pinger thread.
# The ping subprocess runs indefinitely, terminated by the cleanup function
# which is called by atexit right before the main program terminates.
ip = self.queue.get()
cmd="ping %s" % ip
self.proc = subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE)
for line in iter(self.proc.stdout.readline,''):
match=re.search('time=(.*)\s+ms',line)
if match:
avg=float(match.group(1))
self.app.update(ip,avg)
class App(object):
def __init__(self,master,ips):
self.ips=ips
self.fig = plt.Figure(figsize=(5,4), dpi=100)
self.fig.subplots_adjust(bottom=0.25)
self.ax=self.fig.add_subplot(1,1,1)
self.canvas = tkagg.FigureCanvasTkAgg(self.fig, master=master)
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
self.canvas.show()
N=len(self.ips)
self.idx=dict(zip(self.ips,range(N)))
# I set an initial ping time of 200 just to make the initial bar chart
times=[200]*N
self.rects=self.ax.bar(range(N), times)
self.ax.set_xticks(np.arange(N)+0.8*0.5)
self.ax.set_xticklabels(self.ips, rotation=25)
def update(self,ip,avg):
# This is called by Pinger threads, each time a new ping value is obtained
print(ip,avg)
self.rects[self.idx[ip]].set_height(avg)
self.canvas.draw()
def main():
root = Tkinter.Tk()
root.wm_title("Connection Speed")
ips=['bbc.co.uk', 'google.co.uk', 'nhgs.co.uk', 'bing.co.uk', 'msn.com']
app = App(root,ips)
queue = Queue.Queue()
for ip in ips:
queue.put(ip)
# This starts one Pinger for each ip.
pinger=Pinger(app,queue)
pingers.append(pinger)
pinger.daemon=True
pinger.start()
Tkinter.mainloop()
if __name__=='__main__':
main()
关于Python 脚本无缘无故卡住 (afaik),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4639336/
我正在安装这个程序:THERMUS ,据我所知应该安装正常。我/我通过 ubuntu 控制台安装这个程序。但是当我运行 make all 时,我收到了这条消息: make: ***No rule to
我正在渲染一个简单的 sass 文件并收到以下警告: This selector doesn't have any properties and won't be rendered. ╷ 14
我不明白为什么文本不会与 div 的中间对齐,我认为这是导致页面底部出现空白的原因,我希望文本位于中间(高度)页脚的(两个 div 我都有两个强制文本的每个部分到页面的边缘) HTML:
我正在尝试为 android 创建基本的音乐播放器。对我来说一切似乎都很好,但是当我试图在我的手机上运行应用程序时。它说它停止了。我无法解决那个问题。感谢您的任何帮助。我试图在应用程序停止时查看“Lo
在我的 LoginProvider 中,我使用了一个函数来执行登录并将创建的 session 作为 promise 返回。 @Injectable() export class LoginProvid
我在 Google Cloud Platform 上运行 Dataflow-Jobs,我收到的一个新错误是“Workflow failed”,没有任何解释。我得到的日志如下: 2017-08-25
我已经阅读了无数关于这个错误的主题,但是没有一个和我有同样的问题。 我得到了 E/MediaPlayer: 错误 (-19, 0) E/MediaPlayer: 错误 (-19,0) 错误,然而,音乐
这个错误或我缺乏知识或其他东西真的开始困扰我。我正在开发一个 Grails 应用程序,并且在我的工作过程中随机出现 Grails 提示一些导入,说无法解析类名。它在一个保存前工作!我没有对项目的基础设
我为此失去了头发!我不断收到“发送后无法设置 header ”错误,我确定我没有像在其他问题中看到的那样调用 Next()。我的代码一直在工作,直到我尝试进行一些重构,我没有改变这个类的任何东西,所以
我是一名优秀的程序员,十分优秀!