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

Tkinter Advanced Widgets


Let's dive into some of the advanced widgets in Tkinter: Menu, Toplevel, PanedWindow, Scale, Spinbox, and ScrolledText. We'll go step by step with examples for each.

1. Menu Widget

The Menu widget is used to create menus in your application, including menubars, dropdown menus, and context menus.

Example:


python
                        import tkinter as tk

                        def on_new():
                            print("New File")

                        def on_open():
                            print("Open File")

                        def on_exit():
                            root.quit()

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

                        # Create a menubar
                        menubar = tk.Menu(root)

                        # Create a File menu
                        file_menu = tk.Menu(menubar, tearoff=0)
                        file_menu.add_command(text="New", command=on_new)
                        file_menu.add_command(text="Open", command=on_open)
                        file_menu.add_separator()
                        file_menu.add_command(text="Exit", command=on_exit)
                        menubar.add_cascade(label="File", menu=file_menu)

                        # Add the menubar to the root window
                        root.config(menu=menubar)

                        # Run the application
                        root.mainloop()

                    


2. Toplevel Widget

The Toplevel widget is used to create additional windows.

Example:


python
                        import tkinter as tk

                        def open_new_window():
                            new_window = tk.Toplevel(root)
                            new_window.title("New Window")
                            new_window.geometry("200x100")
                            label = tk.Label(new_window, text="This is a new window")
                            label.pack(pady=10)

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

                        # Create a Button widget
                        button = tk.Button(root, text="Open New Window", command=open_new_window)
                        button.pack(pady=20)

                        # Run the application
                        root.mainloop()

                    


3. PanedWindow Widget

The PanedWindow widget is used to create a resizable split window.

Example:


python
                        import tkinter as tk

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

                        # Create a PanedWindow widget
                        paned_window = tk.PanedWindow(root, orient=tk.HORIZONTAL)
                        paned_window.pack(fill=tk.BOTH, expand=True)

                        # Create child widgets
                        left_label = tk.Label(paned_window, text="Left Pane", bg="lightblue")
                        right_label = tk.Label(paned_window, text="Right Pane", bg="lightgreen")

                        # Add child widgets to the PanedWindow
                        paned_window.add(left_label)
                        paned_window.add(right_label)

                        # Run the application
                        root.mainloop()

                    


4. Scale Widget

The Scale widget is used to create a slider for selecting a numerical value.

Example:


python
                        import tkinter as tk

                        def on_scale_change(value):
                            label.config(text=f"Value: {value}")

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

                        # Create a Scale widget
                        scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL, command=on_scale_change)
                        scale.pack(pady=20)

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

                        # Run the application
                        root.mainloop()

                    


5. Spinbox Widget

The Spinbox widget is used to select from a fixed set of values.

Example:


python
                        import tkinter as tk

                        def on_spinbox_change():
                            label.config(text=f"Value: {spinbox.get()}")

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

                        # Create a Spinbox widget
                        spinbox = tk.Spinbox(root, from_=0, to=10, command=on_spinbox_change)
                        spinbox.pack(pady=20)

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

                        # Run the application
                        root.mainloop()

                    


6. ScrolledText Widget

The ScrolledText widget is used to create a text widget with a scrollbar. (Requires the tkinter.scrolledtext module)

Example:


python
                        import tkinter as tk

                        from tkinter import scrolledtext

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

                        # Create a ScrolledText widget
                        scrolled_text = scrolledtext.ScrolledText(root, width=30, height=10)
                        scrolled_text.pack(pady=20)

                        # Insert some text
                        scrolled_text.insert(tk.END, "This is a ScrolledText widget.\nYou can add multiple lines of text here.")

                        # Run the application
                        root.mainloop()

                    


Summary

These advanced widgets offer more functionality and control over your Tkinter applications:

  • Menu: For creating menubars and context menus.
  • Toplevel: For creating additional windows.
  • PanedWindow: For creating resizable split windows.
  • Scale: For creating sliders to select numerical values.
  • Spinbox: For selecting from a set of values.
  • ScrolledText: For text input with scrollbars.






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