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
What does calling Tk() actually do?
Tkinter is Python's built-in GUI library that provides functions and methods to create desktop applications. When you call Tk(), you create the root window − the main container that holds all other GUI components. This root window serves as the foundation of your tkinter application and manages the event loop that handles user interactions.
What Happens When You Call Tk()
When you execute Tk(), several important things occur behind the scenes ?
- Creates the main application window (root window)
- Initializes the Tk interpreter that communicates with the operating system
- Sets up the event handling system
- Establishes the widget hierarchy with this window as the parent
Creating a Basic Tkinter Application
Here's how to create a simple tkinter application using Tk() ?
# Import tkinter library
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("750x250")
# Create a Label widget
label = Label(win, text="Open Source Learning is Awesome!", font=('Courier', 20, 'underline'))
label.pack()
# Start the event loop
win.mainloop()
Key Components Explained
Root Window Properties
The Tk() instance provides access to window properties and methods ?
from tkinter import *
root = Tk()
root.title("My Application")
root.geometry("400x300")
root.resizable(False, False)
print("Window title:", root.title())
print("Window geometry:", root.geometry())
root.mainloop()
The Event Loop
The mainloop() method starts the event loop that keeps your application running ?
from tkinter import *
root = Tk()
root.title("Event Loop Demo")
button = Button(root, text="Click Me!", command=lambda: print("Button clicked!"))
button.pack(pady=20)
# This keeps the window open and responsive
root.mainloop()
Common Tk() Methods
| Method | Purpose | Example |
|---|---|---|
geometry() |
Set window size and position | root.geometry("800x600") |
title() |
Set window title | root.title("My App") |
resizable() |
Control window resizing | root.resizable(True, False) |
mainloop() |
Start the event loop | root.mainloop() |
Multiple Windows
You can create additional windows using Toplevel(), but there should only be one Tk() instance per application ?
from tkinter import *
# Main root window
root = Tk()
root.title("Main Window")
# Create a second window
def open_second_window():
second_window = Toplevel(root)
second_window.title("Second Window")
second_window.geometry("300x200")
Label(second_window, text="This is a second window").pack(pady=50)
Button(root, text="Open Second Window", command=open_second_window).pack(pady=50)
root.mainloop()
Conclusion
Calling Tk() creates the root window that serves as the foundation of your GUI application. It initializes the Tk interpreter, sets up event handling, and provides the main container for all widgets. Always call mainloop() to start the event loop and keep your application responsive.
