如何在Ubuntu中使用PyGTK或GTK打开列表?

joh*_*ohn 7 python gtk ubuntu pygtk window-management

如何在PyGTK或GTK或其他编程语言中打开列表?在Ubuntu?

编辑:

我想在桌面上获取列表路径打开​​的目录!

Mes*_*ion 11

欢迎来到2013!这是使用的代码Wnck及其现代GObject Introspection库,而不是现在弃用的PyGTK方法.您也可以查看我关于wnck的其他答案:

from gi.repository import Gtk, Wnck

Gtk.init([])  # necessary only if not using a Gtk.main() loop
screen = Wnck.Screen.get_default()
screen.force_update()  # recommended per Wnck documentation

# loop all windows
for window in screen.get_windows():
    print window.get_name()
    # ... do whatever you want with this window

# clean up Wnck (saves resources, check documentation)
window = None
screen = None
Wnck.shutdown()
Run Code Online (Sandbox Code Playgroud)

至于文档,请查看Libwnck参考手册.它不是特定于python,但使用GObject Introspection的重点是在所有语言中使用相同的API,这要归功于gir绑定.

此外,Ubuntu随附两个wnck及其相应的gir绑定开箱即用,但如果您需要安装它们:

sudo apt-get install libwnck-3-* gir1.2-wnck-3.0
Run Code Online (Sandbox Code Playgroud)

这也将安装libwnck-3-dev,这不是必需的,但会安装您可以使用DevHelp阅读的有用文档


San*_*ndy 9

您可能想要使用libwnck:

http://library.gnome.org/devel/libwnck/stable/

我相信在python-gnome或类似的包中有python绑定.

运行GTK + mainloop后,您可以执行以下操作:

import wnck
window_list = wnck.screen_get_default().get_windows()

该列表中窗口上的一些有趣方法是get_name()和activate().

当您单击按钮时,这将打印到控制台的窗口名称.但由于某种原因,我不得不点击按钮两次.这是我第一次使用libwnck,所以我可能会遗漏一些东西.:-)

import pygtk
pygtk.require('2.0')
import gtk, wnck

class WindowLister:
    def on_btn_click(self, widget, data=None):
        window_list = wnck.screen_get_default().get_windows()
        if len(window_list) == 0:
            print "No Windows Found"
        for win in window_list:
            print win.get_name()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.button = gtk.Button("List Windows")
        self.button.connect("clicked", self.on_btn_click, None)

        self.window.add(self.button)
        self.window.show_all()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    lister = WindowLister()
    lister.main()

  • 如果您的应用程序是非GUI,您可以在"wnck.get_screen_default()"之后执行"gtk.events_pending():gtk.main_iteration()"以刷新事件并能够获取窗口列表. (2认同)