# Gorgeous design of tkinter calculator in Python

It's a terrible problem. It's only found when writing small programs

No matter what is written on the book, only when you write the program by hand, can you realize that there are many knowledge points that the book does not have. There may be, but many of them are brought by one stroke. Only when you go to Baidu to look up various materials can you

Practice is the fundamental way to solve problems. Just as we read books, 90% of them are doing exercises, and only a few of them are learning knowledge

Learning programming is the same. Only writing a small program can make you know that you are not good at that place, and you can understand it

Here is a small design of the calculator:

```import tkinter

# Calculation
def calculate():
# Calculation
# equ.get() gets the equ content at this time
# eval computing
result = eval(equ.get())
# Splicing
equ.set(equ.get() + "=\n" + str(result))

# Put our numbers in the text box
def show(buttonString):
# Get content
content = equ.get()
# Judge if the content is 0, it is equivalent to no calculation, so null string splicing, or 0
# If the content is 10, the content is 10, and then the numbers we press are spliced
if content == "0":
content = ""
equ.set(content + buttonString)

# Clear, set to 0
def clear():
equ.set("0")

# Abdication
def backSpace():
# Delete previous character
equ.set(str(equ.get()[:-1]))

# main program
root = tkinter.Tk()
# Set program title
root.title("Clean Calculator Beta ")
# The frame can be dragged, 0,0 means it cannot be dragged
root.resizable(0, 0)
# equ is the number of display area
equ = tkinter.StringVar()
# The default is 0.
equ.set("0")

# Set text box
# width is 25
# height is 2
# Relief refers to the relief style of the border, similar to the 3D style
# anchor refers to the orientation of the text in the component, and SE is the lower left corner
# textvariable refers to variable text, which changes with the change of variables
label = tkinter.Label(root, width=25, height=2, relief="raised", anchor=tkinter.SE, textvariable=equ)
# Grid put the button on the screen, grid layout
# row refers to the horizontal position of the entry component in the grid
# column refers to the vertical position of the entry component in the grid
# columnspan refers to the number of merges that set the entry component in the column direction
# Padx / pad sets the distance between the component boundary and the container (think of as window boundary) or between the control boundaries
label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)

# Button for row1

# Clear display area button AC
# Text text on button
# bg background color
# command execute the clear function when the button is clicked
buttonClear = tkinter.Button(root, text="AC", width=5, bg="#ff6348", command=clear)
buttonClear.grid(row=1, column=0)

# Exit button
buttonBack = tkinter.Button(root, text="<-", width=5, bg="#ff6348", command=backSpace)
buttonBack.grid(row=1, column=1)

# Remaining button
# You need to use the lambda function to pass parameters
buttonYu = tkinter.Button(root, text="%", width=5, bg="lightblue", command=lambda: show("%"))
buttonYu.grid(row=1, column=2)

# Except button
buttonDivision = tkinter.Button(root, text="/", width=5, bg="lightblue", command=lambda: show("/"))
buttonDivision.grid(row=1, column=3)

# Button for row2

# Number 7
button7 = tkinter.Button(root, text="7", width=5, bg="orange", command=lambda: show("7"))
button7.grid(row=2, column=0)

# Number 8
button8 = tkinter.Button(root, text="8", width=5, bg="orange", command=lambda: show("8"))
button8.grid(row=2, column=1)

# Number 9
button9 = tkinter.Button(root, text="9", width=5, bg="orange", command=lambda: show("9"))
button9.grid(row=2, column=2)

# Multiplication button
buttonMultiplication = tkinter.Button(root, text="*", width=5, bg="lightblue", command=lambda: show("*"))
buttonMultiplication.grid(row=2, column=3)

# Button for row3

# Number 4
button4 = tkinter.Button(root, text="4", width=5, bg="orange", command=lambda: show("4"))
button4.grid(row=3, column=0)

# Number 5
button5 = tkinter.Button(root, text="5", width=5, bg="orange", command=lambda: show("5"))
button5.grid(row=3, column=1)

# Number 6
button6 = tkinter.Button(root, text="6", width=5, bg="orange", command=lambda: show("6"))
button6.grid(row=3, column=2)

# Subtraction button
buttonSubtraction = tkinter.Button(root, text="-", width=5, bg="lightblue", command=lambda: show("-"))
buttonSubtraction.grid(row=3, column=3)

# Button for row4

# Number 1
button1 = tkinter.Button(root, text="1", width=5, bg="orange", command=lambda: show("1"))
button1.grid(row=4, column=0)

# Number 2
button2 = tkinter.Button(root, text="2", width=5, bg="orange", command=lambda: show("2"))
button2.grid(row=4, column=1)

# Number 3
button3 = tkinter.Button(root, text="3", width=5, bg="orange", command=lambda: show("3"))
button3.grid(row=4, column=2)

# Add button
buttonAdd = tkinter.Button(root, text="+", width=5, bg="lightblue", command=lambda: show("+"))
buttonAdd.grid(row=4, column=3)

# Button for row5

# Decimal button
buttonPoint = tkinter.Button(root, text=".", width=5, bg="orange", command=lambda: show("."))
buttonPoint.grid(row=5, column=0)

# Number 0
button0 = tkinter.Button(root, text="0", width=5, bg="orange", command=lambda: show("0"))
button0.grid(row=5, column=1)

# Be equal to
buttonEqual = tkinter.Button(root, text="=", width=12, bg="lightgreen", command=calculate)
buttonEqual.grid(row=5, column=2, columnspan=2)

# Program cycle
root.mainloop()

```

Operation result:

Because we pay more attention to the interface, we don't pay much attention to judge the validity of the input. We hope the readers can optimize it

Attachment:
Because of the incompatibility of color in Mac os system, the author has consulted many materials. According to the current knowledge level of the article, there is only one kind of incompatibility method, which is not infeasible, but has no similar effect with windows system

``` bg="orange"
# replace with
highlightbackground="orange"
```

You can show the color, but!! Windows does not support it. Only Mac os supports it. If windows is replaced, the color will not be displayed

Thank you for watching. I will keep updating. If there are any mistakes or suggestions, please contact me personally

32 original articles published, 36 praised, 4108 visited

Posted on Thu, 12 Mar 2020 01:48:11 -0400 by CountryGirl