Close the console Python win32

这篇小记介绍了如何在Windows环境下使用Python编写程序时隐藏控制台窗口,适用于各种运行形式。通过特定API调用,可以实现控制台窗口的隐藏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


自己写的,小记一下,以后好用


不管你运行的是什么样的形式,,,只要在win32下,都可以隐藏掉控制台,,

import win32gui, win32con, win32api
  
def _MyCallback( hwnd, extra ):
    windows = extra;
    temp=[];
    temp.append(hwnd);
    temp.append(win32gui.GetClassName(hwnd));
    temp.append(win32gui.GetWindowText(hwnd));
    temp.append(hex(hwnd));
    windows[hwnd] = temp;

#is_clost =True
def TestEnumWindows(closttitle,is_close):
    windows = {};
    win32gui.EnumWindows(_MyCallback, windows);
    print "Enumerated a total of  windows with %d classes" ,(len(windows))
    print '------------------------------';
    #print classes
    print '-------------------------------';
    if is_close :
        for item in windows :
            if windows[item][1]=="ConsoleWindowClass":
                if windows[item][2].endswith(closttitle):
                    print 111
                  #  win32gui.CloseWindow(windows[item][0]);
                    win32gui.SendMessage(windows[item][0], win32con.WM_CLOSE, None, None);
    else:
        for item in windows :
             if windows[item][1]=="ConsoleWindowClass":
                 if windows[item][2].endswith(closttitle):
                     #print windows[item][0]
                     win32gui.ShowWindow(windows[item][0],False);
        
             
        

#print "Enumerating all windows..."
#h=win32gui.FindWindow(None,'\xba\xec\xce\xe5')
#print hex(h)
TestEnumWindows('Regdean.exe',True)
#print "All tests done!"

TestEnumWindows('Regdean.exe',True)
true 为关闭窗体

false 为隐藏窗体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值