python - 如何使用字符串作为图标位图?

python - 如何使用字符串作为图标位图?,第1张

有没有办法在iconbitmap(Python 2.7.9)模块中为Tkinter使用字符串?

我知道你可以推广文件路径(尽管我还没有理解defaultbitmap之间的差异作为参数。

我问的原因是因为我想创建一个带有.exe的{​​{1}}的Python脚本(有效),但是我需要创建一个图标文件然后才能够使用图标。

感谢任何变通方法或其他方法。

最佳答案:

1 个答案:

答案 0 :(得分:2)

我不知道如何通过iconbitmap()除文件路径之外的任何其他方式。所以这是一个解决方法,它从图标文件的内容的字符串表示创建一个临时文件来传递它。它还显示了一种确保临时文件被删除的方法。

import atexit
import binascii
import os
import tempfile
try:
    import Tkinter as tk
except ModuleNotFoundError:  # Python 3
    import tkinter as tk


iconhexdata = '00000100010010100000010018006803000016000000280000001000000020' \
              '000000010018000000000040030000130b0000130b00000000000000000000' \
              'ffffff6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c' \
              '6c6d6c6c6d6c6c6d6c6c6d6c6c6dffffffffffff6c6c6d6c6c6d6c6c6d6c6c' \
              '6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d' \
              'ffffffffffff6c6c6d6c6c6dffffffffffffffffffffffffffffffffffffff' \
              'ffffffffffffffffffffff6c6c6d6c6c6dffffffffffff6c6c6d6c6c6dffff' \
              'ff6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6dffffff6c6c6d' \
              '6c6c6dffffffffffff6c6c6d6c6c6dffffff6c6c6d6c6c6d6c6c6d6c6c6d6c' \
              '6c6d6c6c6d6c6c6d6c6c6dffffff6c6c6d6c6c6dffffffffffff6c6c6d6c6c' \
              '6dfffffffffffffffffffffffffffffff2f4f7d6dfe9b8cadb95b2cfedf2f6' \
              '6c6c6d6c6c6dfffffffffffffffffffffffff4f7fac0d4e69bb9d6739dc657' \
              '89ba3e78b03f78af4177ad4276abd2deeaffffffffffffffffffffffffffff' \
              'ffffffffdfe9f24178ad4178ad4178ad5081b17398be9db8d3bed4e6bbd7ec' \
              'add7f3fffffffffffffffffffffffffffffffffffff8fafcaac2dac4d3e4df' \
              'e8f1f9fbfdfffffff4fafd91cff520a3f10297eee4f4feffffffffffffffff' \
              'ffffffffffffffffffffffffffffffffffffffe7f4fd7fcaf6159def0595ec' \
              '179fec82c7f4bad6f7fdfefffffffffffffffffffffffffffffffffdfeffdb' \
              'f0fd7bc8f6119bed0695eb1a9ded7ecaf5f0f8febfd3f73165e495b1f1ffff' \
              'fffffffffffffffffffffffffff6fbfe2fa6ee0695eb1b9eed86ccf5e8f6fd' \
              'ffffffd2dff93468e5326ae5c7d6f8ffffffffffffffffffffffffffffffff' \
              'ffff96d2f784cbf5eaf6fdffffffffffffe3eafb4275e72c66e4b6caf6ffff' \
              'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' \
              'f3f6fd5784ea2c66e499b5f2ffffffffffffffffffffffffffffffffffffff' \
              'fffffffffffffffffffffffffffffdfeff7097ed2c66e47a9eeeffffffffff' \
              'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffdfeff' \
              '85a7ef2c66e4608cebf9fbfeffffffffffffffffffffffff00000000000000' \
              '00000000000000000000000000000000000000000000000000000000000000' \
              '0000000000000000000000000000000000000000000000000000'

def on_closing(iconfile):
    try:
        os.remove(iconfile.name)
    except Exception:
        pass

with tempfile.NamedTemporaryFile(delete=False) as iconfile:
    iconfile.write(binascii.a2b_hex(iconhexdata))

# Register a clean-up function.
atexit.register(lambda file=iconfile: on_closing(file))

root = tk.Tk()
root.title('stackoverflow!')
root.iconbitmap(iconfile.name)

tk.Label(root, text='Note the custom icon').pack()
tk.Button(root, text='OK', bg='lightgreen', command=root.quit).pack()

root.mainloop()

显示的窗口将显示如下所示的自定义图标:

python - 如何使用字符串作为图标位图?,screenshot of tkinter window created,第2张

你没有问过怎么做,但这里是我用来将原始.ico文件转换为我的例子中使用的Python字符串变量的代码:

from __future__ import print_function
import binascii
try:
    from itertools import izip_longest as zip_longest
except ImportError:
    from itertools import zip_longest

iconfile = 'stackoverflow.ico'  # Path to icon file.
VAR_NAME = 'iconhexdata'
VAR_SUFFIX = ' = '
INDENTATION = ' ' * len(VAR_NAME VAR_SUFFIX)
MAX_LINE_LENGTH = 80
EXTRA_CHARS = '"" \'  # That get added to each group of hex digits.
LINE_LENGTH = MAX_LINE_LENGTH - len(INDENTATION) - len(EXTRA_CHARS)


def grouper(chunk_size, iterable):
    """ Collect data into fixed-length chunks or blocks.
        s -> (s0,s1,...sn-1), (sn,sn 1,...s2n-1), (s2n,s2n 1,...s3n-1), ...
    """
    return zip_longest(*[iter(iterable)]*chunk_size, fillvalue='')

with open(iconfile, 'rb') as imgfile:
    hexstr = [chr(x) for x in bytearray(binascii.b2a_hex(imgfile.read()))]

hexlines = (''.join(str(x) for x in group) for group in grouper(LINE_LENGTH, hexstr))

print(VAR_NAME   VAR_SUFFIX, end='')
print((' \\n'   INDENTATION).join((repr(line) for line in hexlines)))
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复