Build your app using Tkinter in python!

Build your app using Tkinter in python!

What is Tkinter in Python?

Tkinter is a built-in graphical user interface (GUI) library in Python. It allows you to create windows, buttons, labels, text boxes, and other visual elements for your Python programs. With Tkinter, you can make your programs more interactive and user-friendly by providing a graphical interface that users can interact with using their mouse and keyboard.

How to Start Using Tkinter: A Simple Example Let’s say you want to create a basic program that displays a window with a button. When you click the button, a message will appear.

Step 1: Import Tkinter In your Python code, you need to start by importing the tkinter module:

import tkinter as tk

Step 2: Create a Window Next, you create the main window of your program: and size the window to 500 x 200.

root = tk.Tk()
root.geometry("500x200")

Step 3: Add Elements You can add different elements to the window, like buttons, labels, and more. For this example, let’s add a button:

button = tk.Button(root, text="Click Me", command=lambda: print("Hello, Tkinter!"))

Here, text is the text on the button, and command is what should happen when the button is clicked. In this case, we’re using a lambda function to print a message.

Step 4: Display the Elements You need to tell Tkinter to display the elements you’ve added:

button.pack()

Step 5: Run the Application Finally, you run the Tkinter event loop to start the application:

root.mainloop()

Putting it All Together Here’s the complete example:

import tkinter as tk

def show_message():
    print("Hello , Tkinter!")
root = tk.Tk()
root.geometry("500x200")
button = tk.Button(root, text="Click Me", command=show_message)
button.pack()

root.mainloop()

This is a simple code to start working in Tkinter.

The output will look like this as shown:

tkinter output window

This is just basics to start with, you can explore more options on the widgets like radio buttons, entry box..etc.,

After completing these codes, go through the next topic on: NumPy in python

 

Was this article helpful?
YesNo

1 thought on “Build your app using Tkinter in python!”

Leave a Comment