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
# Python program to use entry # box on canvas- Tkinter # Import the libraries tkinter and messagebox from tkinter import * from tkinter import messagebox # Create a GUI app app = Tk() # Create a function to submit the data def submit_data(): # Show submitted message to user when button is clicked messagebox.showinfo( 'Submitted' , "Your data is submitted successfully!" ) # Clear the entry widgets after data is submitted entry_widget1.delete( 0 , END) entry_widget2.delete( 0 , END) # Create and display a canvas on the GUI app canvas_widget = Canvas(app, width = 500 , height = 500 ) canvas_widget.pack() # Create and place the label in canvas # for user to enter his name label_widget1 = Label(app, text = "Enter your name" ) canvas_widget.create_window( 150 , 160 , window = label_widget1) # Create and place the label in canvas # for user to enter his mail I'd label_widget2 = Label(app, text = "Enter your mail I'd" ) canvas_widget.create_window( 150 , 200 , window = label_widget2) # Create an input name on canvas # for inputting user name using widget Entry entry_widget1 = Entry(app) canvas_widget.create_window( 300 , 160 , window = entry_widget1) # Creating another input name on canvas # for inputting user mail using widget Entry entry_widget2 = Entry(app) canvas_widget.create_window( 300 , 200 , window = entry_widget2) # Creating and placing the button on canvas to submit data button_widget = Button(text = 'Submit' , command = submit_data) canvas_widget.create_window( 225 , 250 , window = button_widget) # Make the infinite loop for displaying app 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
# Python program to use entry # box on canvas- Tkinter # Import the library tkinter from tkinter import * from tkinter import messagebox # Creating an app app = Tk() # Create a function to compare the strings def compare_string(): # Taking the value in entry widget from user # and storing it in variables string1 = entry_widget1.get() string2 = entry_widget2.get() # Check if two strings are equal or not if string1 = = string2: a = "Strings are same" else : a = "Strings are different" # Show compared result to user when button is clicked messagebox.showinfo( "Compared Strings" , a) # Create a function to concat the strings def concat_string(): # Taking the value in entry widget from user # and storing it in variables string1 = entry_widget1.get() string2 = entry_widget2.get() # Show concatenated result to user when button is clicked messagebox.showinfo( "Compared Strings" , 'Concatenated String: ' + string1 + string2) # Creating and displaying a canvas canvas_widget = Canvas(app, width = 500 , height = 500 ) canvas_widget.pack() # Creating and placing the label in canvas label_widget1 = Label(app, text = "Enter the string 1" ) canvas_widget.create_window( 150 , 160 , window = label_widget1) # Creating and placing the label in canvas label_widget2 = Label(app, text = "Enter the string 2" ) canvas_widget.create_window( 350 , 160 , window = label_widget2) # Creating an input name on canvas # for input using widget Entry entry_widget1 = Entry(app) canvas_widget.create_window( 150 , 200 , window = entry_widget1) # Creating another input name on canvas # for input using widget Entry entry_widget2 = Entry(app) canvas_widget.create_window( 350 , 200 , window = entry_widget2) # Creating and placing the button on canvas to concat strings button_widget = Button(text = 'Concatenate the strings' , command = concat_string) canvas_widget.create_window( 150 , 250 , window = button_widget) # Creating and placing the button on canvas to compare strings button_widget = Button(text = 'Compare the strings' , command = compare_string) canvas_widget.create_window( 350 , 250 , window = button_widget) # Make the infinite loop for displaying app app.mainloop() |
Output: