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

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

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

答案1

我没有去数猴子 :-),而是修改了第二个脚本詹姆斯回答显示我的笔记本电脑的当前功耗(以瓦为单位)

在此处输入图片描述

该脚本适用于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并添加了附加选项:执行htoppowertoptlp stat在内gnome-terminal

在此处输入图片描述

安装 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
    

答案2

Ubuntu 提供了一组库及其使用示例以实现简单菜单和一致界面的迁移。

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

  • C
  • 皮吉
  • 丙烷二异戊酸酯
  • C#
  • 瓦拉
  • 哈斯克尔

该页面上的一个 python 示例是:

#!/usr/bin/env python
#
# Copyright 2009-2012 Canonical Ltd.
#
# Authors: Neil Jagdish Patel <[email protected]>
#          Jono Bacon <[email protected]>
#          David Planella <[email protected]>
#
# 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 
# <http://www.gnu.org/licenses/>
#

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()

相关内容