Lesson-5: Tkinter Entry Widget Function
Tkinter Entry Widget Function
With this tool, we will create a one-line Field in which the user can enter text. The use of this window tool is similar to the use of other tools. The Entry widget is used to allow the user to accept a value from the user of a single-line text box. We can use the Entry widget to accept text strings from the user. Available only for one line of text from the user. We must use the text widget for multiple lines of text.
Before starting the lesson, I recommend watching the video below.Tkinter Entry Widget Function
import tkinter as tk
pencere=tk.Tk()
giris = tk.Entry()
giris.pack()
pencere.mainloop()
As you can see, we have created an interface that allows the user to enter one line of text.Dec. If you want, now let’s make our interface more functional by adding a few buttons to this window Dec. For example, a button on the window that serves to close the program and a button that deletes the text that the user writes:
import tkinter as tk
pencere=tk.Tk()
def sil():
giris.delete(0,tk.END)
giris = tk.Entry()
giris.pack()
dugme1 = tk.Button(text = "KAPAT", command = pencere.quit)
dugme1.pack(side=tk.LEFT)
dugme2 = tk.Button(text = "SiL", command = sil)
dugme2.pack(side=tk.RIGHT)
pencere.mainloop()
There is an enty called input and 2 buttons. Button1 in side = tk.LEFT with LEFT and command = pencere.quit .when clicked with the quit command, we gave the shutdown task. Button 2 is based on the right and goes to the Delete function when clicked and:
giris.delete(0,tk.END)
with all entries deleted. Display Output:

Select Random Numbers From 1 To 100
import tkinter as tk
import random
pencere=tk.Tk()
def bas():
a = random.randint(1, 100)
giris.delete(0, tk.END)
giris.insert(0, a)
giris = tk.Entry(width=10)
giris.pack()
dugme = tk.Button(text="bas", command=bas, width=2, height=0)
dugme.pack()
pencere.mainloop()
As you can see, this app selects random numbers from 1 to 100… Actually, the process we do is very simple:
First, we called Python’s module “random”. As we have seen before, this module will help us when selecting random numbers. Then we create a function that will allow us to create these random numbers and print them on the screen. In this function, we first determine which range the random numbers will be in and throw it into a variable called “a”. So we created the infrastructure needed to print random numbers on the screen. Now, at this point, if we don’t take precautions, the numbers that will be printed on the screen will be sorted side by side. I mean, for example, let’s say
let our first Random Number be 3. After this number 3 is written on the screen, this first number must be deleted from the screen before the second random number appears on the screen. All numbers should not be placed side by side on the screen. For this, we add the following line to our code:
giris.delete(0, tk.END)
Thanks to this line, the screen is 0 after the first number is printed on the screen. we’re deleting everything from its position to its last position. In this way, we make room for the second Next Issue.If you want, you can try running the above codes without this line. You understand what the line after it does: the variable” a ” is 0. we’re putting it in position. That’s how we completed our function. Then, normally, we create our buttons and text field with the help of window tools called” Entry” and Button”. Here are some new “options” that should have caught your attention: these are the options called “width” and “height”… “width”
the width of a window tool with the help of the option; the height of that tool with the help of the “height” option.