BREAKING NEWS
🚀 Kaweesha launched the new AI Tech Blog! 💻 Python & Java Tutorials are now live. 🎮 Game Development Guide: Building 2D Games with Godot. 🌐 Web Hosting secrets revealed - Check the latest post!

Python Sinhala - Part 10: අපේම සොෆ්ට්වෙයාර් එකක් ඩිසයින් කරමු! (GUI & Tkinter) 🖥️🎨

Python GUI Programming Tkinter Tutorial Sinhala Part 10 - Building Desktop Apps

මෙච්චර කල් අපි හැදුවේ "කළු පාට තිරයේ" (Terminal) දුවන සොෆ්ට්වෙයාර් නේ. හැබැයි අපි පාවිච්චි කරන Chrome, Photoshop, Word වගේ හැම සොෆ්ට්වෙයාර් එකකම ලස්සන බොත්තම්, මෙනු සහ වින්ඩෝස් තියෙනවා.

මේවට කියන්නේ GUI (Graphical User Interface) කියලා. අද අපි බලමු කොහොමද Python වලින් ලස්සන Desktop Application එකක් හදන්නේ කියලා. මේකට අපි පාවිච්චි කරන්නේ Python එක්කම එන Tkinter කියන Library එක.

1. පළමු Window එක හදමු 🖼️

Tkinter කියන්නේ හරියට චිත්‍ර අඳින කැන්වස් එකක් වගේ. මුලින්ම අපි හිස් Window එකක් හදාගන්න ඕනේ.

import tkinter as tk

# 1. ප්‍රධාන වින්ඩෝ එක හදනවා
window = tk.Tk()
window.title("My First Python App 🚀")
window.geometry("400x300") # දිග x පළල

# 2. මේක දාන්නම ඕන! නැත්නම් වින්ඩෝ එක ඇවිත් යනවා.
window.mainloop()

මේ කෝඩ් එක රන් කළාම ඔයාට පොඩි වින්ඩෝ එකක් ඕපන් වෙයි. සුභ පැතුම්! ඔයා දැන් GUI Developer කෙනෙක්. 🎉

2. Widgets - සොෆ්ට්වෙයාර් එකේ කෑලි බෑලි 🧩

වින්ඩෝ එකක් තිබ්බට මදි, ඒක ඇතුලේ බට්න්, අකුරු තියෙන්න ඕනනේ. ඒවට කියන්නේ Widgets කියලා.

වැදගත්ම Widgets 3:
  • Label: නිකන් අකුරු පෙන්නන්න (Display Text).
  • Button: ඔබන්න පුළුවන් බොත්තම්.
  • Entry: User ට Type කරන්න පුළුවන් කොටුවක් (Input Box).
# ලේබල් එකක්
label = tk.Label(window, text="Hello Python!", font=("Arial", 20))
label.pack() # pack() කියන්නේ වින්ඩෝ එකට අලවන්න කියන එක

# බොත්තමක්
btn = tk.Button(window, text="Click Me", bg="yellow")
btn.pack(pady=10) # pady කියන්නේ උඩින් යටින් ඉඩ

3. Grid System - බඩු පිළිවෙලට තියමු 📐

pack() එකෙන් වෙන්නේ බඩු එකකට යටින් එකක් වැටෙන එක. හැබැයි අපිට කැල්කියුලේටරයක් වගේ හැඩේට හදන්න නම් Grid System (කොටු දැලක්) පාවිච්චි කරන්න වෙනවා.

Row & Column:
අපි කියන්නේ "මේ බට්න් එක 1 වෙනි පේළියේ (Row), 2 වෙනි තීරුවේ (Column) තියන්න" කියලා. Excel ශීට් එකක් වගේ හිතන්න.
# නම
lbl_name = tk.Label(window, text="Name:")
lbl_name.grid(row=0, column=0)

# ටයිප් කරන කොටුව
entry_name = tk.Entry(window)
entry_name.grid(row=0, column=1)

Mega Project: KP Calculator App 🧮

දැන් අපි ඉගෙන ගත්ත හැමදේම එකතු කරලා, ඇත්තටම වැඩ කරන කැල්කියුලේටරයක් හදමු. මේකේ ඉලක්කම් දෙකක් එකතු කරන්න පුළුවන්.

💻 The Calculator Code

import tkinter as tk
from tkinter import messagebox # පොප්-අප් මැසේජ් පෙන්නන්න

def calculate():
    try:
        # කොටු වලින් අගයන් ගැනීම
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        result = num1 + num2
        
        # උත්තරේ පෙන්නීම
        lbl_result.config(text=f"Answer: {result}")
    except ValueError:
        messagebox.showerror("Error", "කරුණාකර ඉලක්කම් පමණක් ඇතුලත් කරන්න!")

# --- GUI Setup ---
root = tk.Tk()
root.title("KP Calculator")
root.geometry("300x250")

# ඉලක්කම් 1
tk.Label(root, text="Number 1:").pack(pady=5)
entry1 = tk.Entry(root)
entry1.pack()

# ඉලක්කම් 2
tk.Label(root, text="Number 2:").pack(pady=5)
entry2 = tk.Entry(root)
entry2.pack()

# එකතු කරන බට්න් එක
btn_add = tk.Button(root, text="➕ Add Numbers", command=calculate, bg="#4CAF50", fg="white")
btn_add.pack(pady=20)

# උත්තරේ පෙන්වන තැන
lbl_result = tk.Label(root, text="Answer: 0", font=("Arial", 14, "bold"))
lbl_result.pack()

root.mainloop()

අවසාන වශයෙන්... 🏁

අද ඔයා හැදුවේ ඔයාගේ පළවෙනි Desktop Software එක. මේ දැනුම පාවිච්චි කරලා ඔයාට School Management System, Billing System වගේ ලොකු ව්‍යාපෘති වුනත් හදන්න පුළුවන්. GUI කියන්නේ මාරම ලස්සන ලෝකයක්.

ඊළඟ කොටසින්: Python වලින් Games හදන්නේ කොහොමද? (Pygame Basics) 🎮🕹️
දිගටම KP Tech Gear සමඟ රැඳී සිටින්න! 👇

Author

About

Tech enthusiast, Python/Java Developer, and Gamer. Sharing knowledge about coding and modern technology.

Discussion

Kaweesha AI ×
Hello Kaweesha! I am ready to help with Python, Java, or Hosting queries. 🤖