1. 使用 os 模块调用外部程序
>>> import os
>>> os.system('notepad.exe')
0
>>> os.system('notepad D:\\Python\\test.txt')
也可使用 os 模块的 popen() 方法来打开外部程序,就不会出现命令提示符窗口。
>>> os.popen(r'C:\Windows\notepad.exe')
<os._wrap_close object at 0x032DEEC8>
2. 使用 win32api 模块调用 ShellExecute() 函数来启动外部程序
>>> import win32api
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '',0) # 0 表示在后台运行程序
42
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '',1)# 1 表示在前台运行程序
42
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', 'D:\\Python\\test.txt', '',1) # 传递参数打开文件
42
>>> win32api.ShellExecute(0, 'open', 'www.python.org', '', '',0) # 打开网址
42
>>> win32api.ShellExecute(0, 'open', 'D:\\Python\\test.txt', '', '',1) # 相对于双击文件
3. 通过创建进程来启动外部程序
>>> import win32process
>>> handle = win32process.CreateProcess(r'C:\Windows\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) # 打开记事本程序
>>> win32process.TerminateProcess(handle[0], 0) # 关闭刚才打开的程序
>>> handle = win32process.CreateProcess(r'C:\Windows\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO())
>>> import win32event
>>> win32event.WaitForSingleObject(handle[0], -1) # 需要手动关闭记事本
4 通过 ctypes 来调用动态链接库代码
ctypes 是 Python 处理动态链接库的标准扩展模块,运行在 Python 中调用动态链接库或共享库中的代码。
使用 Windows 动态链接库 user32.dll 中的 MessageBoxA() 函数来显示对话框。
>>> import ctypes
>>> ctypes.windll.user32.MessageBoxA(0, str.encode('Hello world!'), str.encode('Python ctypes'), 0)
调用标准 C 函数库 msvcrt 中的 printf() 函数。
>>> mascrt = ctypes.cdll.LoadLibrary('msvcrt')
>>> printf = mascrt.wprintf
>>> printf('Hello world!')
ctypes 提供了与 C 语言兼容的数据类型,但在 Python 中使用 C 语言的结构体时需要用类来改写。
718C3DDC47FA315EFF7561135338F91C.jpg
枚举进程列表。
from ctypes.wintypes import *
from ctypes import *
import collections
kernel32 = windll.kernel32
class tagPROCESSENTRY32(Structure):
_fields_ = [('dwSize', DWORD),
('cntUsage', DWORD),
('th32ProcessID', DWORD),
('th32DefaultHeapID', POINTER(ULONG)),
('th32ModuleID', DWORD),
('cntThreads', DWORD),
('th32ParentProcessID',DWORD),
('pcPriClassBase', LONG),
('dwFlags', DWORD),
('szExeFile', c_char * 260)]
def enumProcess():
hsnapshot = kernel32.CreateToolhelp32Snapshot(15, 0)
fProcessEntry32 = tagPROCESSENTRY32()
processClass = collections.namedtuple("processInfo", "processName processID")
processSet = []
if hsnapshot:
fProcessEntry32.dwSize = sizeof(fProcessEntry32)
listloop = kernel32.Process32First(hsnapshot, byref(fProcessEntry32))
while listloop:
processName = (fProcessEntry32.szExeFile)
processID = fProcessEntry32.th32ProcessID
processSet.append(processClass(processName, processID))
listloop = kernel32.Process32Next(hsnapshot, byref(fProcessEntry32))
return processSet
for i in enumProcess():
print(i.processName, i.processID)
调用 Windows 底层 API 函数来创建窗口。
import win32gui
from win32con import *
def WndProc(hwnd, msg, wParam, lParam):
if msg == WM_PAINT:
hdc, ps = win32gui.BeginPaint(hwnd)
rect = win32gui.GetClientRect(hwnd)
win32gui.DrawText(hdc, 'GUI Python', len('GUI Python'), rect,
DT_SINGLELINE|DT_CENTER|DT_VCENTER)
win32gui.EndPaint(hwnd, ps)
if msg == WM_DESTROY:
win32gui.PostQuitMessage(0)
return win32gui.DefWindowProc(hwnd, msg, wParam, lParam)
wc = win32gui.WNDCLASS()
wc.hbrBackground = COLOR_BTNFACE + 1
wc.hCursor = win32gui.LoadCursor(0, IDC_ARROW)
wc.hIcon = win32gui.LoadIcon(0, IDI_APPLICATION)
wc.lpszClassName = 'Python on Windows'
wc.lpfnWndProc = WndProc
reg = win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindow(reg, 'Python', WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, None)
win32gui.ShowWindow(hwnd, SW_SHOWNORMAL)
win32gui.UpdateWindow(hwnd)
win32gui.PumpMessages()