(py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
[python/dscho.git] / Demo / tkinter / guido / kill.py
blob8f45a29bb90d2f87f25193a8a1079428511b032f
1 #!/usr/local/bin/python
2 # Tkinter interface to Linux `kill' command.
4 from Tkinter import *
5 from string import splitfields
6 from string import split
7 import commands
8 import posix
10 class BarButton(Menubutton):
11 _CNF = {Pack: {'side': 'left'}}
12 def __init__(self, master=None, cnf={}):
13 Menubutton.__init__(self, master, (self._CNF, cnf))
14 self.menu = Menu(self, {'name': 'menu'})
15 self['menu'] = self.menu
17 class Kill(Frame):
18 # List of (name, option, pid_column)
19 format_list = [('Default', '', 0),
20 ('Long', '-l', 2),
21 ('User', '-u', 1),
22 ('Jobs', '-j', 1),
23 ('Signal', '-s', 1),
24 ('Memory', '-m', 0),
25 ('VM', '-v', 0),
26 ('Hex', '-X', 0)]
27 def kill(self, selected):
28 c = self.format_list[self.format.get()][2]
29 pid = split(selected)[c]
30 posix.system('kill' + ' -9 ' + pid)
31 self.do_update()
32 def do_update(self):
33 name, option, column = self.format_list[self.format.get()]
34 s = commands.getoutput('ps -w ' + option)
35 list = splitfields(s, '\n')
36 self.header.set(list[0])
37 del list[0]
38 y = self.frame.vscroll.get()[2]
39 self.frame.list.delete(0, AtEnd())
40 for line in list:
41 self.frame.list.insert(0, line)
42 self.frame.list.yview(y)
43 def do_motion(self, e):
44 e.widget.select_from(e.widget.nearest(e.y))
45 def do_leave(self, e):
46 e.widget.select_clear()
47 def do_1(self, e):
48 self.kill(e.widget.get(e.widget.nearest(e.y)))
49 def __init__(self, master=None, cnf={}):
50 Frame.__init__(self, master, cnf)
51 self.pack({'expand': 'yes', 'fill': 'both'})
52 self.bar = Frame(
53 self,
54 {'name': 'bar',
55 'relief': 'raised',
56 'bd': 2,
57 Pack: {'side': 'top',
58 'fill': 'x'}})
59 self.bar.file = BarButton(self.bar, {'text': 'File'})
60 self.bar.file.menu.add_command(
61 {'label': 'Quit', 'command': self.quit})
62 self.bar.view = BarButton(self.bar, {'text': 'View'})
63 self.format = IntVar(self)
64 self.format.set(2)
65 for num in range(len(self.format_list)):
66 self.bar.view.menu.add_radiobutton(
67 {'label': self.format_list[num][0],
68 'command': self.do_update,
69 'variable': self.format,
70 'value': num})
71 #self.bar.view.menu.add_separator()
72 #XXX ...
73 self.bar.tk_menuBar(self.bar.file, self.bar.view)
74 self.frame = Frame(
75 self,
76 {'relief': 'raised', 'bd': 2,
77 Pack: {'side': 'top',
78 'expand': 'yes',
79 'fill': 'both'}})
80 self.header = StringVar(self)
81 self.frame.label = Label(
82 self.frame,
83 {'relief': 'flat',
84 'anchor': 'nw',
85 'borderwidth': 0,
86 'textvariable': self.header,
87 Pack: {'side': 'top',
88 'fill': 'x'}})
89 self.frame.vscroll = Scrollbar(
90 self.frame,
91 {'orient': 'vertical'})
92 self.frame.list = Listbox(
93 self.frame,
94 {'relief': 'sunken',
95 'selectbackground': '#eed5b7',
96 'selectborderwidth': 0,
97 'yscroll': self.frame.vscroll.set})
98 self.frame.vscroll['command'] = self.frame.list.yview
99 self.frame.vscroll.pack({'side': 'right', 'fill': 'y'})
100 self.frame.list.pack(
101 {'side': 'top',
102 'expand': 'yes',
103 'fill': 'both'})
104 self.update = Button(
105 self,
106 {'text': 'Update',
107 'command': self.do_update,
108 Pack: {'expand': 'yes',
109 'fill': 'x'}})
110 self.frame.list.bind('<Motion>', self.do_motion)
111 self.frame.list.bind('<Leave>', self.do_leave)
112 self.frame.list.bind('<1>', self.do_1)
113 self.do_update()
115 if __name__ == '__main__':
116 kill = Kill(None, {'bd': 5})
117 kill.winfo_toplevel().title('Tkinter Process Killer')
118 kill.winfo_toplevel().minsize(1, 1)
119 kill.mainloop()