如何编写一键指示符(添加中间点击function)?

我非常熟练地使用菜单创建应用程序指标。 在下面的示例中,有一个菜单项(退出)。 添加另一个项目,我的实际应用程序或要运行的行,我不会有任何问题。

但是,当我尝试做的是在单击指示器图标时使项目运行。 每次单击指示器图标时,它都将运行。 要删除指示符图标,用户将单击退出。

有人能告诉我在代码中运行我的行吗?

这是指标代码:

#!/usr/bin/python import os import signal import subprocess import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk as gtk gi.require_version('AppIndicator3', '0.1') from gi.repository import AppIndicator3 as appindicator APPINDICATOR_ID = 'appreveallauncher' def main(): indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath('sample_icon.svg'), appindicator.IndicatorCategory.SYSTEM_SERVICES) indicator.set_status(appindicator.IndicatorStatus.ACTIVE) indicator.set_menu(build_menu()) gtk.main() def build_menu(): menu = gtk.Menu() item_quit1 = gtk.MenuItem('Quit') item_quit1.connect('activate', quit) item_reveallauncher = gtk.MenuItem('Reveal Launcher') item_reveallauncher.connect('activate', reveallauncher) # This is my attempt to add the middle click functionality menu_items = gtk.MenuItem("Reveal Launcher Middle Click") menu.append(menu_items) menu_items.connect("activate", menu_items) menu_items.set_secondary_activate_target(menu_items) menu.append(item_reveallauncher) menu.append(item_quit1) menu.show_all() return menu def menu_items(_): subprocess.call("xdotool key alt+F1", shell=True) def reveallauncher(_): subprocess.call("xdotool key alt+F1", shell=True) def quit1(_): gtk.main_quit() if __name__ == "__main__": signal.signal(signal.SIGINT, signal.SIG_DFL) main() # last = self.get_last_menuitem(self.app_menu) # self.app.set_secondary_activate_target(last) 

这是一行:

 subprocess.call("xdotool key alt+F1", shell=True) 

运行上面的代码时出现错误:

 Traceback (most recent call last): File "/home/users/l/j/ljames/workspace/pythontest/src/basic.py", line 57, in  main() File "/home/users/l/j/ljames/workspace/pythontest/src/basic.py", line 19, in main indicator.set_menu(build_menu()) File "/home/users/l/j/ljames/workspace/pythontest/src/basic.py", line 35, in build_menu menu_items.connect("activate", menu_items) TypeError: second argument must be callable 

我想要实现的是在点击指标时让Ubuntu Launcher显示出来。

更新

我已经更新了上面的代码示例,以包含所需操作的行。 Sergiy Kolodyazhnyy在他的回答中指出了中间点击分辨率。 我在底部评论了必要的线条。 现在我正在尝试为这些行获得正确的语法和位置。

简短的回答是,你做不到。 单击指示器图标仅显示菜单。 可以做的是设置中间(滚轮)单击(在右键和左键单击按下的触摸板上),这通过设置指示器对象的辅助激活目标来完成。

这是我在第152和153行的自己的启动器列表指示器中使用的示例:

 152 last = self.get_last_menuitem(self.app_menu) 153 self.app.set_secondary_activate_target(last) 

当用户按下中间点击idicator项目时,它将激活特定菜单项而不关闭菜单。

您还可以选择使用滚动事件,例如:

 86 self.app.connect("scroll-event", self.set_next) 

要删除错误并将middleclickfunction应用于相关代码(虽然可能有许多替代方法),我在main si中的build_menu语句中添加了一个参数, build_menu定义可以使用参数。 我打电话给这个论点indicator 。 然后添加了set_secondary_activate_target()方法set_secondary_activate_target()

固定代码:

 #!/usr/bin/python import os import signal import subprocess import gi from middleClick import reveallauncher gi.require_version('Gtk', '3.0') from gi.repository import Gtk as gtk gi.require_version('AppIndicator3', '0.1') from gi.repository import AppIndicator3 as appindicator APPINDICATOR_ID = 'appreveallauncher' def main(): indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath('sample_icon.svg'), appindicator.IndicatorCategory.SYSTEM_SERVICES) indicator.set_status(appindicator.IndicatorStatus.ACTIVE) indicator.set_menu(build_menu(indicator)) gtk.main() def build_menu(indicator): menu = gtk.Menu() item_reveallauncher = gtk.MenuItem('Reveal Launcher') item_reveallauncher.connect('activate', reveallauncher) indicator.set_secondary_activate_target(item_reveallauncher) item_quit = gtk.MenuItem('Quit') item_quit.connect('activate', quit) menu.append(item_reveallauncher) menu.append(item_quit) menu.show_all() return menu def reveallauncher(_): subprocess.call("xdotool key alt+F1", shell=True) def quit(_): gtk.main_quit() if __name__ == "__main__": signal.signal(signal.SIGINT, signal.SIG_DFL) main() 

可以通过Google搜索找到set_secondary_activate_target方法的一些示例用法。 但是,我遇到的问题是它们都包含在以许多依赖行的方式使用的复杂脚本中。 这是一个努力将它放在一个最简化的示例中,以便下一个尝试使其适应其代码的人更容易。