Tkinter Canvas are generally used for creating shapes such as arc, rectangle, triangle, freeform shapes, etc. All these shapes can be drawn using the inbuilt function available in tkinter library.ExampleIn this example, we will create a Circle using the create_oval(x0, y0, x1, y1) method by passing the following values of coordinates (x0, y0, x1, y1)#Import the library from tkinter import * #Create an instance of tkinter frame win= Tk() #Define the geometry of window win.geometry("600x400") #Create a canvas object c= Canvas(win, width=400, height=400) c.pack() #Draw an Oval in the canvas c.create_oval(60, 60, 210, 210) win.mainloop()OutputRunning ... Read More
Whenever we run a tkinter application, it displays a GUI-based window that would have widgets, frames, and other elements. Let us suppose we want to close our application with a function. The destroy() method in Python tkinter is used to terminate the normal execution of the application after the mainloop function.ExampleIn this example, will create a button object which triggers to close the application.#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("650x250") #Define a function def close_app(): win.destroy() #Create a text Label Label(win, text= ... Read More
Tkinter windows can be resized automatically by hovering and pulling over the window. We can disable the resizable property using the resizable(boolean value) method. We will pass false value to this method which will disable the window to be resized.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("650x250") Label(win, text= "Hello World", font=('Times New Roman bold', 20)).pack(pady=20) #Make the window resizable false win.resizable(False, False) win.mainloop()OutputRunning the above code will display the following the tkinter window, but you won't be able to resize it.Read More
Pressing a key and handling some operation with the key is an event that can be triggered through a button. We can bind the key event using the Binding method in a tkinter application.Whenever the key will be triggered, it will call a handler that will raise the specific operation for the key event.If we want to trigger the Enter key with the bind function, we will use the bind('', Handler) method. For Enter Key, we use bind('', Handler) function.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the ... Read More
In order to change the background color and foreground color of a tkinter frame, we can assign different values to the bg and fg parameters in the Frame function.ExampleIn this example, we have created two frames with different background colors.#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("650x250") #Create an frame frame1= Frame(win, bg= "red") frame2= Frame(win, bg="black") #Create an label inside the frame Label(frame2, text= "Line:1", font=('Lucida font', 20)).pack(pady=20) Label(frame1, text= "Line:2", font=('Lucida font', 20)).pack(pady=20) frame1.pack() frame2.pack() win.mainloop()OutputRunning the above ... Read More
Tooltips are useful in applications where we need to display some information while hovering on a button.In order to create and display a tooltip, we can use the Balloon property of tkinter.Example#Import the tkinter library from tkinter import * from tkinter.tix import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x450") #Create a tooltip tip = Balloon(win) #Create a Button widget my_button=Button(win, text= "Hover Me") my_button.pack(pady=20) #Bind the tooltip with button tip.bind_widget(my_button, balloonmsg="www.tutorialspoint.com") win.mainloop()OutputThe above code will display the following window with a button "Hover Me". When the user hovers ... Read More
Tkinter provides a custom handler to close the window. It acts as a callback function that the user can run in order to close the window.To close the window using the handler, we can use the destroy() method. It closes the window abruptly after calling it in any function or any widget. Let us invoke the close event handler by defining a method.By using as an argument in WidgetExample#Importing the required library from tkinter import * #Create an instance of tkinter frame or window win= Tk() #Set the geometry win.geometry("600x400") #Create a button and pass arguments in ... Read More
Python provides the Pillow (PIL) package to support, process, and display the images in tkinter applications. A Tkinter application generally supports image files such as, ppm, png, and gif.Let us suppose we want to embed and display a JPEG or JPG image in our application.Tkinter Label widgets are generally used to display the text or images on the window and thus by passing the img value, we can display the JPEG image in the window.Example#Import required libraries from tkinter import * from PIL import ImageTk, Image #Create an instance of tkinter window win =Tk() #Define the geometry of ... Read More
A Tkinter window can be initialized after running the application. Generally, the width and the height of the window is resizable which can be minimized.In order to set the window size to its minimum value, assign the value of width and height in minsize(height, width) method. The method can be invoked with the window or frame object.Example#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Minimize the window win.minsize(150, 100) #Create a text label Label(win, text= "Window Size is minimized to 150x100", font=('Helvetica bold', 20)).pack(pady=20) win.mainloop()OutputRunning the above code ... Read More
Tkinter Text widget is used to accept multiline user Input. It is similar to Entry Widget but the only difference is that Text widget supports multiple line texts. In order to create a Text widget, we have to instantiate a text object.Adding multiple texts will require to add the ScrollBar. In order to add a scrollbar in the text widget, we can call the ScrolledText(root) function. This function generally creates a text field with a scrollbar.The ScrolledText(root) function resides in Tkinter ScrolledText Module. We can import it using the following command, from tkinter.scrolledtext import ScrolledTextExampleIn this example, we will create ... Read More