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 @ 2023-12-29 09:27  txwtech  阅读(222)  评论(0编辑  收藏  举报