In this Python tutorial, we will learn everything about Python Tkinter add function, how to use add function in Python Tkinter and we will also cover different examples related to add function. And, we will cover these topics.
- Python Tkinter add Image
- Python Tkinter add Two Numbers
- Python Tkinter add a label
- Python Tkinter add Text Box
- Python Tkinter add Menu Bar
- Python Tkinter add Text to Entry
- Python Tkinter add a Scrollbar To Frame Grid
- Python Tkinter add icon
If you are new to Python Tkinter, check out Python GUI Programming.
Python Tkinter add function
Let us see, how to use the add function in Python Tkinter. We will see, how we can use the add function with images, two numbers, textbox, etc.
Python Tkinter add Image
In this section, we will learn how to add images in Python Tkinter.
We want to add an image on the window screen. We create a window where we want to add an image. We add the image from the dialog box. Select the image and open it. The selected image will add to the window.
Code:
in the following code, we create a window ws=Tk() Inside the window we add a button that functions like an event which helps to select an image that we want to add inside a box.
- Image. open() is used to open an image on the window.
- filedialog.askopenfilename() is used here to ask to open which file name from our local system location we want to open.
- Image.resize() helps to size the image to the defined px.
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os
ws = Tk()
ws.title("Python Guides")
ws.geometry("550x300+300+150")
ws.resizable(width=True, height=True)
def openfun():
filename = filedialog.askopenfilename(title='open')
return filename
def open_img():
a = openfun()
imag = Image.open(a)
imag = imag.resize((250, 250), Image.ANTIALIAS)
imag = ImageTk.PhotoImage(imag)
label = Label(ws, image=imag)
label.image = imag
label.pack()
button = Button(ws, text='Add Image', command=open_img).pack()
ws.mainloop()
Output:
In the following output, we can see a button with the label Add Image which functions to select the path of the image and input inside a box.
As earlier discussed while clicking on add image button how its function works. It is asking us from the path which image we want to add or open inside a window.
After selecting an image in this way it shows on over the window and our image is added inside a window.
Read How to Set Background to be an Image in Python Tkinter
Python Tkinter Add Two Numbers
In this section, we learn how to add two numbers in Python Tkinter.
The addition is taking two or more numbers and adding the numbers together. After adding the numbers we get the result.
Code:
In the following code, we create a window inside the window we add labels, entries, and buttons. The user gives the input in the entry box and the result is shown as the addition of the numbers given by the user.
Label() function is used to implement display boxes where we can place text or images.
entry() widget is used to accept single text.
from tkinter import *
def addnumber():
res1=int(entry1.get())+int(entry2.get())
mytext.set(res1)
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")
mytext=StringVar()
Label(ws, text="First").grid(row=0, sticky=W)
Label(ws, text="Second").grid(row=1, sticky=W)
Label(ws, text="Result:").grid(row=3, sticky=W)
result=Label(ws, text="", textvariable=mytext).grid(row=3,column=1, sticky=W)
entry1 = Entry(ws)
entry2 = Entry(ws)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
button = Button(ws, text="Calculate", command=addnumber)
button.grid(row=0, column=2,columnspan=2, rowspan=2,sticky=W+E+N+S, padx=5, pady=5)
ws.mainloop()
Output:
After running the above code we get the following output we see a window inside the window there is an input box that gets the values from the user after getting the value then calculates and gives the result back to the user.
Check out, BMI Calculator Using Python Tkinter
Python Tkinter add a label
In this section, we will learn how to add a label in Python Tkinter.
The label is used to provide a message to the user about the widget. The text displayed on the widget can be changed by the user anytime. The label can use one font at a time.
Label(master,option)
master: This represents the parent window.
Code:
In the following code, we create a widget inside the widget we add a label that provides a message to the user. The user can change this text anytime they want.
Label() is used the user have to specify what to display a text or an image.
from tkinter import *
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")
label = Label(ws, text="Welcome To Python Guides Tutorial !!",font="arial" "bold")
label.pack()
ws.mainloop()
Output:
After running the above code we get the following output in which we see the label is highlighted in the widget.
Read, Python Tkinter Entry – How to use
Python Tkinter add Text Box
In this section, we will learn how to add a text box in Python Tkinter.
The text box allows the user to input text information to be used by the program. The user can insert multiple texts in the text box at a single time.
Code:
In the following code we create a widget inside the widget we add a text box in which the user can insert the text or by default text is a popup in the text field.
- inputtext.get() is used for getting the input from the user.
- Text() function is used for entering the text.
- Label() is used the user have to specify what to display a text or an image.
- Button()is used to submitting the text.
from tkinter import *
ws = Tk()
ws.geometry("500x300")
ws.title("Python Guides")
def take_input():
INPUT = inputtext.get("1.0", "end-1c")
print(INPUT)
if(INPUT == "120"):
Output.insert(END, 'Correct')
else:
Output.insert(END, "Wrong answer")
label = Label(text = "What is 24 * 5 ? ")
inputtext = Text(ws, height = 10,
width = 25,
bg = "light yellow")
Output = Text(ws, height = 5,
width = 25,
bg = "light cyan")
display = Button(ws, height = 2,
width = 20,
text ="Show",
command = lambda:take_input())
label.pack()
inputtext.pack()
display.pack()
Output.pack()
ws.mainloop()
Output:
After running the above code we get the following output in which the user can insert the number in the text field.
After entering the text click on the show button if the answer that the user inserts in the text field is correct then the next text field show correct otherwise it shows incorrectly.
The user can insert the correct value and the value is shown on the command prompt.
Read, Python Tkinter Radiobutton – How to use
Python Tkinter add Menu Bar
In this section, we will learn how to add Menu Bar in Python Tkinter.
The menu bar is located at the of the screen below the title bar containing a drop-down menu. The menu bar gives access to functions such as an Opening file, Edit.
Code:
In the following code, we import Toplevel, Button, Tk, Menu libraries and also create a window on the top of the window a menubar is placed containing the dropdown menu.
- file1.add_command() is used to add menu items to the menu.
- file1.add_separator() is used to separate the menu accordingly.
- menubar.add_cascade() is used to creating menu bar in proper way.
from tkinter import Toplevel, Button, Tk, Menu
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")
menubar = Menu(ws)
file1 = Menu(menubar, tearoff=0)
file1.add_command(label="New")
file1.add_command(label="Open")
file1.add_command(label="Save")
file1.add_command(label="Save as...")
file1.add_command(label="Close")
file1.add_separator()
file1.add_command(label="Exit", command=ws.quit)
menubar.add_cascade(label="File", menu=file1)
edit1 = Menu(menubar, tearoff=0)
edit1.add_command(label="Undo")
edit1.add_separator()
edit1.add_command(label="Cut")
edit1.add_command(label="Copy")
edit1.add_command(label="Paste")
edit1.add_command(label="Delete")
edit1.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=edit1)
help1 = Menu(menubar, tearoff=0)
help1.add_command(label="About")
menubar.add_cascade(label="Help", menu=help1)
ws.config(menu=menubar)
ws.mainloop()
Output:
After running the above code we get the following output in which we add 3 Popular menubars File, Edit, and Help.
In this picture, File, Edir is menubar and the menu options as New, Open, Save, Save as, etc.
Read, Python Tkinter Menu bar – How to Use
Python Tkinter add Text to Entry
In this section, we will learn how to add text to entry in Python Tkinter.
Entry is where we can input the text or we can say that text entry refers to creating a message in form of characters, numbers, etc, or sometimes by default message can also generate.
Code:
In the following code, we create a window ws=Tk() inside the window there is a text box. In which users can enter their text if the user will not provide any input then the default value will be used.
textBox.insert() is used to insert the word or sentence.
from tkinter import *
import tkinter as tk
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")
textBox = Entry(ws)
textBox.insert(0, "Python Guides..!!")
textBox.pack()
ws.mainloop()
Output:
The following output displays that text is already mentioned in the textbox. If users want to some another text added in the textbox then they remove this text first then enter otherwise “Python Guides..!!” text show by default.
Read How to make a calculator in Python
Python Tkinter add a Scrollbar To Frame Grid
In this section, we will learn about how to add a scrollbar to a frame grid in Python Tkinter.
A scrollbar is a widget in which text, Picture content can be scrolled in a predetermined direction(Horizontal or Vertical). When the content exceeds then the scrollbar is added.
Code:
In the following code, we create a window ws=Tk() we want to add a scrollbar we use Scrollbar() function to add scrollbar on the main window. Orient determines whether Scrollbar will be Vertical or Horizontal.
Scrollbar() is used for adding the Scrollbar to the main window.
from tkinter import *
class ScrollBar:
def __init__(self):
ws = Tk()
ws.title("Python Guides")
ws.geometry("200x200")
h1 = Scrollbar(ws, orient = 'horizontal')
h1.pack(side = BOTTOM, fill = X)
v1 = Scrollbar(ws)
v1.pack(side = RIGHT, fill = Y)
t1 = Text(ws, width = 15, height = 15, wrap = NONE,
xscrollcommand = h1.set,
yscrollcommand = v1.set)
for i in range(20):
t1.insert(END,"Python Guides Tutorial..!!\n")
t1.pack(side=TOP, fill=X)
h1.config(command=t1.xview)
v1.config(command=t1.yview)
ws.mainloop()
s1 = ScrollBar()
Output:
In the following output, we see there is vertical or Horizontal both the scrollbar is added on the window.
Read How to Take User Input and Store in Variable using Python Tkinter
Python Tkinter add icon
In this section, we will learn how to add icons in Python Tkinter.
The icon is similar to the logo it gives brand to any site. If the user wants to search any site they firstly see the icon of the site. An icon is added on the title bar with the Sitename.
Code:
In the following code, we create a window inside the window there is a title bar and the icon is added on the title bar. The title bar icon for the window is based on the name PhotoImage.
- PhotoImage() is used to display an image for an icon.
- ws.iconphoto() method is used to set the title bar icon.
from tkinter import *
from tkinter.ttk import *
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")
image = PhotoImage(file = 'Pyimage.png.crdownload')
ws.iconphoto(False, image)
button = Button(ws, text = 'Click Me !')
button.pack(side = TOP)
ws.mainloop()
Output:
After running the above code we get the following output in which we see the icon which is added to the title bar.
You may like the following Python Tkinter tutorials:
- Python Tkinter Exit Program
- How to go to next page in Python Tkinter Program
- How to read a text file using Python Tkinter
- Python Tkinter panel
- Python Tkinter after method
So, in this tutorial, we discussed Python Tkinter add function and we have also covered different examples. Here is the list of the examples that we have covered.
- Python Tkinter add Image
- Python Tkinter add Two Numbers
- Python Tkinter add a label
- Python Tkinter add Text Box
- Python Tkinter add Menu Bar
- Python Tkinter add Text to Entry
- Python Tkinter add a Scrollbar To Frame Grid
- Python Tkinter add icon
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.