Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python में tkinter का उपयोग करके सिंपल GUI कैलकुलेटर कैसे बनाएं?


परिचय

Python में, हम GUI कंपोनेंट्स बनाने और बेहतर यूजर इंटरफेस तैयार करने के लिए tkinter लाइब्रेरी का उपयोग करते हैं।

इस लेख में आप सिंपल GUI आधारित कैलकुलेटर एप्लिकेशन बनाने के मेथड्स सीखेंगे।

शुरुआत करना

इसमें कूदने से पहले, कुछ चीजें हैं जिन्हें हमें पहले व्यवस्थित करने की आवश्यकता है।

आइए Python की इमेजिंग लाइब्रेरी को डाउनलोड करके शुरू करें जिसका उपयोग हम अपने लोकल सिस्टम से इमेजेज प्राप्त करने के लिए करेंगे। PIL (Pillow) इंस्टॉल करने के लिए, अपना टर्मिनल लॉन्च करें और नीचे दिए गए कमांड को टाइप करें।

pip install Pillow

अब जब आपके पास पैकेज इंस्टॉल है। आपको कैलकुलेटर के लिए आवश्यक आइकन्स डाउनलोड करने होंगे।

आप Google इमेजेज पर जा सकते हैं और आवश्यक आइकन्स डाउनलोड कर सकते हैं। हालांकि, यदि आप इस प्रोजेक्ट के लिए मेरे द्वारा उपयोग किए गए आइकन्स का वही सेट चाहते हैं, तो आप इसे यहां से डाउनलोड कर सकते हैं -

https://www.dropbox.com/sh/0zqd6zd9b8asmor/AAC3d2iOvMRl8INkbCuMUo_ya?dl=0.

सुनिश्चित करें कि आप सभी आइकन्स को "asset" नाम के फोल्डर में डाउनलोड करते हैं।

अगला, हमें आवश्यक मॉड्यूल्स इम्पोर्ट करने होंगे।

from tkinter import *
from PIL import Image # pip install Pillow
from PIL import ImageTk

और बस। अब आपके पास सब कुछ सेट अप है और शुरू करने के लिए तैयार है।

फंक्शन्स बनाना

सबसे पहले, हमें वे फंक्शन्स बनाने होंगे जिनका उपयोग GUI कंपोनेंट्स करेंगे।

तीन मुख्य फंक्शन्स हैं, एक जब नंबर या सिंबल दबाया जाता है, दूसरा जब इक्वल टू बटन दबाया जाता है और अंत में जब क्लियर बटन दबाया जाता है।

आइए पहले कुछ ग्लोबल वेरिएबल्स इनिशियलाइज़ करें -

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)

उदाहरण1

इक्वल टू बटन दबाए जाने पर फंक्शन -

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 फोल्डर में सेव करें।

नीचे दिए गए फॉर्मेट का पालन करें -

+---Working Directory
    +---Calculator.py
    +---assets
        +---Calculator
            +---All the icons.

अब, आइए उस टेक्स्ट फील्ड को डिज़ाइन करें जहां हम नंबर्स देखेंगे।

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")

अब, हम GUI विंडो में आइकन्स को एक-एक करके जोड़ने की दोहराव वाली प्रक्रिया का पालन करेंगे। नीचे इसका एक उदाहरण दिया गया है, बाकी के लिए इसका पालन करें या इस लेख के अंत में पूरे कोड से कॉपी करें।

उदाहरण

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")

उपरोक्त लाइनों के समान, बटन2, बटन3 और तब तक फॉलो करें जब तक आप सभी नंबर्स और सिंबल्स को कवर न कर लें।

और बस। यदि आप अब प्रोग्राम चलाते हैं, तो आपको एक बहुत ही अमूर्त दिखने वाला कैलकुलेटर दिखाई देगा।

यदि आप फॉलो नहीं कर पा रहे हैं, तो आप नीचे से पूरा कोड ले सकते हैं।

उदाहरण

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
    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")
    img2 = Image.open("assets/Calculator/two.PNG")
    img2 = img2.resize((width, height))
    twoImage = ImageTk.PhotoImage(img2)
    button2 = Button(window, image=twoImage, bg="white", command=lambda: press(2), height=height, width=width)
    button2.grid(row=2, column=1, sticky="nsew")
    img3 = Image.open("assets/Calculator/three.PNG")
    img3 = img3.resize((width, height))
    threeImage = ImageTk.PhotoImage(img3)
    button3 = Button(window, image=threeImage, bg="white", command=lambda: press(3), height=height, width=width)
    button3.grid(row=2, column=2, sticky="nsew")
    img4 = Image.open("assets/Calculator/four.PNG")
    img4 = img4.resize((width, height))
    fourImage = ImageTk.PhotoImage(img4)
    button4 = Button(window, image=fourImage, bg="white", command=lambda: press(4), height=height, width=width)
    button4.grid(row=3, column=0, sticky="nsew")
    img5 = Image.open("assets/Calculator/five.PNG")
    img5 = img5.resize((width, height))
    fiveImage = ImageTk.PhotoImage(img5)
    button5 = Button(window, image=fiveImage, bg="white", command=lambda: press(5), height=height, width=width)
    button5.grid(row=3, column=1, sticky="nsew")
    img6 = Image.open("assets/Calculator/six.PNG")
    img6 = img6.resize((width, height))
    sixImage = ImageTk.PhotoImage(img6)
    button6 = Button(window, image=sixImage, bg="white", command=lambda: press(6), height=height, width=width)
    button6.grid(row=3, column=2, sticky="nsew")
    img7 = Image.open("assets/Calculator/seven.PNG")
    img7 = img7.resize((width, height))
    sevenImage = ImageTk.PhotoImage(img7)
    button7 = Button(window, image=sevenImage, bg="white", command=lambda: press(7), height=height, width=width)
    button7.grid(row=4, column=0, sticky="nsew")
    img8 = Image.open("assets/Calculator/eight.PNG")
    img8 = img8.resize((width, height))
    eightImage = ImageTk.PhotoImage(img8)
    button8 = Button(window, image=eightImage, bg="white", command=lambda: press(8), height=height, width=width)
    button8.grid(row=4, column=1, sticky="nsew")
    img9 = Image.open("assets/Calculator/nine.PNG")
    img9 = img9.resize((width, height))
    nineImage = ImageTk.PhotoImage(img9)
    button9 = Button(window, image=nineImage, bg="white", command=lambda: press(9), height=height, width=width)
    button9.grid(row=4, column=2, sticky="nsew")
    img0 = Image.open("assets/Calculator/zero.PNG")
    img0 = img0.resize((width, height))
    zeroImage = ImageTk.PhotoImage(img0)
    button0 = Button(window, image=zeroImage, bg="white", command=lambda: press(0), height=height, width=width)
    button0.grid(row=5, column=1, sticky="nsew")
    imgx = Image.open("assets/Calculator/multiply.PNG")
    imgx = imgx.resize((width, height))
    multiplyImage = ImageTk.PhotoImage(imgx)
    buttonx = Button(window, image=multiplyImage, bg="white", command=lambda: press("*"), height=height, width=width)
    buttonx.grid(row=2, column=3, sticky="nsew")
    imgadd = Image.open("assets/Calculator/add.PNG")
    imgadd = imgadd.resize((width, height))
    addImage = ImageTk.PhotoImage(imgadd)
    buttonadd = Button(window, image=addImage, bg="white", command=lambda: press("+"), height=height, width=width)
    buttonadd.grid(row=3, column=3, sticky="nsew")
    imgdiv = Image.open("assets/Calculator/divide.PNG")
    imgdiv = imgdiv.resize((width, height))
    divImage = ImageTk.PhotoImage(imgdiv)
    buttondiv = Button(window, image=divImage, bg="white", command=lambda: press("/"), height=height, width=width)
    buttondiv.grid(row=5, column=3, sticky="nsew")
    imgsub = Image.open("assets/Calculator/subtract.PNG")
    imgsub = imgsub.resize((width, height))
    subImage = ImageTk.PhotoImage(imgsub)
    buttonsub = Button(window, image=subImage, bg="white", command=lambda: press("-"), height=height, width=width)
    buttonsub.grid(row=4, column=3, sticky="nsew")
    imgeq = Image.open("assets/Calculator/equal.PNG")
    imgeq = imgeq.resize((width, height))
    eqImage = ImageTk.PhotoImage(imgeq)
    buttoneq = Button(window, image=eqImage, bg="white", command=equal, height=height, width=width)
    buttoneq.grid(row=5, column=2, sticky="nsew")
    imgclear = Image.open("assets/Calculator/clear.PNG")
    imgclear = imgclear.resize((width, height))
    clearImage = ImageTk.PhotoImage(imgclear)
    buttonclear = Button(window, image=clearImage, bg="white", command=clear, height=height, width=width)
    buttonclear.grid(row=5, column=0, sticky="nsew")
    window.mainloop()

यदि आपको उपरोक्त प्रोग्राम के साथ फॉर्मेटिंग समस्याएं आ रही हैं, तो आप इसे https://github.com/SVijayB/PyHub/blob/master/Graphics/Simple%20Calculator.py से भी प्राप्त कर सकते हैं।

आउटपुट

Python में tkinter का उपयोग करके सिंपल GUI कैलकुलेटर कैसे बनाएं?


  1. पायथन में ट्किंटर प्रोग्रामिंग पायथन में ट्किंटर प्रोग्रामिंग

    ट्किंटर पायथन के लिए मानक GUI लाइब्रेरी है। ट्किंटर के साथ संयुक्त पायथन GUI एप्लिकेशन बनाने का एक तेज़ और आसान तरीका प्रदान करता है। ट्किंटर Tk GUI टूलकिट के लिए एक शक्तिशाली ऑब्जेक्ट-ओरिएंटेड इंटरफ़ेस प्रदान करता है।ट्किंटर का उपयोग करके GUI एप्लिकेशन बनाना एक आसान कार्य है। आपको बस निम्नलिखित चरण

  1. Python Tkinter का उपयोग करके सरल रजिस्ट्रेशन फॉर्म Python Tkinter का उपयोग करके सरल रजिस्ट्रेशन फॉर्म

    Tkinter GUI (ग्राफिकल यूज़र इंटरफेस) विकसित करने के लिए एक Python लाइब्रेरी है। हम UI (यूज़र इंटरफेस) का एक एप्लिकेशन बनाने के लिए tkinter लाइब्रेरी का उपयोग करते हैं, विंडोज और अन्य सभी ग्राफिकल यूज़र इंटरफेस बनाने के लिए। यदि आप Python 3.x का उपयोग कर रहे हैं (जो अनुशंसित है), तो Tkinter Python

  1. पायथन में Tkinter का उपयोग करके रंग खेल पायथन में Tkinter का उपयोग करके रंग खेल

    GUI एप्लिकेशन विकसित करने के लिए tkinter बहुत लोकप्रिय और आसान है। Tkinter का उपयोग करके आसानी से GUI गेम विकसित किए जा सकते हैं। यहाँ भी हम रंग खेल विकसित करने का प्रयास कर रहे हैं। इस खेल में खिलाड़ी को स्क्रीन पर दिखाई देने वाले शब्द का रंग दर्ज करना होता है और इस तरह स्कोर एक से बढ़ जाता है, इस