python pyinstaller打包exe实现控制台窗体关闭按钮启用与禁用--避免不经意关闭了窗口

python pyinstaller打包exe实现控制台窗体关闭按钮启用与禁用

 

 

 pycharm直接点run是会报错的:

 

复制代码
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import time

import win32console
import win32gui
import win32api
import win32con
from threading import Thread


gl_input_msg = ''


def btn_disable():
    """

    """
    hwnd = win32console.GetConsoleWindow()
    # hwnd = win32gui.FindWindow(None, 'main')
    print('hwnd:', hwnd)
    if hwnd:
        h_menu = win32gui.GetSystemMenu(hwnd, 0)
        if h_menu:
            win32gui.EnableMenuItem(h_menu, win32con.SC_CLOSE, win32con.MF_DISABLED)
    print('thread_running_hwnd:', hwnd)


def btn_enable():
    """

    """
    hwnd = win32console.GetConsoleWindow()
    if hwnd:
        hMenu = win32gui.GetSystemMenu(hwnd, 0)
        if hMenu:
            win32gui.EnableMenuItem(hMenu, win32con.SC_CLOSE, win32con.MF_ENABLED)


def set_windows_title(title):
    """

    :param title:
    """
    hwnd = win32console.GetConsoleWindow()
    print("hwnd:", hwnd)
    win32gui.SetWindowText(hwnd, title)

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.





if __name__ == '__main__':
    print_hi('PyCharm')
    set_windows_title('通信接口服务')
    # thread1 = Thread(target=btn_disable)
    # thread1.start()
    # time.sleep(0.3)
    #
    # thread2 = Thread(target=exit_window)
    # thread2.start()
    # time.sleep(0.3)
    b_sw = True
    while b_sw:
        key_in_msg = input("请输入close,open,exit:")
        if key_in_msg == 'close':
            btn_disable()
        if key_in_msg == 'open':
            btn_enable()
        if key_in_msg == 'exit':
            break



# See PyCharm help at https://www.jetbrains.com/help/pycharm/
复制代码

 

posted @   txwtech  阅读(320)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2022-12-29 c# 启动代码无法打开照片-Win10企业版LTSC图片打开方式里没有默认照片查看器
2019-12-29 c++ cc24a_demo //转换函数,用来做转换操作符,int()括号里面必须是空的,必须定义为const,代码示范
2019-12-29 VS2017未安装MFC解决方法
2019-12-29 program files (x86)\microsoft visual studio 14.0\vc\include\xtree,如果没有找到,下标溢出了,就报错咯
点击右上角即可分享
微信分享提示