Answers for "python calculator gui"

1

python calculator gui

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()
Posted by: Guest on March-25-2021
0

python gui calculator

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()                                   
Posted by: Guest on July-05-2021
-2

python code to make a calculator frame

# 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()
Posted by: Guest on September-09-2020

Python Answers by Framework

Browse Popular Code Answers by Language