如何获取状态栏中的当前功耗? (开发状态图标)

我想问一下如何开发一个简单的脚本/应用程序并将其放在时间附近的状态栏中(右上角)。 假设我有一台笔记本电脑,脚本每10秒就会以瓦特为单位获取电池电量,因此会显示在状态栏中。 我使用ubuntu 16统一

Ubuntu提供了一组库和示例,用于迁移简单菜单和一致的界面。

上面链接的文档中的示例包括以下语言的版本:

  • C
  • PYGI
  • PyGTK的
  • C#
  • 瓦拉
  • 哈斯克尔

页面中的python`示例是:

#!/usr/bin/env python # # Copyright 2009-2012 Canonical Ltd. # # Authors: Neil Jagdish Patel  # Jono Bacon  # David Planella  # # This program is free software: you can redistribute it and/or modify it # under the terms of either or both of the following licenses: # # 1) the GNU Lesser General Public License version 3, as published by the # Free Software Foundation; and/or # 2) the GNU Lesser General Public License version 2.1, as published by # the Free Software Foundation. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranties of # MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR # PURPOSE. See the applicable version of the GNU Lesser General Public # License for more details. # # You should have received a copy of both the GNU Lesser General Public # License version 3 and version 2.1 along with this program. If not, see #  # from gi.repository import Gtk from gi.repository import AppIndicator3 as appindicator def menuitem_response(w, buf): print buf if __name__ == "__main__": ind = appindicator.Indicator.new ( "example-simple-client", "indicator-messages", appindicator.IndicatorCategory.APPLICATION_STATUS) ind.set_status (appindicator.IndicatorStatus.ACTIVE) ind.set_attention_icon ("indicator-messages-new") # create a menu menu = Gtk.Menu() # create some for i in range(3): buf = "Test-undermenu - %d" % i menu_items = Gtk.MenuItem(buf) menu.append(menu_items) # this is where you would connect your menu item up with a function: # menu_items.connect("activate", menuitem_response, buf) # show the items menu_items.show() ind.set_menu(menu) Gtk.main() 

您可以使用列表中的程序作为脚本的包装器,以便单击该项目将调用您的脚本。


使图标和文本动态化

(摘自: 如何编写动态更新的面板应用/指示器? )

这个例子建议使用GObject 。 调用gobject.threads_init()一个应用程序初始化。 然后正常启动线程,但要确保线程不会直接执行任何GUI任务。 而是使用gobject.idle_add直接调度GUI任务。 (以上是链接停止工作时链接的确切引用。)

 #!/usr/bin/env python3 import signal import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, AppIndicator3, GObject import time from threading import Thread class Indicator(): def __init__(self): self.app = 'test123' iconpath = "/opt/abouttime/icon/indicator_icon.png" self.indicator = AppIndicator3.Indicator.new( self.app, iconpath, AppIndicator3.IndicatorCategory.OTHER) self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) self.indicator.set_menu(self.create_menu()) self.indicator.set_label("1 Monkey", self.app) # the thread: self.update = Thread(target=self.show_seconds) # daemonize the thread to make the indicator stopable self.update.setDaemon(True) self.update.start() def create_menu(self): menu = Gtk.Menu() # menu item 1 item_1 = Gtk.MenuItem('Menu item') # item_about.connect('activate', self.about) menu.append(item_1) # separator menu_sep = Gtk.SeparatorMenuItem() menu.append(menu_sep) # quit item_quit = Gtk.MenuItem('Quit') item_quit.connect('activate', self.stop) menu.append(item_quit) menu.show_all() return menu def show_seconds(self): t = 2 while True: time.sleep(1) mention = str(t)+" Monkeys" # apply the interface update using GObject.idle_add() GObject.idle_add( self.indicator.set_label, mention, self.app, priority=GObject.PRIORITY_DEFAULT ) t += 1 def stop(self, source): Gtk.main_quit() Indicator() # this is where we call GObject.threads_init() GObject.threads_init() signal.signal(signal.SIGINT, signal.SIG_DFL) Gtk.main() 

而不是计算猴子:-),我修改了LD James的答案中的第二个脚本, 以瓦特为单位显示我笔记本电脑的当前功耗

在此处输入图像描述

该脚本适用于Ubuntu 16.04 ,可能唯一的系统特定事物是存储当前功耗值的文件。 在我的情况下,我在tlp的帮助下找到了它:

 $ sudo tlp stat | grep -P '\[m(W|A)\]' # Output on Lenovo ThinkPad X230 Tablet /sys/class/power_supply/BAT0/power_now = 11246 [mW] $ sudo tlp stat | grep -P '\[m(W|A)\]' # Output on Dell Vostro 3350 Laptop /sys/class/power_supply/BAT0/power_now = 6700 [mA] 

请注意,某些器件以瓦特为单位提供当前功耗,但有些器件提供电压和电流的当前值(安培) – 脚本涵盖了这些情况

此外,我创建了GitHub项目PowerNow并添加了其他选项:在gnome-terminal执行htoppowertoptlp stat

在此处输入图像描述

安装Python脚本powerNow和可选的启动应用程序(和〜/ Desktop) .desktop文件:

  • 将脚本复制到/usr/local/bin以使其可以作为系统范围的shell命令访问:

     sudo wget https://raw.githubusercontent.com/pa4080/powerNow/master/powerNow.py -O /usr/local/bin/powerNow sudo chmod +x /usr/local/bin/powerNow 
  • 将脚本复制到~/bin ,使其仅对当前用户可访问:

     wget https://raw.githubusercontent.com/pa4080/powerNow/master/powerNow.py -O $HOME/bin/powerNow chmod +x $HOME/bin/powerNow 
  • 将桌面文件复制到~/Desktop (需要脚本):

     wget https://raw.githubusercontent.com/pa4080/powerNow/master/powerNow.desktop -O $HOME/Desktop/powerNow.desktop chmod +x $HOME/Desktop/powerNow.desktop 
  • 将桌面文件复制到~/.config/autostart (需要脚本):

     wget https://raw.githubusercontent.com/pa4080/powerNow/master/powerNow.desktop -O $HOME/.config/autostart/powerNow.desktop chmod +x $HOME/.config/autostart/powerNow.desktop