我正在尝试制作一个plasmoid小部件来显示我桌面上的Snort输出,我现在正在学习C但我对Python的了解不多,除了我已经想到试图做这个应用程序我最初以为它会变得简单,但耶稣......我的头脑已经乱了4天而且我无法让它工作......
我不能只做
commands.getoutput("snort -A console -q -u snort -g snort -c /etc/snort/snort.conf")
因为像“top”这样的程序在bash屏幕上刷新并保持“打开”状态,而不是仅仅转储输出并完成执行......
所以从我收集到的东西,我需要通过线程来做到这一点,所以我发现我可以做以下
def snortout():
print(time.ctime())
threading.Timer(10, repeat).start()
f= open('snort.out', 'w')
top= os.system("snort -A console -q -u snort -g snort -c /etc/snort/snort.conf")
s=str(top)
textout = f.write(s)
snortout()
现在我需要将“textout”数据传递给paintInterface()函数,我不知道该怎么做...我想我可以在程序的顶部声明一个全局并传递“textout” snortout()到全局并从paintInterface()调用它,以便画家显示它?
我在这里找不到线索...
# -*- coding: utf-8 -*-
# <Copyright and license information goes here.>
from PyQt4.QtCore import Qt
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
import commands
import os
import time
import threading
class HelloPython(plasmascript.Applet):
def __init__(self,parent,args=None):
plasmascript.Applet.__init__(self,parent)
def init(self):
self.setHasConfigurationInterface(False)
self.resize(800, 400)
self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
def repeat():
print(time.ctime())
threading.Timer(10, repeat).start()
f= open('snort.out', 'w')
top= os.system("snort -A console -q -u snort -g snort -c /etc/snort/snort.conf")
s=str(top)
textout = f.write(s)
repeat()
def paintInterface(self, painter, option, rect):
painter.save()
painter.setPen(Qt.black)
painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter,textout)
painter.restore()
def CreateApplet(parent):
return HelloPython(parent)