|
收集一些Python操作windows的代码 (不管是自带的or第三方库)均来自网上
1.shutdown 操作
定时关机、重启、注销
#!/usr/bin/python
#-*-coding:utf-8-*-
#shutdown.py
import sys#导入
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class ShutDown(QWidget):
def __init__(self):
super(ShutDown, self).__init__()
self.setWindowTitle('PyQt')
self.hourLabel = QLabel(u'小时')#标签
self.minuteLabel = QLabel(u'分钟')
self.secondLabel = QLabel(u'秒')
self.shutDownButton = QPushButton()#按钮
self.shutDownButton.setText(u'关机')
self.shutDownButton.clicked.connect(self.confirm_shutdown)
self.rebootButton = QPushButton()
self.rebootButton.setText(u'重启')
self.rebootButton.clicked.connect(self.confirm_reboot)
self.logoffButton = QPushButton()
self.logoffButton.setText(u'注销')
self.logoffButton.clicked.connect(self.confirm_logoff)
self.hourSpinBox = QSpinBox()
self.hourSpinBox.setRange(0,1000)
self.hourSpinBox.setSingleStep(1)
self.hourSpinBox.setValue(0)
self.minuteSpinBox = QSpinBox()
self.minuteSpinBox.setRange(0,1000)
self.minuteSpinBox.setSingleStep(1)
self.minuteSpinBox.setValue(0)
self.secondSpinBox = QSpinBox()
self.secondSpinBox.setRange(0,1000)
self.secondSpinBox.setSingleStep(1)
self.secondSpinBox.setValue(0)
self.layout = QGridLayout()#网格布局
self.layout.addWidget(self.hourLabel,1,0)
self.layout.addWidget(self.hourSpinBox,1,1)
self.layout.addWidget(self.minuteLabel,1,2)
self.layout.addWidget(self.minuteSpinBox,1,3)
self.layout.addWidget(self.secondLabel,1,4)
self.layout.addWidget(self.secondSpinBox,1,5)
self.layout.addWidget(self.shutDownButton,2,0,2,2)
self.layout.addWidget(self.rebootButton,2,2,2,2)
self.layout.addWidget(self.logoffButton,2,4,2,2)
self.setLayout(self.layout)
def confirm_shutdown(self):
self.time = (self.hourSpinBox.value() * 3600 + self.minuteSpinBox.value() * 60
+ self.secondSpinBox.value())
shutdown = 'C:\Windows\System32\shutdown.exe -s -t ' + str(self.time)
os.system(shutdown)
sys.exit(0)
def confirm_reboot(self):
reboot = 'C:\Windows\System32\shutdown.exe -r'
os.system(reboot)
sys.exit(0)
def confirm_logoff(self):
logoff = 'C:\Windows\System32\shutdown.exe -l'
os.system(logoff)
sys.exit(0)
if __name__ == '__main__':
app = QApplication(sys.argv)
shutdown = ShutDown()
shutdown.show()
sys.exit(app.exec_())
2.windows锁屏
相当与WIN+L 操作
1 # -*- coding: utf-8 -*-
2 # python在windows锁屏的代码
3 import sys
4 from ctypes import *
5 from PyQt4 import QtGui, QtCore
6 try:
7 _fromUtf8 = QtCore.QString.fromUtf8
8 except AttributeError:
9 def _fromUtf8(s):
10 return s
11
12 class QuitButton(QtGui.QWidget):
13 def __init__(self, parent=None):
14 QtGui.QWidget.__init__(self, parent)
15 self.setGeometry(300, 300, 250, 150)
16 self.setWindowTitle(_fromUtf8('windows锁屏')) #utf8 输出中文
17 lockscreen = QtGui.QPushButton('OK', self)
18 lockscreen.setGeometry(10, 10, 60, 35)
19 QtCore.QObject.connect(lockscreen, QtCore.SIGNAL('clicked()'), self.win_L) #self.win_L 点击执行
20 def win_L(self):
21 user32 = windll.LoadLibrary('user32.dll')
22 user32.LockWorkStation()
23
24 class lockScr(QtGui.QWidget):
25 def __init__(self):
26 QtGui.QWidget.__init__(self)
27 def win_L(self):
28 user32 = windll.LoadLibrary('user32.dll')
29 user32.LockWorkStation()
30
31 app = QtGui.QApplication(sys.argv)
32 qb = QuitButton()
33 qb.show()
34 sys.exit(app.exec_())
3.获取电脑的基本信息
引用第三方库WMI 可以查看到 windows当前的版本、位数X86(64)、进程数目、CPU类型和使用率、硬盘的分区和使用率、Ip地址和Mac地址、 详细进程数据等
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import wmi
5 import os
6 import sys
7 import platform
8 import time
9 import sys
10 reload(sys)
11 sys.setdefaultencoding( "utf-8" )
12 def sys_version():
13 c = wmi.WMI ()
14 #获取操作系统版本
15 for sys in c.Win32_OperatingSystem():
16 print u"Version:%s" % sys.Caption,"Vernum:%s" % sys.BuildNumber
17 print sys.OSArchitecture#系统是32位还是64位的
18 print sys.NumberOfProcesses #当前系统运行的进程总数
19
20 def cpu_mem():
21 c = wmi.WMI ()
22 #CPU类型和内存
23 for processor in c.Win32_Processor():
24 #print "Processor ID: %s" % processor.DeviceID
25 print "Process Name: %s" % processor.Name.strip()
26 for Memory in c.Win32_PhysicalMemory():
27 print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576)
28
29 def cpu_use():
30 #5s取一次CPU的使用率
31 c = wmi.WMI()
32 while True:
33 for cpu in c.Win32_Processor():
34 timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
35 print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage)
36 time.sleep(5)
37 break
38
39 def disk():
40 c = wmi.WMI ()
41 #获取硬盘分区
42 for physical_disk in c.Win32_DiskDrive ():
43 for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
44 for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
45 print physical_disk.Caption, partition.Caption, logical_disk.Caption
46
47 #获取硬盘使用百分情况
48 for disk in c.Win32_LogicalDisk (DriveType=3):
49 print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))
50
51 def network():
52 c = wmi.WMI ()
53 #获取MAC和IP地址
54 for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
55 print "MAC: %s" % interface.MACAddress
56 for ip_address in interface.IPAddress:
57 print "ip_add: %s" % ip_address
58 print
59
60 #获取自启动程序的位置
61 for s in c.Win32_StartupCommand ():
62 print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)
63
64
65 #获取当前运行的进程
66 for process in c.Win32_Process ():
67 print process.ProcessId, process.Name
68
69 def main():
70 sys_version()
71 cpu_mem()
72 disk()
73 network()
74 #cpu_use()
75
76 if __name__ == '__main__':
77 main()
78 print platform.system()
79 print platform.release()
80 print platform.version()
81 print platform.platform()
82 print platform.machine()
|
|