这个问题看起来很简单,但是我在google上搜索了一天并查看了stackoverflow之后找不到任何解决方案。 最初我正在开发一个简单的等离子体,它将每30分钟向本地web服务器发送一个特定的请求,解析输出并在面板上的标签中显示。我举了一个等离子体的例子-BWC-Balance-并对其进行了修改。代码如下:
#!/usr/bin/env python
# coding: utf-8
"""
BWC Balance plasmoid
Site: http://bitbucket.org/svartalf/bwc-balance-plasmoid/
Author: SvartalF (http://svartalf.info)
Original idea: m0nochr0me (http://m0nochr0me.blogspot.com)
"""
import re
from urllib import urlencode
import urllib2
import cookielib
import datetime
import sys
import re
import string
import os
import gobject
import commands
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kio import *
from PyKDE4.kdeui import *
from PyKDE4.kdecore import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from PyKDE4.solid import Solid
from settings import SettingsDialog
parsed_ok = 0
curr_day = ''
class BWCBalancePlasmoid(plasmascript.Applet):
"""Applet main class"""
def __init__(self, parent, args=None):
plasmascript.Applet.__init__(self, parent)
def init(self):
"""Applet settings"""
self.setHasConfigurationInterface(True)
self.setAspectRatioMode(Plasma.Square)
self.theme = Plasma.Svg(self)
# self.theme.setImagePath("widgets/background")
# self.setBackgroundHints(Plasma.Applet.DefaultBackground)
self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
# Main label with balance value
self.label = Plasma.Label(self.applet)
self.label.setText(u'<b><font color=blue size=3>No data...</font></b>')
self.layout.addItem(self.label)
self.applet.setLayout(self.layout)
self.resize(350, 30)
self.startTimer(2500)
def postInit(self):
"""Start timer and do first data fetching
Fired only if user opened access to KWallet"""
self.setLabelText()
def update(self, value):
"""Update label text"""
self.label.setText(value)
def timerEvent(self, event):
"""Create thread by timer"""
self.setLabelText()
pass
def setLabelText(self):
login = 'mylogin'
request = 'curl --ntlm -sn http://some.local.resource'
out_exp = ""
out_exp = commands.getoutput(request)
table_name_exp = re.findall(r"some_regular_expression",out_exp)
tp = '| html2text | grep -i -A3 ' + login
out_exp = ''
try:
cmd_exp = 'curl --ntlm -sn ' + table_name_exp[0] + ' ' + tp
out_exp = commands.getoutput(cmd_exp)
except:
cmd_exp = ''
date_check = re.findall(r"one_more_regular_expression", out_exp)
times_exp = re.findall(r"[0-9][0-9]:[0-9][0-9]", out_exp )
if len(times_exp) != 0 and len(date_check) != 0:
self.label.setText(u'<b><font color=blue size=3>Start: ' + times_exp[0] + u' --- Finish: ' + str(int(string.split(times_exp[0], ':')[0]) + 9) + ':' + string.split(times_exp[0], ':')[1] + ' </span></b>')
else:
self.label.setText(u'<b><font color=blue size=3>No data...</span></b>')
def CreateApplet(parent):
return BWCBalancePlasmoid(parent)
我得到的是以下错误:
^{pr2}$经过几个小时的谷歌搜索,我明白了:从管道里读东西会被一些信号打断。但我唯一的信号就是计时器。我发现的唯一建议是“去掉干扰你阅读的信号”。对我来说,这似乎有点奇怪和不现实:不使用计时器定期读取数据。 我错过什么了吗?也许应该使用其他机制来访问web资源并解析其输出?或者“中断系统调用”是一种正常情况,应该如何处理?在
提前感谢你的帮助。在
当管道仍在读数时,似乎正在发送信号。在
因此,请尝试在调用
setLabelText()
之前停止计时器,然后再重新启动它。在编辑
您还应该尝试重写代码以使用
subprocess
,而不是不推荐使用的commands
模块。例如:相关问题 更多 >
编程相关推荐