python gui calculator

from tkinter import *


def adding():
    try:
        text1 = int(Textbox1.get())
        text2 = int(Textbox2.get())
    except Exception:
        Output.delete(0, END)
        Output.insert(0, 'Error! Enter a number please!')
        return
    text_output = str(text1 + text2)
    Output.delete(0, END)
    Output.insert(0, text_output)

root = Tk()
root.title('Adding')
root.geometry('500x500')
Textbox1 = Entry(root)
Textbox1.pack(ipadx=50, ipady=10)
spacing = Label(root, text='+')
spacing.pack()
Textbox2 = Entry(root)
Textbox2.pack(ipadx=50, ipady=10)
spacing2 = Label(root)
spacing2.pack()
Button1 = Button(root, text='Add The numbers!', command=adding)
Button1.pack()
spacing3 = Label(root)
spacing3.pack()
Output = Entry(root)
Output.pack(ipadx=50)
root.mainloop()

3
3

                                    from tkinter import *
 
class Calculator:
    def __init__(self,master):
        self.master = master
        master.title("My Calculator @ www.pickupbrain.com")
        master.configure(bg='#C0C0C0')
         
        #creating screen widget
        self.screen = Text(master, state='disabled', width=50,height= 3, background="#EAFAF1", foreground="#000000",font=("Arial",15,"bold"))
         
        #Screen position in window
        self.screen.grid(row=0,column=0,columnspan=4,padx=2,pady=2)
        self.screen.configure(state='normal')
         
        #initialize screen value as empty
        self.equation=''
         
        #create buttons
        b1 = self.createButton(7)
        b2 = self.createButton(8)
        b3 = self.createButton(9)
        b4 = self.createButton(u"\u232B",None)
        b5 = self.createButton(4)
        b6 = self.createButton(5)
        b7 = self.createButton(6)
        b8 = self.createButton(u"\u00F7")
        b9 = self.createButton(1)
        b10 = self.createButton(2)
        b11 = self.createButton(3)
        b12 = self.createButton('*')
        b13 = self.createButton('.')
        b14 = self.createButton(0)
        b15 = self.createButton('+')
        b16 = self.createButton('-')
        b17 = self.createButton('=', None,35)
         
        #stored all buttons in list
        buttons = [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17]
         
        #initalize counter
        count=0
         
        #arrange buttons with grid manager
        for row in range(1,5):
            for column in range(4):
                buttons[count].grid(row = row, column= column,padx=0,pady=0)
                count +=1
                 
                #arrange last button '=' at the bottom
            buttons[16].grid(row=5,column=0,columnspan=4)
                 
    def createButton(self,val,write=True,width=8):
         
        #this function creates a button and takes one compulsary argument, the value that should be on the button
        return Button(self.master,text=val,command= lambda:self.click(val,write),width=width,background="#ffffff",foreground ="#1f4bff",font=("times",20,"bold"))
                 
    def click(self,text,write):
        #this function handles the actions when you
        #click a button 'write' arguement, if true
        #than value val should be written on screen,
        #if none then should not be written on screen
        if write == None:
             
            #Evaluates when there is an equation to be evaluated
            if text == '=' and self.equation:
                #replace the unicode values of division ./. with python division
                #symbol / using regex
                self.equation = re.sub(u"\u00F7", '/', self.equation)
                print(self.equation)
                answer = str(eval(self.equation))
                self.clear_screen()
                self.insert_screen(answer,newline = True)
            elif text == u"\u232B":
                self.clear_screen()
             
        else:
            #add text to screen
                self.insert_screen(text)
                 
    def clear_screen(self):
        #to clear screen
        #set equation to empty before deleteing screen
        self.equation = ''
        self.screen.configure(state = 'normal')
        self.screen.delete('1.0', END)
                     
    def insert_screen(self, value, newline = False):
        self.screen.configure(state ='normal')
        self.screen.insert(END,value)
         
        #record every value inserted in screen
        self.equation += str(value)
        self.screen.configure(state = 'disabled')
                     
def calci():
     
    #Function that creates calculator GUI
    root = Tk()
    my_gui = Calculator(root)
    root.mainloop()
     
# Running Calculator    
calci()                                    

3 (3 Votes)
0
5
1

                                    # importing everyting from tkinter
from tkinter import *
# expression to access among all the functions
expression = ""
# functions
def input_number(number, equation):
   # accessing the global expression variable
   global expression
   # concatenation of string
   expression = expression + str(number)
   equation.set(expression)
def clear_input_field(equation):
   global expression
   expression = ""
   # setting empty string in the input field
   equation.set("Enter the expression")
def evaluate(equation):
global expression
# trying to evaluate the expression
try:
result = str(eval(expression))
# showing the result in the input field
equation.set(result)
# setting expression to empty string
expression = ""
except:
# some error occured
# showing it to the user equation.set("Enter a valid expression")
expression = ""
# creating the GUI
def main():
   # main window window = Tk()
   # setting the title of GUI window
   window.title("Calculator")
   # set the configuration of GUI window
   window.geometry("325x175")
   # varible class instantiation
   equation = StringVar()
   # input field for the expression
   input_field = Entry(window, textvariable=equation)
   input_field.place(height=100)
   # we are using grid position
   # for the arrangement of the widgets
   input_field.grid(columnspan=4, ipadx=100, ipady=5)
   # settin the placeholder message for users
   equation.set("Enter the expression")
   # creating buttons and placing them at respective positions
   _1 = Button(window, text='1', fg='white', bg='black', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
   _1.grid(row=2, column=0)
   _2 = Button(window, text='2', fg='white', bg='black', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
   _2.grid(row=2, column=1)
   _3 = Button(window, text='3', fg='white', bg='black', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
   _3.grid(row=2, column=2)
   _4 = Button(window, text='4', fg='white', bg='black', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
   _4.grid(row=3, column=0)
   _5 = Button(window, text='5', fg='white', bg='black', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
   _5.grid(row=3, column=1)
   _6 = Button(window, text='6', fg='white', bg='black', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
   _6.grid(row=3, column=2)
   _7 = Button(window, text='7', fg='white', bg='black', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
   _7.grid(row=4, column=0)
   _8 = Button(window, text='8', fg='white', bg='black', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
   _8.grid(row=4, column=1)
   _9 = Button(window, text='9', fg='white', bg='black', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
   _9.grid(row=4, column=2)
   _0 = Button(window, text='0', fg='white', bg='black', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
   _0.grid(row=5, column=0)
   plus = Button(window, text='+', fg='white', bg='black', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
   plus.grid(row=2, column=3)
   minus = Button(window, text='-', fg='white', bg='black', bd=0, command=lambda: input_number('-', equation), height=2, width=7)
   minus.grid(row=3, column=3)
   multiply = Button(window, text='*', fg='white', bg='black', bd=0, command=lambda:  input_number('*', equation), height=2, width=7)
   multiply.grid(row=4, column=3)
   divide = Button(window, text='/', fg='white', bg='black', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
   divide.grid(row=5, column=3)
   equal = Button(window, text='=', fg='white', bg='black', bd=0, command=lambda: evaluate(equation), height=2, width=7)
   equal.grid(row=5, column=2)
   clear = Button(window, text='Clear', fg='white', bg='black', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
   clear.grid(row=5, column=1)
   # showing the GUI
   window.mainloop()
# start of the program
if __name__ == '__main__':
      main()

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
gui calculator for pyhton python gui calculator step by step calculator.py gui python simple calculator gui python simple calculator with gui python calculator in gui python gui calculator source code python calculator program gui gui calculator code in python gui calculator python calculator python 3 gui python calculator with gui calculator in python with gui source python how to make a calculator with gui make gui calculator with python create a gui calculator in python calculator in tkinter from sctract create calcultor in python project calculator in python gui making a calculator in tkinter how to make a calculator without display box tkinter how to make a calculator in python tkinter how to make a gui calculator in python simple calculator gui in python do calculations with tkinter python Simple Calculator (using tkinter) creating tkinter gui calculator calculator with gui in python make databse ui simple calculation code make clculator with interface in python strcutured package for calculator using python create calculator app in python how to create a calculator gui in python create calculator in python using tkinter simple addition calculator in python using tkinter calculator using tkinter for only addition create a calculator app with python addition calculator using python tkinter create a calculator with tinkter window geometry calculator in python how to create calculator with tkinter calculator python tkinter calculator gui code in python calculator python gui calculator in python with gui how to make a calculator tkinter how to keep adding number in cacultor tkinter beautiful calculator using tkinker python calculator app calculator with tkinter python gui calculator code calculator gui python kalkulator gui python calculator GUI using tk inter creating a calculator in tkinter Calculator in Python Tkinter gui calculator in python doubt python code calculator with gui how to make calculator in python tkinter full code how to make calculator in python tkinter how to crreat a calculator in python with graphical user interface calculator gui tkinter python 3 windows GUI calculator code how to make a calculator in python with gui building a gui calculator calculator in tkinter python create calculator using any python gui python tkinter calculator source code for GUI calculator in python calculator using tkinter python program for gui calculator calculator program usin tkinter in python how to make graphical calculator pythob electronics how to make calculator in python building a calculator in tkinter simple add calculator in tkinter python calculator tkinter gui calculator in python simple python code for calculator tkinter design simple python gui calculator create calculator in tkinter caculator in tkinter calculator with python tkinter python gui calculator tkinter calculator calculator gui projects in python calculator in tkinter python gui calculator program calculator using python tkinter Tkinter gui calculator with download pdf tkinter code for calculator python calculator tkinter gui python code for calculator gui python calculator gui make a compex calculator app in python how to make calculator layout in python using tkinter tkinter calculator python 3 code tkinter claculetor
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source