研究Python的同时顺便做了个小工具,可以显示当前系统的一些信息
由于使用的都是Linux系统命令,所以提供源代码,Windows需要的同学自己修改一下就行啦
以下代码在Lubuntu 12.04 下测试通过(屏幕1366*768),还需要自己准备一幅文件名为“close.png”的图片放到家目录
PS:需要Python和wxPython的支持
#!/usr/bin/python
#coding=utf-8
import wx
import time
import threading
import commands
class MainFrame(wx.Frame):
cutMoveStep=0
IsShowOut=False
def __init__(self):
self.ScreenSize=wx.DisplaySize()
self.ScreenWidth=self.ScreenSize[0]-300
self.ScreenHeight=self.ScreenSize[1]
wx.Frame.__init__(self, None, -1, "BootReport", pos=(self.ScreenWidth, self.ScreenHeight), size=(200,300), style=wx.FRAME_SHAPED | wx.NO_BORDER | wx.FRAME_NO_TASKBAR)
if wx.Platform == "__WXGTK__":
self.Bind(wx.EVT_WINDOW_CREATE, self.SetBalloonShape)
else:
self.SetBalloonShape()
panel = wx.Panel(self, -1)
panel.SetBackgroundColour(wx.Colour(0, 180, 207))
closeBtnBack = wx.Image("~/close.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
self.closeBtn = wx.BitmapButton(panel, wx.ID_ANY, closeBtnBack,(168,0), (32,32),style=wx.NO_BORDER)
panel.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.closeBtn)
wx.StaticText(panel, -1, "上次开机时间", pos=(10, 40))
stat, content = commands.getstatusoutput('last | grep reboot | awk \'{print $6}\' | head -2 | tail -1')
if content=='Jan':
content='1'
elif content=='Feb':
content='2'
elif content=='Mar':
content='3'
elif content=='Apr':
content='4'
elif content=='May':
content='5'
elif content=='Jun':
content='6'
elif content=='Jul':
content='7'
elif content=='Aug':
content='8'
elif content=='Sep':
content='9'
elif content=='Oct':
content='10'
elif content=='Nov':
content='11'
elif content=='Dec':
content='12'
month=content
stat, content = commands.getstatusoutput('last | grep reboot | awk \'{print $7}\' | head -2 | tail -1')
day=content
stat, content = commands.getstatusoutput('last | grep reboot | awk \'{print $8}\' | head -2 | tail -1')
wx.StaticText(panel, -1, month+'月'+day+'日 '+content, pos=(60,60),size=(100,100)).SetForegroundColour(wx.Colour(255, 100, 100))
wx.StaticText(panel, -1, "上次运行时长", pos=(10,100))
stat, content = commands.getstatusoutput('last | grep reboot| awk \'{print $11}\' | head -2 | tail -1')
content = content[1:len(content)-1]
wx.StaticText(panel, -1, content, pos=(60,120)).SetForegroundColour(wx.Colour(255, 100, 100))
wx.StaticText(panel, -1, "本次开机耗时", pos=(10,160))
stat, content = commands.getstatusoutput('awk -F. \'{print $1}\' /proc/uptime')
wx.StaticText(panel, -1, content+'秒', pos=(60,180)).SetForegroundColour(wx.Colour(255, 100, 100))
wx.StaticText(panel, -1, "当前进程总数", pos=(10,220))
stat, content = commands.getstatusoutput('expr `ps -e | wc -l` - 2')
wx.StaticText(panel, -1, content+'个', pos=(60,240)).SetForegroundColour(wx.Colour(255, 100, 100))
def OnCloseMe(self, event):
self.Hide()
self.Destroy()
app.ExitMainLoop()
def SetBalloonShape(self, event=None):
width, height = self.GetSize()
bmp = wx.EmptyBitmap(width,height)
dc = wx.BufferedDC(None, bmp)
dc.BeginDrawing()
dc.SetBackground(wx.Brush(wx.Colour(0,0,0), wx.SOLID))
dc.Clear()
dc.DrawRoundedRectangle(0, 0, width-1, height-1, 10)
dc.EndDrawing()
r = wx.RegionFromBitmapColour(bmp, wx.Colour(0,0,0))
self.hasShape = self.SetShape(r)
def MoveTo(self):
if(self.IsShowOut):
self.cutMoveStep-=1
self.Move((self.ScreenWidth, self.ScreenHeight-self.cutMoveStep))
else:
self.cutMoveStep+=1
self.Move((self.ScreenWidth, self.ScreenHeight-self.cutMoveStep))
class WindowMoveShowThread(threading.Thread):
def __init__(self, caller):
threading.Thread.__init__(self)
self.caller = caller
def run(self):
for i in range(300):
wx.CallAfter(self.caller.MoveTo)
time.sleep(0.005)
time.sleep(10)
self.caller.IsShowOut=True
for i in range(300):
wx.CallAfter(self.caller.MoveTo)
time.sleep(0.005)
app.ExitMainLoop()
app = wx.PySimpleApp()
frm = MainFrame()
frm.Show()
WindowMoveShowThread(frm).start()
app.MainLoop()
对python真的是很不适应啊
