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