English 中文(简体)
How might I grab all windows with a certain word in their titles?
原标题:

I m running gnome and have a program that spawns off a large number of separate processes each with its own gui window. I d like to be able to selectively grab open windows whose titles match a certain pattern to close them. Anyone know a way to do this easily ?

最佳回答

You definitely want to use python-wnck (for documentation, you might need to look for python-gnome-extras, or the Perl bindings, or just the plain C documentation). WNCK is written to make it easy to look at screens, workspaces, and windows. Something like this:

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

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()
for w in windows:
    if w.get_name() ==  foo :
        w.close(0)

...but I haven t tested it.

(Also, this won t be GNOME-specific. It works with any desktop environment.)

问题回答

Great stuff jleedev, here s a minor tweak to scriptify it it and use a pattern to match the windows.

#!/usr/bin/python

import pygtk
pygtk.require( 2.0 )
import gtk
import wnck
import re
import sys

if(len(sys.argv) < 2):
  print  A regex pattern is required to match window titles 
  print  Usage: wkill <regex> 
  sys.exit(1)

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

titlePattern = re.compile(sys.argv[1])

windows = screen.get_windows()
for w in windows:
  if titlePattern.match(w.get_name()):
    print "Closing window - ", w.get_name()
    w.close(0)




相关问题
How to start to create an application GUI using C#?

HI! I am new to C# and plan to use it for my application GUI. I am trying to make my GUI similar to SPSS:http://www.spss.com/images/08/statistics_screens/ez_rfm-big.jpg Is this easy in C#? Is there ...

Automatic height of edit box

My shoes application has three items stacked on top of each other (with a stack, of course), in order: A banner An edit box Two buttons in a flow What I want to do is have the banner stay at it s ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

UI Convention: Shortcut key for application exit? [closed]

Is there a convention for the shortcut keys for application exit? Some applications uses Alt+X some others use Ctrl+ X and Ctrl+Q. Applications like FF and IE doesnot assign a shortcut at all. So is ...

热门标签