Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a simple screen using Tkinter?
Tkinter is Python's standard GUI library for creating desktop applications. We will create a simple screen using the Tkinter library to demonstrate the basic setup.
Algorithm
Step 1: Import tkinter. Step 2: Create an object of the tkinter class. Step 3: Display the screen using mainloop().
Example Code
Here's how to create a basic Tkinter window ?
import tkinter as tk
# Create the main window
window = tk.Tk()
# Set window title
window.title("Simple Tkinter Screen")
# Set window size
window.geometry("400x300")
# Start the event loop
window.mainloop()
The output of the above code is ?
A window with title "Simple Tkinter Screen" and size 400x300 pixels will appear on screen.
Key Components
Let's understand each part of the code ?
- tk.Tk() − Creates the main window object
- title() − Sets the window title displayed in the title bar
- geometry() − Sets the window size in "width x height" format
- mainloop() − Starts the event loop that keeps the window running
Enhanced Example
Here's a more complete example with additional window properties ?
import tkinter as tk
# Create main window
window = tk.Tk()
# Configure window properties
window.title("My First GUI Application")
window.geometry("500x400")
window.configure(bg="lightblue")
# Make window non-resizable (optional)
window.resizable(False, False)
# Add a simple label
label = tk.Label(window, text="Welcome to Tkinter!", font=("Arial", 16))
label.pack(pady=50)
# Start the GUI event loop
window.mainloop()
This creates a blue window with a welcome message that cannot be resized.
Conclusion
Creating a simple Tkinter screen requires importing tkinter, creating a Tk() object, and calling mainloop(). Use geometry() and title() methods to customize your window appearance and always end with mainloop() to display the GUI.
Advertisements
