কম্পিউটার টিউটোরিয়াল

পাইথনে tkinter দিয়ে সিম্পল GUI ক্যালকুলেটর বানানো

ভূমিকা

পাইথনে GUI কম্পোনেন্ট বানাতে tkinter লাইব্রেরি ব্যবহার করা হয়। এই আর্টিকেলে সিম্পল GUI ক্যালকুলেটর অ্যাপ্লিকেশন বানানো শিখব।

শুরু করা

পাইথন ইমেজিং লাইব্রেরি (Pillow) ইনস্টল করুন আইকন লোড করতে:

pip install Pillow

ক্যালকুলেটর আইকন ডাউনলোড করে "asset" ফোল্ডারে রাখুন। (লিংক: dropbox) আপনার কাঙ্খিত আইকন গুগল ইমেজ থেকেও নেওয়া যায়।

প্রয়োজনীয় মডিউল ইম্পোর্ট:

from tkinter import *
from PIL import Image
from PIL import ImageTk

ফাংশন তৈরি

তিনটি মূল ফাংশন: নম্বর/সিম্বল প্রেস, ইকুয়াল প্রেস, ক্লিয়ার প্রেস।

গ্লোবাল ভ্যারিয়েবল ইনিশিয়ালাইজ:

txt = ""
res = False
ans = 0

নম্বর/সিম্বল প্রেস ফাংশন

def press(num):
    global txt, ans, res
    if res == True:
        txt = ans
        res = False
    txt = txt + str(num)
    equation.set(txt)

ইকুয়াল প্রেস ফাংশন

def equal():
    try:
        global txt, ans, res
        ans = str(eval(txt))
        equation.set(ans)
        res = True
    except:
        equation.set("ERROR : Invalid Equation")
        txt = ""

ক্লিয়ার প্রেস ফাংশন

def clear():
    global txt, ans, res
    txt = ""
    equation.set("")
    res = False

মেইন ফাংশন ও GUI সেটআপ

if __name__ == "__main__":
    window = Tk()
    window.configure(background="black")
    window.title("Calculator")
    window.iconbitmap("assets\\Calculator\\Logo.ico")
    window.geometry("343x417")
    window.resizable(0,0)

নোট: ফাইল স্ট্রাকচার সঠিক রাখুন (assets/Calculator/Logo.ico)।

টেক্সট ফিল্ড ডিজাইন

equation = StringVar()
txt_field = Entry(relief=RIDGE, textvariable=equation, bd=10, font=("Aerial", 20), bg="powder blue")
txt_field.grid(columnspan=4, ipady=10, ipadx=10, sticky="nsew")

বাটন যোগ (উদাহরণ: ১ নম্বর বাটন)

width = 80
height = 80
img1 = Image.open("assets/Calculator/one.PNG")
img1 = img1.resize((width, height))
oneImage = ImageTk.PhotoImage(img1)
button1 = Button(window, image=oneImage, bg="white", command=lambda: press(1), height=height, width=width)
button1.grid(row=2, column=0, sticky="nsew")

বাকি বাটনগুলো (২-৯, ০, গুন, ভাগ, যোগ, বিয়োগ, সমান, ক্লিয়ার) একই প্যাটার্নে যোগ করুন। সম্পূর্ণ কোড নিচে দেওয়া আছে।

সম্পূর্ণ কোড

from tkinter import *
from PIL import Image
from PIL import ImageTk
txt = ""
res = False
ans = 0
def press(num):
    global txt, ans, res
    if res == True:
        txt = ans
        res = False
    txt = txt + str(num)
    equation.set(txt)
def equal():
    try:
        global txt, ans, res
        ans = str(eval(txt))
        equation.set(ans)
        res = True
    except:
        equation.set("ERROR : Invalid Equation")
        txt = ""
def clear():
    global txt, ans, res
    txt = ""
    equation.set("")
    res = False
if __name__ == "__main__":
    window = Tk()
    window.configure(background="black")
    window.title("Calculator")
    window.iconbitmap("assets\\Calculator\\Logo.ico")
    window.geometry("343x417")
    window.resizable(0,0)
    equation = StringVar()
    txt_field = Entry(relief=RIDGE, textvariable=equation, bd=10, font=("Aerial", 20), bg="powder blue")
    txt_field.grid(columnspan=4, ipady=10, ipadx=10, sticky="nsew")
    width = 80
    height = 80
    # সব বাটন যোগ করুন (এখানে সংক্ষিপ্ত)
    window.mainloop()

ফরম্যাটিং সমস্যা হলে GitHub-এ পান: https://github.com/SVijayB/PyHub/blob/master/Graphics/Simple%20Calculator.py

আউটপুট

পাইথনে tkinter দিয়ে সিম্পল GUI ক্যালকুলেটর বানানো

  1. পাইথনে Tkinter ব্যবহার করে সিম্পল GUI ক্যালকুলেটর তৈরি করা

  2. পাইথনে Tkinter প্রোগ্রামিং

  3. পাইথন Tkinter ব্যবহার করে সিম্পল রেজিস্ট্রেশন ফর্ম

  4. পাইথন Tkinter-এ কালার গেম তৈরি