Python 3 Tkinter Display Image (PNG,JPG,GIF) From Base64 Code Using PhotoImage Widget GUI Desktop App Full Project For Beginners

 

 

 

app.py

 

 

# use a base64 encoded string of a gif image in Tkinter
# (base64 converts binary data into string characters)
# good for small images, avoids having to send the image file
# the image string is created with the following simple
# Python code and is then copied and pasted to this program
# (tk only uses gif, not all gif formats work properly!)
"""

import base64
gif_file = "grape.gif"
print "grape_gif='''\\\n" + base64.encodestring(open(gif_file).read()) + "'''"

"""
# tested with Python25     EU      2/20/2007

import Tkinter as tk

grape_gif='''\
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai8P8A////
/////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva
0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJWuSCAQ30+vNTGcxnOIARj3eT
YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ+PjZUjaJOElKanqJyRrJyZgSKkokOs
NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs=
'''

root = tk.Tk()

gif_image = tk.PhotoImage(data=grape_gif)

b1 = tk.Button(root, image=gif_image)
b1.pack(pady=10)
# save the button image from garbage collection!
b1.image = gif_image

root.mainloop()

Leave a Reply