How to Use Entry Box on Canvas – Tkinter
There are various ways of creating GUI applications in Python. One of the easiest and most commonly used ways is through the use of the Tkinter module. Tkinter offers various widgets for creating web apps. One such widget is the Entry box, which is used to display a single line of text. Are you facing the issue of creating an entry box on the canvas in Tkinter? Here is the solution for you. This article will discuss how we can use the entry box on Canvas using Tkinter.
Required Modules
Tkinter: The standard GUI library for Python which provides an easy to create GUI applications is known as Tkinter. You can install the Tkinter module by running the following command in the command prompt.
Stepwise Implementation
Step 1: First of all, import the library Tkinter.
from tkinter import *
Step 2: Now, create a GUI app using Tkinter. Tk() is used for creating an instance of the Tkinter frame.
app = Tk()
Step 3: Then, create and display the canvas in the GUI app.
canvas_widget = Canvas(app, width=#Distance from x-axis, height=#Distance from y-axis)
canvas_widget.pack()
Step 4: Moreover, create an input name on canvas for input using widget Entry.
entry_widget1 = Entry(app)
canvas_widget.create_window(#Distance from x-axis, #Distance from y-axis, window=entry_widget1)
Step 5: Finally, make the loop for displaying the GUI app on the screen
app.mainloop()
Example 1:
In this example, we have created a form to collect data of names and email IDs in the entry boxes and display to users the message of data submitted successfully once they have filled in the data. Also, the entry boxes will get blank after submission.
Python
from tkinter import *
from tkinter import messagebox
app = Tk()
def submit_data():
messagebox.showinfo( 'Submitted' ,
"Your data is submitted successfully!" )
entry_widget1.delete( 0 , END)
entry_widget2.delete( 0 , END)
canvas_widget = Canvas(app, width = 500 , height = 500 )
canvas_widget.pack()
label_widget1 = Label(app, text = "Enter your name" )
canvas_widget.create_window( 150 , 160 , window = label_widget1)
label_widget2 = Label(app, text = "Enter your mail I'd" )
canvas_widget.create_window( 150 , 200 , window = label_widget2)
entry_widget1 = Entry(app)
canvas_widget.create_window( 300 , 160 , window = entry_widget1)
entry_widget2 = Entry(app)
canvas_widget.create_window( 300 , 200 , window = entry_widget2)
button_widget = Button(text = 'Submit' , command = submit_data)
canvas_widget.create_window( 225 , 250 , window = button_widget)
app.mainloop()
|
Output:
Example 2:
In this example, we take two strings as input from the user. Then, we let the user concatenate or compare those two strings on the basis of the button he pressed present on the app and display the result in the message box.
Python
from tkinter import *
from tkinter import messagebox
app = Tk()
def compare_string():
string1 = entry_widget1.get()
string2 = entry_widget2.get()
if string1 = = string2:
a = "Strings are same"
else :
a = "Strings are different"
messagebox.showinfo( "Compared Strings" , a)
def concat_string():
string1 = entry_widget1.get()
string2 = entry_widget2.get()
messagebox.showinfo( "Compared Strings" ,
'Concatenated String: ' + string1 + string2)
canvas_widget = Canvas(app, width = 500 ,
height = 500 )
canvas_widget.pack()
label_widget1 = Label(app, text = "Enter the string 1" )
canvas_widget.create_window( 150 , 160 ,
window = label_widget1)
label_widget2 = Label(app, text = "Enter the string 2" )
canvas_widget.create_window( 350 , 160 ,
window = label_widget2)
entry_widget1 = Entry(app)
canvas_widget.create_window( 150 , 200 ,
window = entry_widget1)
entry_widget2 = Entry(app)
canvas_widget.create_window( 350 , 200 ,
window = entry_widget2)
button_widget = Button(text = 'Concatenate the strings' ,
command = concat_string)
canvas_widget.create_window( 150 , 250 , window = button_widget)
button_widget = Button(text = 'Compare the strings' ,
command = compare_string)
canvas_widget.create_window( 350 , 250 , window = button_widget)
app.mainloop()
|
Output:
Last Updated :
09 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...