GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Python GUI Calculator Project

Gk Scientist December 25, 2022 No Comments
Tweet WhatsApp Telegram

Python GUI Calculator Project:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) Open the PyCharm code editor and create a new project in it. We named the project calculator.py. To build a GUI Calculator in Python we need to install the package. For this open the terminal in PyCharm and install one package i.e. tkinter. You install this package by simply typing in the terminal following command i.e. pip install tk and then press enter as shown below.

install tk for calculator in python - Python GUI Calculator Project

Note: If the package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here.

(2) Now write the following code for creating Python GUI Calculator Project as shown below.

python gui calculator code 1 - Python GUI Calculator Project
python gui calculator code 2 - Python GUI Calculator Project
python gui calculator code 3 - Python GUI Calculator Project
python gui calculator code 4 - Python GUI Calculator Project

Python GUI Calculator Project Source Code:

import tkinter

calculation = ""

def add_to_calculation(symbol):
   global calculation
   calculation += str(symbol)
   txt_result.delete(1.0,"end")
   txt_result.insert(1.0,calculation)

def evaluate_calculation():
  global calculation
  try:
      calculation = str(eval(calculation))
      txt_result.delete(1.0,"end")
      txt_result.insert(1.0, calculation)
  except:
      clear_field()
      txt_result.insert(1.0,"Error...")

def clear_field():
    global calculation
    calculation = ""
    txt_result.delete(1.0,"end")

cal_window = tkinter.Tk()
cal_window.geometry("400x280")
cal_window.title("Simple GUI Calculator")
cal_window.config(bg="black")
cal_window.resizable(False,False)

txt_result = tkinter.Text(cal_window, height=2, width=27,
                          font=("Open Sans",20),bg="white",fg="black")
txt_result.grid(columnspan=6)

button1 = tkinter.Button(cal_window, text="1", command=lambda: add_to_calculation(1),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button1.grid(row=2,column=1)

button2 = tkinter.Button(cal_window, text="2", command=lambda: add_to_calculation(2),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button2.grid(row=2,column=2)

button3 = tkinter.Button(cal_window, text="3", command=lambda: add_to_calculation(3),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button3.grid(row=2,column=3)

button4 = tkinter.Button(cal_window, text="4", command=lambda: add_to_calculation(4),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button4.grid(row=3,column=1)

button5 = tkinter.Button(cal_window, text="5", command=lambda: add_to_calculation(5),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button5.grid(row=3,column=2)

button6 = tkinter.Button(cal_window, text="6", command=lambda: add_to_calculation(6),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button6.grid(row=3,column=3)

button7 = tkinter.Button(cal_window, text="7", command=lambda: add_to_calculation(7),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button7.grid(row=4,column=1)

button8 = tkinter.Button(cal_window, text="8", command=lambda: add_to_calculation(8),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button8.grid(row=4,column=2)

button9 = tkinter.Button(cal_window, text="9", command=lambda: add_to_calculation(9),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button9.grid(row=4,column=3)

button0 = tkinter.Button(cal_window, text="0", command=lambda: add_to_calculation(0),
                         width=6,font=("Open Sans",16),bg="black",fg="white")
button0.grid(row=5,column=2)

button_plus = tkinter.Button(cal_window, text="+", command=lambda: add_to_calculation("+"),
                             width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_plus.grid(row=2,column=4)

button_minus = tkinter.Button(cal_window, text="-", command=lambda: add_to_calculation("-"),
                              width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_minus.grid(row=3,column=4)

button_multiplication = tkinter.Button(cal_window, text="*", command=lambda: add_to_calculation("*"),
                                       width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_multiplication.grid(row=4,column=4)

button_division = tkinter.Button(cal_window, text="/", command=lambda: add_to_calculation("/"),
                                 width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_division.grid(row=5,column=4)

button_openpar = tkinter.Button(cal_window, text="(", command=lambda: add_to_calculation("("),
                                width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_openpar.grid(row=5,column=1)

button_closepar = tkinter.Button(cal_window, text=")", command=lambda: add_to_calculation(")"),
                                 width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_closepar.grid(row=5,column=3)

button_equals = tkinter.Button(cal_window, text="=", command=evaluate_calculation,
                               width=14,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_equals.grid(row=6,column=3,columnspan=2)

button_clear = tkinter.Button(cal_window, text="C", command=clear_field,
                              width=14,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_clear.grid(row=6,column=1,columnspan=2)

cal_window.mainloop()

(3) After running the code, the output window will pop us as shown below.

python gui calculator - Python GUI Calculator Project

Tutorial MySQL
Tutorial Python
Procedure-Oriented Programming
Object-Oriented Programming
Modular Programming
Structured Programming
Python (programming language)– Wikipedia
Prev Article
Next Article

Related Articles

Create CheckBox in Java
Create CheckBox in Java: We can use the Eclipse IDE …
Gk Scientist January 14, 2023 Computer

Create CheckBox in Java

Database Models or Data Models
Data Models: The database model, commonly known as the data …
Gk Scientist October 16, 2022 Computer

Database Models or Data Models

Python SQLite Insert Query
Python SQLite Insert Query: We can use the PyCharm code …
Gk Scientist December 8, 2022 Computer

Python SQLite Insert Query

Computer Software and Hardware
Computer Software and Hardware: What is Software? Software is an …
Gk Scientist August 2, 2022 Computer

Computer Software and Hardware

C++ Character Set and Program Structure
What is C++? C++ is an object-oriented programming (OOP) language. …
Gk Scientist August 18, 2022 Computer

C++ Character Set and Program Structure

Python MySQL Self Join and Full Join
Python MySQL Self Join and Full Join: (1) Self Join- …
Gk Scientist December 15, 2022 Computer

Python MySQL Self Join and Full Join

Inheritance in Java
Inheritance in Java: We can use the Eclipse IDE for …
Gk Scientist January 10, 2023 Computer

Inheritance in Java

Java KeyListener
Java KeyListener: We can use the Eclipse IDE for this …
Gk Scientist January 18, 2023 Computer

Java KeyListener

Leave a Reply Cancel Reply

Search

  • Popular
  • Recent

GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (34) Biology Questions (89) Chemistry (57) Computer (215) Current Affairs (4) Current Content (0) Economy (18) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (115) Physics (89) Political Science (134) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...