In this Python TKinter article we are going to learn How to Build TextBox in Python TKinter,
basically we are going to create a simple application in tkinter python that can add two
numbers and as you know for writing the numbers we need to create two TKinter TextBox.
in TKinter we can use ttk.Entry() for creating TextBox.
All right these are the imports that we need for this tutorial.
1 2 |
import tkinter as tk from tkinter import ttk |
We want to give a title for our TKinter window, so you can use this code for giving the
title for the TKinter window.
1 |
self.title("Build TextBox - Geekscoders.com") |
You can set the min size for tkinter window.
1 |
self.minsize(350,250) |
Now we want to add an icon for the window, make sure that you have already added an icon in your working directory, as i have already added an icon.
1 |
self.iconphoto(False, tk.PhotoImage(file='python.png')) |
Basically we are going to create three labels in our window, the first and second labels
are for the TextBox, and the third label is for the result of adding two numbers. also we have
added our labels in the grid layout.
1 2 3 4 5 6 7 8 9 |
self.label = ttk.Label(self, text = "Enter Your Name") self.label2 = ttk.Label(self, text = "Enter Your Name") self.label.grid(column = 0, row = 0) self.label2.grid(column=1, row=0) self.label3 = ttk.Label(self, text="Result") self.label3.grid(column=0, row=3) self.label3.config(font=("Courier", 15)) |
We need to get the values from python tkinter textbox.
1 2 |
self.first = tk.IntVar() self.second = tk.IntVar() |
In here we have created our two TextBoxes.
1 2 |
self.textbox = ttk.Entry(self, width = 20, textvariable = self.first) self.textbox2 = ttk.Entry(self, width=20, textvariable=self.second) |
Also we need to create a button for adding the two numbers, also i have connected the
button with the click_me() method, because in this method we want to do our adding
operations.
1 2 |
self.button = ttk.Button(self, text = "Add", command = self.click_me) self.button.grid(column=0, row=2) |
In this method first we have received the two numbers from tkinter textbox, after that we
have added these two numbers and at the end we have printed the result in our label.
1 2 3 4 5 |
def click_me(self): num1 = self.first.get() num2 = self.second.get() add = num1 + num2 self.label3.configure(text = "Result Is : " + str(add)) |
- TKinter Tutorial For Beginners
- PyQt5 Tutorial – Build GUI in Python with PyQt5
- Python Speech Recognition For Beginners
And now this is the complete code for Python TKinter TextBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import tkinter as tk from tkinter import ttk class TextBoxWindow(tk.Tk): def __init__(self): super(TextBoxWindow, self).__init__() self.title("Build TextBox - Geekscoders.com") self.minsize(350,250) self.iconphoto(False, tk.PhotoImage(file='python.png')) self.text_box() def click_me(self): num1 = self.first.get() num2 = self.second.get() add = num1 + num2 self.label3.configure(text = "Result Is : " + str(add)) def text_box(self): self.first = tk.IntVar() self.second = tk.IntVar() self.label = ttk.Label(self, text = "Enter Your Name") self.label2 = ttk.Label(self, text = "Enter Your Name") self.label.grid(column = 0, row = 0) self.label2.grid(column=1, row=0) self.label3 = ttk.Label(self, text="Result") self.label3.grid(column=0, row=3) self.label3.config(font=("Courier", 15)) self.textbox = ttk.Entry(self, width = 20, textvariable = self.first) self.textbox2 = ttk.Entry(self, width=20, textvariable=self.second) self.textbox.focus() self.textbox.grid(column=0, row=1) self.textbox2.grid(column=1, row=1) self.button = ttk.Button(self, text = "Add", command = self.click_me) self.button.grid(column=0, row=2) window = TextBoxWindow() window.mainloop() |
Run the complete code and this is the result.
