What is tkinter?
- Get link
- X
- Other Apps
1. What is tkinter?
tkinter is a Python library that helps you create graphical user interfaces (GUIs). You can make windows, buttons, text boxes, and more.
2. Starting a Basic Window
To create a basic window:
import tkinter as tk
# Create the main window
root = tk.Tk()
# Set a title for the window
root.title("My First Window")
# Display the window
root.mainloop()
When you run this code, a blank window will appear.
3. Adding Things to the Window
You can add "widgets" like labels, buttons, or text boxes.
Add a Label (Text)
label = tk.Label(root, text="Hello, Tkinter!")label.pack() # This places the label in the window
Add a Button
def say_hello():
print("Hello!")
button = tk.Button(root, text="Click Me", command=say_hello)
button.pack()
Add a Text Box
entry = tk.Entry(root) # Create a text box
entry.pack()
def show_text():
print("You typed:", entry.get())
button = tk.Button(root, text="Show Text", command=show_text)
button.pack()
4. Organizing Widgets
Widgets can be arranged using three main methods:
Pack (Simple Stacking)
label.pack() button.pack() entry.pack()
Grid (Like a Table)
label.grid(row=0, column=0)
entry.grid(row=0, column=1)
button.grid(row=1, column=0, columnspan=2)
5. Making the Window Look Better
You can adjust the size of the window:
root.geometry("300x200") # Width x Height
6. Example: A Simple App
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Simple App")
root.geometry("300x200")
# Add a label
label = tk.Label(root, text="Type something:")
label.pack()
# Add a text box
entry = tk.Entry(root)
entry.pack()
# Add a button
def display_text():
text = entry.get()
print("You typed:", text)
button = tk.Button(root, text="Show Text", command=display_text)
button.pack()
# Run the app
root.mainloop()
Explanation
1. Importing the tkinter Library
import tkinter as tk
tkinter is the library used to create graphical user interfaces (GUIs).- We import it as
tk to make the code cleaner and easier to write.
2. Creating the Main Window
root = tk.Tk()
Tk() creates the main application window, often called the root window.root is a variable that stores this window object.
3. Setting Window Properties
root.title("Simple App")
- Sets the title of the window to "Simple App". This text appears in the title bar of the window.
root.geometry("300x200")
- Sets the size of the window to 300 pixels wide and 200 pixels tall.
4. Adding a Label
label = tk.Label(root, text="Type something:")
label.pack()
tk.Label: Creates a label widget, which is used to display static text or images.root: Specifies the parent window where the label will appear.text: The text displayed in the label.
label.pack(): Places the label in the window using the pack layout manager, which automatically positions widgets vertically or horizontally.
5. Adding a Text Box (Entry Widget)
entry = tk.Entry(root)
entry.pack()
tk.Entry: Creates a single-line text input field.root: Specifies the parent window.
entry.pack(): Adds the text box to the window.
6. Adding a Button
def display_text():
text = entry.get() # Retrieves the text from the text box
print("You typed:", text) # Prints the text to the console
- Function
display_text:entry.get(): Gets the current text in the text box.print("You typed:", text): Displays the retrieved text in the console.
button = tk.Button(root, text="Show Text", command=display_text)
button.pack()
tk.Button: Creates a button widget.root: Specifies the parent window.text: The text displayed on the button.command: The function to call when the button is clicked (in this case, display_text).
button.pack(): Places the button in the window.
7. Running the Application
root.mainloop()
root.mainloop(): Starts the main event loop, which keeps the window open and responsive.- This loop waits for user interactions like button clicks or text entry and responds accordingly.
How It Works Together
- The program creates a window with the title "Simple App" and size 300x200.
- A label prompts the user to "Type something."
- A text box allows the user to enter some text.
- A button labeled "Show Text" appears.
- When the button is clicked, the
display_text function runs:- It retrieves the text entered in the text box and prints it to the console.
Output Example
- User enters "Hello, Tkinter!" in the text box.
- User clicks the "Show Text" button.
- The console displays:
You typed: Hello, Tkinter!
- Get link
- X
- Other Apps
Comments
Post a Comment