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 Get Started


Download Python


Click Here

Download Python


Setting Up Tkinter

Tkinter is included with the standard Python distribution. To verify Tkinter is installed, you can try importing it in a Python shell:


python
import tkinter as tk

If this command runs without error, Tkinter is installed and ready to use.

Your First Tkinter Program

Let's create a simple window using Tkinter.


python
                        import tkinter as tk

                        # Create the main window (root window)
                        root = tk.Tk()

                        # Set the window title
                        root.title("Hello Tkinter")

                        # Set the window size
                        root.geometry("400x300")

                        # Run the application
                        root.mainloop()

                    

Explanation:
  • tk.Tk(): Initializes the main window.
  • root.title("Hello Tkinter"): Sets the title of the window.
  • root.geometry("400x300"): Sets the size of the window (width x height).
  • root.mainloop(): Starts the event loop, waiting for user interactions.

Basic Widgets

  • Label: Displays text or images.
  • Button: A clickable button.
  • Entry: A single-line text box.
  • Text: A multi-line text box.
  • Frame: A container for organizing other widgets.

Example with a Label and a Button:


python
                        import tkinter as tk

                        def on_button_click():
                            label.config(text="Button Clicked!")

                        # Create the main window
                        root = tk.Tk()
                        root.title("Simple Tkinter App")
                        root.geometry("300x200")

                        # Create a Label
                        label = tk.Label(root, text="Hello, Tkinter!")
                        label.pack(pady=10)

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

                        # Run the application
                        root.mainloop()

                    

Explanation:
  • tk.Label: Creates a label widget.
  • label.pack(): Packs the label into the window, managing its placement.
  • tk.Button: Creates a button widget.
  • button.pack(): Packs the button into the window.
  • command=on_button_click: Binds the button click event to the on_button_click function.

Conclusion

Tkinter is a powerful and easy-to-use toolkit for creating GUI applications in Python. With just a few lines of code, you can create windows, handle user inputs, and manage the application’s interface. This introduction covers the basics to get you started with Tkinter. As you progress, you will learn more about the wide range of widgets and capabilities Tkinter offers.







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