Hello guys, as usual we are sharing one more source code using which you can display images inside your GUI screen of tkinter
We will restrict ourselves to .png format only. For .jpg format, we have given an example as comments
Tkinter GUI is a python library which is designed in such a way that it’s use is very easy and also there you will take very less time to learn it
In this code, we have imported full tkinter using
from tkinter import *
Then we have created a reference to create the root to the screen using
root = Tk()
Now let’s set the screen size using
root.geometry("500x500")
Note: Here ‘x‘ is small case letter
After setting size, we are loading image using PhotoImage, then assigning an image to the label and finally packing it
photo = PhotoImage(file="imagename.png")
myimage = Label(image=photo)
myimage.pack()
Then we set a simple text using Label widget and pack it as well
text = Label(text="this is my gui") text.pack()
Now, to call the GUI screen we need to type
root.mainloop()
Now let’s have a look at the full source code.
Full Code
from tkinter import * root = Tk() # width x height root.geometry("550x550") photo = PhotoImage(file="imagename.png") myimage = Label(image=photo) myimage.pack() # for jpg format # image = Image.open("imagename.jpg") # photo = ImageTk.PhotoImage(image) # myimage = Label(image=photo) # myimage.pack() text = Label(text="this is my gui") text.pack() root.mainloop()
Output
Thanks for reading
Keep updated with us for more amazing free source codes like this.
Also read:
- See connected wifi passwords using Python
- Simple Text-To-Speech In Python
- GUI Calculator Using Python tkinter
- Password Generator Application
- Creating User-defined Entry Widgets
- GUI Stone Paper Scissor Game: Python
- GUI Age Calculator
- Changing Screen Size: Tkinter
- Simple Music Player Using Python
- Adding three matrices
- Auto-Login with Python