将i3bar与pythonscrpit一起使用时发生了一些奇怪的事情
这里是Python新手将i3bar与pythonscrpit一起使用时发生了一些奇怪的事情,python,i3,Python,I3,这里是Python新手 #!/usr/bin/python # -*- coding: utf-8 -*- import commands,time def get_vol(): vol =int(commands.getoutput("amixer get Master | grep -E -o '[0-9] {1,3}?%' | head -1 | sed 's/.$//'")) full_block =(vol+10)/10-1 if vol%10==0:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import commands,time
def get_vol():
vol =int(commands.getoutput("amixer get Master | grep -E -o '[0-9]
{1,3}?%' | head -1 | sed 's/.$//'"))
full_block =(vol+10)/10-1
if vol%10==0:
half_block =0
else:
half_block =1
empty_block =10-(full_block+half_block)
return '▮'*full_block+'▯'*half_block+'-'*empty_block
def NTime():
return commands.getoutput("date '+%H:%M'")
def battery():
prenBat =int(commands.getoutput('acpi battery | grep -o "[0-9]*[0-9]" | sed "1d"'))
bat_icon =prenBat/20
status =commands.getoutput('acpi battery | egrep -o -m1 "Discharging|Charging|AC|Full|Unknown"')
if status== "Charging":
return '❖'+' ●'*bat_icon+' ○'*(5-bat_icon)
elif status=="Full":
return '✔'+' ●'*bat_icon+' ○'*(5-bat_icon)
else:
return ' ●'*bat_icon+' ○'*(5-bat_icon)
battime =1
batNum =battery()
while True:
time.sleep(0.1)
volume =get_vol()
nTime =NTime()
while battime ==30:
batNum =str(battery())
battime =1
else:
battime =battime+1
print batNum+" | "+volume+" | "+nTime
我使用这个脚本直接指向i3bar(没有I3block),它什么也没有显示。然后我等了一会儿。它显示了barttery,但没有音量部分和时间部分。然后,我猜大约10秒。它显示了一个或两个块(▮) 从音量部分,没有别的
我试着在终端上测试它,结果很好。
这是i3config中我的i3bar部件
bar {
position top
colors {
statusline #414149
background #8cb194
separator #414149
focused_workspace #8cb194 #c8d087 #000000
inactive_workspace #8cb194 #868974 #000000
}
# status_command i3blocks -c ~/.config/i3blocks.conf
status_command ~/.config/bar.py
}
Sry用于我的英语和新手代码。i3bar预期输入格式为json,如文档中的以下链接所述:
- 由于在Python 3中不存在并且不存在模块,您可以考虑使用而不是运行命令。 <0.1s的睡眠时间可能过于激进,你可能会考虑改变间隔几秒钟,因为电池和体积值不经常改变,这也避免浪费太多的CPU。 不要使用大量的SED,GRIP,您可以考虑使用Python本身做字符串操作部分,甚至在不同的OS上使用更多的代码。
- 如PEP8中所述,最好的做法是“始终在运算符的两侧使用一个空格:赋值(=)、增广赋值(+=、-=等)、比较(=、!=、=、in、not in、is、is、not)、布尔值(and、or、not)。”
- 将代码主体放在一个main方法中,允许您在其他脚本上或从REPL导入代码——除了帮助加强作用域和使代码看起来更干净之外……这是一本不错的书