Python在函数之间传递数据

2024-06-26 01:34:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个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()函数,我不知道该怎么办。。。我想我可以在程序的顶部声明一个global,并将“textout”从snortout()传递到global,然后从paintInterface()调用它,这样就可以由painter显示它了?你知道吗

我没有线索了伙计们。。。你知道吗

# -*- 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)

Tags: importselftimeconftopdefetcqt