给tkinter程序添加左上角图标

2020年9月25日 / 8次阅读 / Last Modified 2020年9月25日
tkinter

tkinter是我唯一在使用的GUI框架,个人认为挺好用的。本文记录如何给GUI程序添加一个左上角的图标。直接代码:

icon_img = \
b'AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAMMOAADDDg'\
b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJRXvUhYJ98ESBfTwEAL2/xEC/P8RA/P/EQTx'\
b'+RQG9OwfDvKiOyrlHgAAAAAAAAAAAAAAAAAAAAAzZswFLRDjnSUI9f8jBfX/JQbw/yoL5d'\
b'krFuWfKhvqiikg7IcmGumZJhHvszEV4b9CIdk9AAAAAAAAAAB/f/8CPBLUsjkL7f81Ctj/'\
b'Ogve/z8Z2r0mGeqtEgj30BAH8/gPBf//CwT29BAH98knFe6cTzDUc39VvwwAAAAATxnGek'\
b'0Q3P9HDsb/TQ/V/1Qgx5dLQuI2IxPuTywY6zQuG+dCJRDofCEH6O4gBP3/HQX4/ysV57Z1'\
b'desNazrEGlkUt+xcEr7/XRLA/18budX/zJkFAAAAAAAAAAAAAAAAAAAAAAAAAABQKNYmNQ'\
b'3c5jIJ4P80CfD/PRrYknQmrZNtFqz/bRaq/3UZtf98L65cAAAAAAAAAAAAAAAAAAAAAAAA'\
b'AAAAAAAAAAAAAE0axl9ID9f/RAzO/0MPyfSIKqL1fxqb/4Eanf+CH531o1utGQAAAAAAAA'\
b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAB/P78QVxS571gRv/9WErz/liuR+ZIdjP+VHpD/kiOM'\
b'6MyZzAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGoaq9ZsFbT/aBWt/6czhK'\
b'CmIn//piGA/6YmgfevV54dAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKqq/wN7G53f'\
b'fxmk/3sZnv+/SH8ctiVu7b0mc//EKXj/tzt7ZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'\
b'AAAACSMJcvkB+S/I4dkf+OHZD/AAAAAMcxZHffLWn/0Spk/8wwZeS8XoQbAAAAAAAAAAAA'\
b'AAAAAAAAAAAAAAAAAAAAnSeCoqwiiv+gIIH/oCGB+wAAAAD//38C2TVZne4wVv/wMFn/3D'\
b'JX3t09XFvUVW4exmN/EuxCWyrwZWUjsTR8UroneP+zJHL/wyd6/7UveLgAAAAAxlSNCchO'\
b'eVTlPlaC9TVFzvkzRf//Nkn/7jRE/vM4SN/tQFCu00FqjccqaPDKKWb/1Spo/8kuaubIVH'\
b'shAAAAAAAAAADLNmJB1Tdes+g7VpHrQlKC8UJLg/FDToXmP1SF2zdbqtkrV/rkLlj/7jBb'\
b'/90zXNTTUGkpAAAAAAAAAAAAAAAAAAAAANw5VizqNU+67DJL7egxSebpMUjo5zBH/vcxSf'\
b'/wMUb/7jRK6uk8VYzXiXUNAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZKTVb5PUTY'\
b'/zQ7//wzOf/5Njz4+kVLqfFoYicAAAAAAAAAAAAAAAAAAAAA8AMAAMABAACAAAAAgAAAAA'\
b'PgAAAH8AAAB/AAAAf4AAAH8AAAB/AAAIPwAACAAAAAgAAAAMABAADgAwAA+A8AAA=='

gRoot = tk.Tk()
gRoot.title('title')
# get top left icon
try:
    _tmpfile = '_icon_tmp.ico'
    with open(_tmpfile,'wb') as f:
        f.write(base64.b64decode(icon_img))
    gRoot.iconbitmap(_tmpfile)
    os.remove(_tmpfile)
except:
    pass

主要就是调用这个函数:gRoot.iconbitmap(_tmpfile)

代码也可以不用通过base64来存放图片文件,直接用一个独立的文件可能更简单。我只是比较喜欢这样。

-- EOF --

本文链接:https://www.pynote.net/archives/2505

留言区

电子邮件地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top