0% found this document useful (0 votes)
5 views2 pages

Python Tkinter Calculator Code

The document describes an Interface class that creates a graphical user interface for a basic calculator in Python using Tkinter. The class creates buttons for numbers and operators, handles click events, and displays results in a text box. When clicking on the buttons, the operation is stored as a string and evaluated when pressing "=".
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)
5 views2 pages

Python Tkinter Calculator Code

The document describes an Interface class that creates a graphical user interface for a basic calculator in Python using Tkinter. The class creates buttons for numbers and operators, handles click events, and displays results in a text box. When clicking on the buttons, the operation is stored as a string and evaluated when pressing "=".
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

from tkinter import Tk, Text, Button, END, re

class Interface:
def __init__(self, window):
Initialize the window with a title
[Link]=window
[Link]('Calculator')

Add a text box to serve as the calculator screen


[Link]=Text([Link], state="disabled", width=40, height=3,
orchid

Position the screen in the window


[Link](row=0, column=0, columnspan=4, padx=5, pady=5)

Initialize the operation displayed on the screen as an empty string


[Link]

Create the calculator buttons


[Link](7)
[Link](8)
[Link](9)
[Link](u" B", escribir=False)
button5=[Link](4)
button6=[Link](5)
[Link](6)
[Link](u"÷")
button9=[Link](1)
[Link](2)
button11=[Link](3)
[Link]("*")
[Link](".")
[Link](0)
button15=[Link]("+")
[Link]("-")
[Link]("=", write=False, width=20, height=2)

Locate the buttons with the grid manager


buttons=[button1, button2, button3, button4, button5, button6, button7, button8,
button9, button10, button11, button12, button13, button14, button15, button16, button17
contador=0
for row in range(1,5):
for column in range(4):
buttons[counter].grid(row=row, column=column)
counter += 1
Place the last button at the end
buttons[16].grid(row=5,column=0,columnspan=4)

return

Create a button showing the value passed as a parameter


def createButton(self, value, write=True, width=9, height=1):
return Button([Link], text=value, width=width, height=height,
font=("Helvetica",15), command=lambda:[Link](valor,escribir))

Controls the event triggered when clicking a button


def click(self, text, write):
If the parameter 'write' is True, then the text parameter must
show on screen. If it is False, no.
if not write:
Only calculate if there is an operation to be evaluated and if the user
pressed '='
if text=="=" and [Link]!="":
Replace the unicode value of division with the operator
Python division '/'
[Link] = [Link](u"\u00F7", "/", [Link])
result = str(eval([Link]))
[Link]
[Link]()
[Link](result)
If the delete button was pressed, clear the screen
elif text == u"\u232B":
[Link]
[Link]()
#Show text
else:
[Link] += str(text)
[Link](text)
return

Clear the content of the calculator screen


def clearScreen(self):
[Link](state="normal")
[Link]("1.0", END)
[Link](state="disabled")
return

Display the content of the operations and the


results
def displayOnScreen(self, value):
[Link](state="normal")
[Link](END, value)
[Link](state="disabled")
return

main_window = Tk()
Interface(main_window)
main_window.mainloop()

You might also like