Jo-*_*tad 18 indicator python application-development
我正在编写一个需要使用指标的应用程序。我过去曾使用 PyGTK 和 GTK2 完成此操作,并使用本文档作为参考:https : //wiki.ubuntu.com/DesktopExperienceTeam/ApplicationIndicators#Python_version
但是,这只适用于 PyGTK 和 GTK2。从那时起事情发生了变化,我需要找到一些好的文档、教程或好的示例来了解它是如何工作的。
此外,前面提到的文档根本没有描述的一件事是如何向指标添加子菜单。我希望有人可以对此有所了解,以及如果使用相同的工具完成,如何与类别指标集成。
谢谢。
sag*_*ise 19
这是我的 gtk3 和 appindicator 的试用代码,它为GPaste创建了一个指标。
基本上,
from gi.repository import AppIndicator3 as AppIndicator
Run Code Online (Sandbox Code Playgroud)
为了将 appindicator 用于由 package 提供的 gtk3 应用程序gir1.2-appindicator3。
这是AppIndicator3文档。
pygtk将被 Gtk3 弃用,你必须通过GObject-Introspection路线在 python 中开发 Gtk3 应用程序。您可以参考PyGObject 文档。代替
import pygtk, gtk, gdk, gobject, pango
Run Code Online (Sandbox Code Playgroud)
等你应该做
from gi.repository import Gtk, Gdk, Pango, GObject
Run Code Online (Sandbox Code Playgroud)
为了研究工作的代码,你可以查看咔嚓已转移到从GTK2和用途gtk3 appindicator3。
还有一个包gir1.2-appindicator似乎与 using 相同,python-appindicator因为它们都提供 gtk2 应用程序的用法,即:
from gi.repository import AppIndicator
Run Code Online (Sandbox Code Playgroud)
或者
import appindicator
Run Code Online (Sandbox Code Playgroud)
这篇博文中也有一些信息。
Rob*_*ans 13
这是一个愚蠢的简单脚手架应用程序,它有一个配置窗口、一个主窗口和一个应用程序指示器。
#!/usr/bin/env python3.3
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator
class MyIndicator:
def __init__(self, root):
self.app = root
self.ind = appindicator.Indicator.new(
self.app.name,
"indicator-messages",
appindicator.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status (appindicator.IndicatorStatus.ACTIVE)
self.menu = Gtk.Menu()
item = Gtk.MenuItem()
item.set_label("Main Window")
item.connect("activate", self.app.main_win.cb_show, '')
self.menu.append(item)
item = Gtk.MenuItem()
item.set_label("Configuration")
item.connect("activate", self.app.conf_win.cb_show, '')
self.menu.append(item)
item = Gtk.MenuItem()
item.set_label("Exit")
item.connect("activate", self.cb_exit, '')
self.menu.append(item)
self.menu.show_all()
self.ind.set_menu(self.menu)
def cb_exit(self, w, data):
Gtk.main_quit()
class MyConfigWin(Gtk.Window):
def __init__(self, root):
super().__init__()
self.app = root
self.set_title(self.app.name + ' Config Window')
def cb_show(self, w, data):
self.show()
class MyMainWin(Gtk.Window):
def __init__(self, root):
super().__init__()
self.app = root
self.set_title(self.app.name)
def cb_show(self, w, data):
self.show()
class MyApp(Gtk.Application):
def __init__(self, app_name):
super().__init__()
self.name = app_name
self.main_win = MyMainWin(self)
self.conf_win = MyConfigWin(self)
self.indicator = MyIndicator(self)
def run(self):
Gtk.main()
if __name__ == '__main__':
app = MyApp('Scaffold')
app.run()
Run Code Online (Sandbox Code Playgroud)
以防万一有人会觉得它有用,我用 Python、GIR 和 GTK3 制作了一个最小的应用程序指示器。它每隔几秒钟从 /proc/cpuinfo 读取 CPU 速度并显示它们。
见这里:https : //bitbucket.org/cpbotha/indicator-cpuspeed/src
小智 8
这是读取cpu温度的示例。在脚本目录中复制一个名为 temp-icon.png/svg 的图标
from gi.repository import Gtk, GLib
from gi.repository import AppIndicator3 as appindicator
import os
def cb_exit(w, data):
Gtk.main_quit()
def cb_readcputemp(ind_app):
# get CPU temp
fileh = open(
'/sys/devices/platform/thinkpad_hwmon/subsystem/devices/coretemp.0/temp1_input',
'r')
ind_app.set_label(fileh.read(2), '')
fileh.close()
return 1
ind_app = appindicator.Indicator.new_with_path (
"cputemp-indicator",
"temp-icon",
appindicator.IndicatorCategory.APPLICATION_STATUS,
os.path.dirname(os.path.realpath(__file__)))
ind_app.set_status (appindicator.IndicatorStatus.ACTIVE)
# create a menu
menu = Gtk.Menu()
menu_items = Gtk.MenuItem("Exit")
menu.append(menu_items)
menu_items.connect("activate", cb_exit, '')
menu_items.show()
ind_app.set_menu(menu)
GLib.timeout_add(500, cb_readcputemp, ind_app)
Gtk.main()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8352 次 |
| 最近记录: |