需要備忘一下的是gui下代替sudo,su的程式是gksu.可惜是英文的介面.
還可以用glade設計gtk的介面,可惜我是寫完這個程式才知道.
2005.11.18
封裝了一個imagebutton出來,也算是對python的進一步學習吧~~~window的樣式也做了修改~~~現在基本上已經可以正常使用了~~~我把我那個醜陋的fvwm菜單的退出已經換為這個了~~
1#!/usr/bin/python
2import pygtk
3import gtk
4import os
5import sys
6
7class BaseWindow:
8
9 def delete_event(self, widget, data=None):
10 print "delete_event"
11
12 def destroy(self, widget, data=None):
13 gtk.main_quit()
14
15 def loginout(self, widget, data=None):
16 os.system("fvwm -r")
17
18 def shutdown(self, widget, data=None):
19 os.system("shutdown -h now")
20
21 def reboot(self, widget, data=None):
22 os.system("shutdown -r now")
23
24 def restart(self, widget, data=None):
25 os.system("fvwm -r")
26 def exit(self, widget, data=None):
27 sys.exit()
28
29 def __init__(self):
30 self.window = gtk.Window(gtk.WINDOW_POPUP)
31 self.window.set_title("xyShutdown")
32 self.window.set_position(gtk.WIN_POS_CENTER)
33 self.window.set_modal(True)
34 self.window.set_icon_name("gtk-quit")
35 self.window.set_resizable(False)
36 self.window.set_decorated(True)
37 self.window.set_keep_above(True)
38 self.window.connect("destroy", self.destroy)
39 self.window.connect("delete_event", self.delete_event)
40
41 self.boxv = gtk.VBox(False, 0)
42 self.window.add(self.boxv)
43
44 self.btnShutdown = xyImageButton("關閉電腦", "quit.xpm")
45 self.btnShutdown.connect("clicked", self.shutdown, None)
46 self.boxv.pack_start(self.btnShutdown, True, True, 0)
47
48 self.btnReboot = xyImageButton("重啟電腦", "restart.xpm")
49 self.btnReboot.connect("clicked", self.reboot, None)
50 self.boxv.pack_start(self.btnReboot, True, True, 0)
51
52 self.btnRestart = xyImageButton("退出登入", "restart.xpm")
53 self.btnRestart.connect("clicked", self.restart, None)
54 self.boxv.pack_start(self.btnRestart, True, True, 0)
55
56
57 self.btnCancel = xyImageButton("取消操作", "exit.xpm")
58 self.btnCancel.connect("clicked", self.exit, None)
59 self.boxv.pack_start(self.btnCancel, True, True, 0)
60
61 self.btnCancel.show()
62 self.btnRestart.show()
63 self.btnReboot.show()
64 self.btnShutdown.show()
65 self.boxv.show()
66 self.window.show()
67
68 def main(self):
69 gtk.main()
70
71
72class xyImageButton(gtk.Button):
73 def __init__(self, text, image):
74 gtk.Button.__init__(self)
75 hbox = gtk.HBox(False, 0)
76 img = gtk.Image()
77 img.set_from_file(image)
78 img.show()
79 hbox.pack_start(img, True, True, 0)
80 lbl = gtk.Label(text)
81 lbl.show()
82 hbox.pack_start(lbl, True, True, 0)
83 hbox.show()
84 self.add(hbox)
85
86baseWindow = BaseWindow()
87baseWindow.main()
88