Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Basic Tkinter Widgets


1. Label Widget

The Label widget is used to display text or images.

Example:


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Label Example")
                        root.geometry("300x200")

                        # Create a Label widget
                        label = tk.Label(root, text="Hello, Tkinter!")
                        label.pack(pady=20)  # Add some padding for better layout

                        # Run the application
                        root.mainloop()

                    

2. Button Widget

The Button widget is used to perform an action when clicked.

Example:



python
                        import tkinter as tk

                        def on_button_click():
                            label.config(text="Button Clicked!")
                                            
                        # Create the main window
                        root = tk.Tk()
                        root.title("Button Example")
                        root.geometry("300x200")
                                            
                        # Create a Label widget
                        label = tk.Label(root, text="Press the Button")
                        label.pack(pady=10)
                                            
                        # Create a Button widget
                        button = tk.Button(root, text="Click Me", command=on_button_click)
                        button.pack(pady=10)
                                            
                        # Run the application
                        root.mainloop()

                    


3. Entry Widget

The Entry widget is used to accept single-line text input from the user.

Example:


python
                        import tkinter as tk

                        def on_button_click():
                            label.config(text="Hello, " + entry.get())

                        # Create the main window
                        root = tk.Tk()
                        root.title("Entry Example")
                        root.geometry("300x200")

                        # Create a Label widget
                        label = tk.Label(root, text="Enter your name:")
                        label.pack(pady=10)

                        # Create an Entry widget
                        entry = tk.Entry(root)
                        entry.pack(pady=10)

                        # Create a Button widget
                        button = tk.Button(root, text="Submit", command=on_button_click)
                        button.pack(pady=10)

                        # Run the application
                        root.mainloop()

                    


4. Text Widget

The Text widget is used for multi-line text input.

Example:


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Text Example")
                        root.geometry("300x200")

                        # Create a Text widget
                        text = tk.Text(root, height=5, width=30)
                        text.pack(pady=10)

                        # Run the application
                        root.mainloop()

                    


5. Checkbutton Widget

The Checkbutton widget is used to select or deselect an option.

Example:


python
                        import tkinter as tk

                        def on_checkbutton_toggle():
                            label.config(text="Checked" if var.get() else "Unchecked")

                        # Create the main window
                        root = tk.Tk()
                        root.title("Checkbutton Example")
                        root.geometry("300x200")

                        # Create an IntVar for the Checkbutton
                        var = tk.IntVar()

                        # Create a Checkbutton widget
                        checkbutton = tk.Checkbutton(root, text="Check Me", variable=var, command=on_checkbutton_toggle)
                        checkbutton.pack(pady=10)

                        # Create a Label widget
                        label = tk.Label(root, text="Unchecked")
                        label.pack(pady=10)

                        # Run the application
                        root.mainloop()

                    


6. Radiobutton Widget

The Radiobutton widget is used to select one option from a set of options.

Example:


python
                        import tkinter as tk

                        def on_radiobutton_select():
                            label.config(text="Selected: " + str(var.get()))

                        # Create the main window
                        root = tk.Tk()
                        root.title("Radiobutton Example")
                        root.geometry("300x200")

                        # Create an IntVar for the Radiobuttons
                        var = tk.IntVar()

                        # Create Radiobutton widgets
                        radiobutton1 = tk.Radiobutton(root, text="Option 1", variable=var, value=1, command=on_radiobutton_select)
                        radiobutton1.pack(pady=5)

                        radiobutton2 = tk.Radiobutton(root, text="Option 2", variable=var, value=2, command=on_radiobutton_select)
                        radiobutton2.pack(pady=5)

                        # Create a Label widget
                        label = tk.Label(root, text="Selected: None")
                        label.pack(pady=10)

                        # Run the application
                        root.mainloop()

                    


7. Listbox Widget

The Listbox widget is used to display a list of options.

Example:


                        import tkinter as tk

                        def on_listbox_select(event):
                            selection = event.widget.curselection()
                            if selection:
                                index = selection[0]
                                data = event.widget.get(index)
                                label.config(text="Selected: " + data)

                        # Create the main window
                        root = tk.Tk()
                        root.title("Listbox Example")
                        root.geometry("300x200")

                        # Create a Listbox widget
                        listbox = tk.Listbox(root)
                        listbox.pack(pady=10)
                        listbox.insert(1, "Option 1")
                        listbox.insert(2, "Option 2")
                        listbox.insert(3, "Option 3")

                        # Bind the select event
                        listbox.bind("<>", on_listbox_select)
                        
                        # Create a Label widget
                        label = tk.Label(root, text="Selected: None")
                        label.pack(pady=10)
                        
                        # Run the application
                        root.mainloop()
                        
                    


8. Canvas Widget

The Canvas widget is used for drawing shapes, text, and images.

Example:


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Canvas Example")
                        root.geometry("300x200")

                        # Create a Canvas widget
                        canvas = tk.Canvas(root, width=200, height=100)
                        canvas.pack(pady=10)

                        # Draw shapes on the Canvas
                        canvas.create_line(0, 0, 200, 100, fill="blue")
                        canvas.create_rectangle(50, 25, 150, 75, fill="red")

                        # Run the application
                        root.mainloop()

                    


9. Frame Widget

The Frame widget is used as a container to hold other widgets.

Example:


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Frame Example")
                        root.geometry("300x200")

                        # Create a Frame widget
                        frame = tk.Frame(root, borderwidth=2, relief="sunken")
                        frame.pack(pady=10, padx=10, fill="both", expand=True)

                        # Create widgets inside the Frame
                        label = tk.Label(frame, text="This is inside a frame")
                        label.pack(pady=10)

                        button = tk.Button(frame, text="Click Me")
                        button.pack(pady=10)

                        # Run the application
                        root.mainloop()

                    

This step-by-step guide covers the basic widgets in Tkinter. Each example demonstrates how to create and use a specific widget, and you can expand these examples to build more complex GUI applications.








Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java