HOME/Articles/

tkinter example embeddedImage (snippet)

Article Outline

Python tkinter (gui) example 'embeddedImage'

Modules used in program:

  • import base64

embeddedImage

Python tkinter example: embeddedImage

import base64
from tkinter import *

# This is a base64 encoded GIF image. One neat trick with using Python to create GUIs
# is you can embed your graphics into the code so anything you want to display doesn't
# need to be pre-loaded or downloaded separately. You can only use GIFs in this way.
gifBase = '''
R0lGODlhQABAAPcAAO3t7RoaGg8PDw4ODqOho9TS1NbV1q2sraemp6alprSztRIRFBMTFw8PERERExUVFxoaHA4ODxMTFBUVFhkZGg0OFRARFxgZHx8hKxka
HxwdIhcaJhASGg4PEx0fJxARFR0eIiIjJwsNFBMWIRodKBwfKhIVHxQZKBUXHRkbIQ8QExESFRYXGhcYGxgZHBobHh8gIwwOEyMlKhMVGSAiJhMUFhscHiQl
JxETFhcZHBgaHRESExwdHhgZGRkaGh0eHiEiIicoKCUmJsPExMHCwqWmpg8QD7u8uyIiHhoaGaurqpWVlP/2Tv/yWv7uY/3nS/7jO/zkV9a3JbqcGb2eIvHN
N/3eXRwbFx4dGcumJeW9KxoYEdeqGoVpEYttEqCAFsOdHLeRGo9zFZp7GKeFG4RqGdWsKm1ZF9mxNu3HUM6cEpRzEM+hGndcD4FkE2BLD3xiFE8+DXRcFVRD
FFxJFlhHFy4lDdOtQ9+4TCciExoXDiQhGcmSD8CPEItnDKuAEJFtDsmVFdCaGG5TDltFDMWTG8uYHpJvF0k4DYxsGmVQGVA/FDktD59+K0AzEk4/GEc5Fr2Y
PCkhDkE1GDowFi0mFDgwHBUTDhcVECspJLqHD6Z6FaV9IcybKltFE5BvJEs6FGBLGjMoDolrJ5h4LaiGN0k6GSQdDjUsFx8aDyEeF0c1Eo9wL2pTJHxiLVVD
IIltNUY5HjIpFikmIBwaFl5JIU4+ISokGRMRDxUTERcVExEQDxMSEScmJRoZGdbV1bOysuzs7Orq6unp6eHh4dra2tbW1tXV1dTU1NPT09HR0c7OzsvLy8nJ
ycbGxr6+vry8vLu7u7q6uri4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Cs4IYk83CMA+ZiHEeQhABekohNukCz/PewhAXpgohL5YAEjIjGIUrShFHLIQyoeUIMtnKIW7I+EIyqBiwDAkFdmdw8s
sApQMAYPIAEssAVZoAV5wAE1EADXdw/zIICiUAVhMH5OYg89gID3MAOeAAWaUAV9YIETsAJOIgEWcAggCAVvwAEOMA9mlkncYAQBIAA9gAuOkApDRQ8OYAei
IAFHBVUUIAA12ANGQAmn8ABMZR9BRQ82eAmOoAdRyAinMAG3ZR81kApR6Ai4QA8D0AP/d2b3AA8SEA8SkAESwAP2AF9e1gPzUA8dxQMS4EtmmAMSAF8B0ANu
uIb24EsX4AB/eAF3WA9GIjU1sF8XsAPxMIYCxiun/6VapgWJq6UPY1WJlHiJl2iJblWJbfVWmyUARqBioTiKlWVZqEVafiWJkFhWluiJNWIE+KBcsjiLtCiL
o3iLoKhipZhaqnhYicUPbMVW/DCMxFiM/NAPyJiMyriMyugPzoiM/rCMzhiNz+gPKZMA50AO2riN2jgO3uiN4hCO4RgO5FiO5niO5yiO4fiN3siN2lgO5HAO
4pAoSpANQBEUQkEU0hANa9EWcCEXdZEMybAXvwAaBnkMyCCQqzEYhYEYsQEN0hAVwEAVQIEA3TEMStAN0ZENRZAAPPET+GgW03AZbOEWhxEXKBkXh4EYibEW
0BANUFENE2kNPrETCKAVx3TRDd0hDNbwDb3xG9ZRE9mgHDvRE0AxkdVgFmdhFFARFVNBkVZxFdeQAFvBDdYBHF/xDeAQFgAQDMcADumADmI5lmRZlmZ5lmiZ
lmmZDuegBC4hFseQknI5l3RZl3Z5l3TJDMTAEXzZl375l4AZmII5mIEZEAA7
'''

root = Tk()
bgColor = "#EDEDED"

# Here we are loading our base64 image data and placing it in our window using
        # the Label widget.  You will also see the grid method. There are three styles
        # of window management in Tkinter. Using grid allows us to place widgets according
        # to X,Y coordinates which makes designing your GUI as easy as plotting it out on
        # a sheet paper before writing your code.
gif = PhotoImage(data = gifBase)
displayGif = Label(root, image = gif, borderwidth = 10, bg = bgColor).grid(row = 0, rowspan = 6, columnspan = 2)

root.mainloop()