Select a Subtopic
π― Day 15: Creating a Desktop GUI with Tkinter
Today, you'll learn how to build a basic desktop GUI for your Instagram bot using Tkinter, Python's built-in library for creating graphical user interfaces. The GUI will allow users to enter Instagram credentials and start the bot with a button click.
β What Youβll Learn Today
- Setting up Tkinter.
- Creating a window with input fields (username & password).
- Adding buttons and labels.
- Using grid layout to organize the GUI.
- Basic event handling.
π 1. Setting Up Tkinter
Tkinter is built into Python, so there's no need to install anything separately. Just import it like this:
import tkinter as tk
Create a basic window to get started:
# Import the Tkinter module
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Instagram Bot")
# Set the size of the window
root.geometry("400x300")
# Run the main event loop
root.mainloop()π¨ 2. Adding Input Fields and Labels
Letβs add fields for username and password along with labels.
# Create a label for Username username_label = tk.Label(root, text="Username:") username_label.grid(row=0, column=0, padx=10, pady=10) # Create an entry field for Username username_entry = tk.Entry(root) username_entry.grid(row=0, column=1, padx=10, pady=10) # Create a label for Password password_label = tk.Label(root, text="Password:") password_label.grid(row=1, column=0, padx=10, pady=10) # Create an entry field for Password password_entry = tk.Entry(root, show="*") password_entry.grid(row=1, column=1, padx=10, pady=10)
π±οΈ 3. Adding a Button to Start the Bot
Create a button that will trigger the Instagram bot to start.
# Create a button to start the bot start_button = tk.Button(root, text="Start Bot", command=lambda: start_bot(username_entry.get(), password_entry.get())) start_button.grid(row=2, column=0, columnspan=2, pady=20)
π‘ 4. Handling Button Click (Function)
Define the start_bot() function that will capture the username and password from the input fields and print them to the console.
# Function to start the bot
def start_bot(username, password):
print(f"Starting bot with Username: {username} and Password: {password}")π Full Code So Far:
import tkinter as tk
# Function to start the bot
def start_bot(username, password):
print(f"Starting bot with Username: {username} and Password: {password}")
# Create the main window
root = tk.Tk()
root.title("Instagram Bot")
root.geometry("400x300")
# Create labels and entry fields
username_label = tk.Label(root, text="Username:")
username_label.grid(row=0, column=0, padx=10, pady=10)
username_entry = tk.Entry(root)
username_entry.grid(row=0, column=1, padx=10, pady=10)
password_label = tk.Label(root, text="Password:")
password_label.grid(row=1, column=0, padx=10, pady=10)
password_entry = tk.Entry(root, show="*")
password_entry.grid(row=1, column=1, padx=10, pady=10)
# Create a button to start the bot
start_button = tk.Button(root, text="Start Bot", command=lambda: start_bot(username_entry.get(), password_entry.get()))
start_button.grid(row=2, column=0, columnspan=2, pady=20)
# Run the main event loop
root.mainloop()π― 5. Adding Styling (Optional)
You can style your GUI by modifying fonts, colors, and other attributes:
username_label = tk.Label(root, text="Username:", font=("Arial", 14), fg="blue")
password_label = tk.Label(root, text="Password:", font=("Arial", 14), fg="blue")You can also adjust the button styling:
start_button = tk.Button(root, text="Start Bot", bg="green", fg="white", font=("Arial", 12, "bold"))π₯ 6. Running Your First GUI
- Save the code as instagram_bot_gui.py.
- Run the file in your terminal or command prompt:
python instagram_bot_gui.py
π Practical Task for Today:
- Create a Tkinter GUI with username and password fields.
- Add a Start Bot button that prints the entered credentials to the console.
- Add styling to your GUI.