0% found this document useful (0 votes)
71 views3 pages

Python Tkinter GUI Calculator Code

This document describes a GUI calculator created using Python's Tkinter module. It defines functions for displaying numbers and operators, clearing the calculation, and evaluating the calculation. Buttons are created for each number and operator and placed in a grid layout. The buttons call the display functions. An equals button calls the calculate function to evaluate the expression and display the result. The calculator provides a simple GUI for performing basic arithmetic calculations.

Uploaded by

nhbh7579707644
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views3 pages

Python Tkinter GUI Calculator Code

This document describes a GUI calculator created using Python's Tkinter module. It defines functions for displaying numbers and operators, clearing the calculation, and evaluating the calculation. Buttons are created for each number and operator and placed in a grid layout. The buttons call the display functions. An equals button calls the calculate function to evaluate the expression and display the result. The calculator provides a simple GUI for performing basic arithmetic calculations.

Uploaded by

nhbh7579707644
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

*GUI Calculator in python using TKinter……….

import tkinter

from tkinter import*

root=Tk()

[Link]("Calculator")

[Link]("570x600+100+200")

[Link](False,False)

[Link](bg="#17161b")

equation=""

def show(value):

global equation

equation+=value

label_result.config(text=equation)

def clear():

global equation

equation = ""

label_result.config(text=equation)

def calculate():
global equation

result=""

if equation!="":

try:

result=eval(equation)

except:

result="error"

equation=""

label_result.config(text=result)

label_result=Label(root,width=25,height=2,text="",font=("arial",30))

label_result.pack()

Button(root,width=5,height=1, text="C",command=lambda:clear(), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#3697f5").place(x=10,y=100)

Button(root,width=5,height=1, text="/",command=lambda:show("/"), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=150,y=100)

Button(root,width=5,height=1,text="%",command=lambda:show("%"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=290,y=100)

Button(root,width=5,height=1, text="*", command=lambda:show("*"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=430,y=100)

Button(root,width=5,height=1, text="7",command=lambda:show("7"), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=10,y=200)

Button(root,width=5,height=1, text="8",command=lambda:show("8"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=150,y=200)

Button(root,width=5,height=1, text="9",command=lambda:show("9"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=290,y=200)
Button(root,width=5,height=1, text="-",command=lambda:show("-"), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=430,y=200)

Button(root,width=5,height=1, text="4",command=lambda:show("4"), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=10,y=300)

Button(root,width=5,height=1, text="5",command=lambda:show("5"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=150,y=300)

Button(root,width=5,height=1, text="6",command=lambda:show("6"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=290,y=300)

Button(root,width=5,height=1, text="+",command=lambda:show("+"), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=430,y=300)

Button(root,width=5,height=1, text="1",command=lambda:show("1"), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=10,y=400)

Button(root,width=5,height=1, text="2",command=lambda:show("2"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=150,y=400)

Button(root,width=5,height=1, text="3",command=lambda:show("3"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=290,y=400)

Button(root,width=11,height=1, text="0",command=lambda:show("0"),font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=10,y=500)

Button(root,width=5,height=1, text=".",command=lambda:show("."), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#2a2d36").place(x=290,y=500)

Button(root,width=5,height=3, text="=",command=lambda:calculate(), font=("arial",30,"bold"),bd=1,fg="#fff",bg="#fe9037").place(x=430,y=400)

[Link]()

Common questions

Powered by AI

The 'show(value)' function captures user inputs by updating the global 'equation' variable with the button value pressed. This integration allows the calculator to construct a mathematical expression incrementally as users interact with the interface, building the logic needed for executing calculations .

The 'calculate()' function ensures validity using a check for a non-empty equation, followed by a try-except block. If 'eval()' raises an exception, indicating an invalid expression, the catch block outputs 'error'. This mechanism allows only syntactically correct input to be computed and displayed, maintaining application reliability .

The code uses a try-except block to handle errors that occur during the evaluation of the mathematical expression entered by the user. If an exception is raised (e.g., due to an invalid expression), the 'except' block sets the result to 'error' and clears the current equation .

Using 'eval()' presents security risks, as it can execute arbitrary code. If user input is not properly validated, it could lead to malicious code execution. To mitigate this, it is advisable to sanitize and strictly control input before evaluation or replace 'eval()' with a safer mathematical expression parser library specifically designed to evaluate mathematical operations securely .

The 'Label' widget in the calculator application displays the current equation and results of calculations. It is updated through the 'config' method during calculations and input entries. Functions like 'show', 'clear', and 'calculate' update the label by changing its 'text' property to reflect the equation string or calculation result .

In this application, the appearance of buttons, including their color, font, and size, is set using the 'Button' widget parameters. The 'fg' and 'bg' parameters are used to define the text and background colors, respectively. The 'font' parameter specifies the font style, size, and weight, while the 'width' and 'height' parameters define the button's size in pixels .

The 'global' keyword is used to modify the global variable 'equation' from within the functions 'show', 'clear', and 'calculate'. By declaring 'equation' as global, these functions can update and manipulate its value, which is necessary for the calculator to retain and modify the expression as users enter their input .

Making the GUI window non-resizable benefits the application by preserving layout integrity and ensuring consistent user experience, as resizing could distort the button alignment and alter the user interface's visual balance. However, it limits flexibility for users wanting to adjust the window size to fit specific screen requirements or preferences .

The 'mainloop()' function is crucial as it enters the Tkinter event loop. This loop awaits user actions/events such as button clicks, ensuring the application remains responsive and updates the interface accordingly. Without calling 'root.mainloop()', the application would not be able to interact with user inputs or perform GUI updates .

The placement of buttons is calculated using the 'place()' method with x and y coordinates, creating a grid-like layout critical for intuitive user interaction. This positioning helps users quickly find and press the desired buttons, mimicking a physical calculator layout, thereby improving usability and reducing user errors during operation .

You might also like