您如何在Python代码中更改Windows控制台图标?

发布于 2025-01-20 08:53:21 字数 111 浏览 2 评论 0原文

我可以将此代码添加到我的 python 脚本中来配置 Windows 控制台。 如何更改默认的 Windows 控制台图标?

os.system('模式 con: cols=90 行=28')

I can add this code to my python script to configure the windows console.
How do you change the default windows console icon?

os.system('mode con: cols=90 lines=28')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-01-27 08:53:21

您可以更改窗口的图标,但您需要知道窗口的标题。下面是一些代码,用于设置标题,等待其应用,然后更新图标。

import win32gui
import os
import time

def set_window_title(window_title_string, wait_for_change=False):
    os.system("title " + window_title_string)
    if (wait_for_change):
        matched_window = 0
        while (matched_window == 0):
            matched_window = win32gui.FindWindow(None, window_title_string)
            time.sleep(0.025) # To not flood it too much...
    
    return window_title_string

def set_window_icon(window_title, image_path):
    hwnd = win32gui.FindWindow(None, window_title)
    icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
    hicon = win32gui.LoadImage(None, image_path, win32con.IMAGE_ICON, 0, 0, icon_flags)
    
    win32gui.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_SMALL, hicon)
    win32gui.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_BIG, hicon)

def set_title_and_icon(window_title, icon_path):
    """Set the window title, wait for it to apply, then adjust the icon."""
    window_title = set_window_title(window_title, wait_for_change=True)
    set_window_icon(window_title, image_path)
    return window_title

set_title_and_icon("Cool Title Name", "C:\Users\youruser\an_icon.ico")

如果您想在 set_window_title 中设置标题时添加唯一 ID 或类似的内容,则会传回 window_title 变量。

You can change the icon of the window, but you need to know the window's title. Here's some code that will set the title, wait for it to apply, then update the icon.

import win32gui
import os
import time

def set_window_title(window_title_string, wait_for_change=False):
    os.system("title " + window_title_string)
    if (wait_for_change):
        matched_window = 0
        while (matched_window == 0):
            matched_window = win32gui.FindWindow(None, window_title_string)
            time.sleep(0.025) # To not flood it too much...
    
    return window_title_string

def set_window_icon(window_title, image_path):
    hwnd = win32gui.FindWindow(None, window_title)
    icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
    hicon = win32gui.LoadImage(None, image_path, win32con.IMAGE_ICON, 0, 0, icon_flags)
    
    win32gui.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_SMALL, hicon)
    win32gui.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_BIG, hicon)

def set_title_and_icon(window_title, icon_path):
    """Set the window title, wait for it to apply, then adjust the icon."""
    window_title = set_window_title(window_title, wait_for_change=True)
    set_window_icon(window_title, image_path)
    return window_title

set_title_and_icon("Cool Title Name", "C:\Users\youruser\an_icon.ico")

The window_title variable is passed back in case you wanted to add a unique ID or something similar when the title is set in set_window_title.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文