When working with desktop applications, is there a moment when you want to learn desktop application development? I suggest you do a little bit of research and don’t waste too much time, because no company hires Python programmers to develop desktop programs, right?

(Want to learn Python? Python Learning exchange group: 1039649593, to meet your needs, materials have been uploaded to the group file stream, you can download! There is also a huge amount of new 2020Python learning material.)

Development environment:

  • Python 3.6
  • Pycharm

code

Interface Settings

1. Import modules

import tkinter as tk
Copy the code

2. Instantiate a form object

root = tk.Tk()
Copy the code

3. The title

Root.title (' calculator ')Copy the code

4. Size and location

root.geometry("295x280+150+150")
Copy the code

The transparency of the 5.

Root. The attributes (0.9) "- alpha",Copy the code

Background of 6.

root["background"] = "#ffffff"
Copy the code

7. The label

Lable1 = tk.Label(root, textVariable =result_num, width=20, height=2, font=(' text ', 20), justify='left', background='#ffffff', anchor='se')Copy the code

Layout of 8.

lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)
Copy the code

9. The button

Button_clear = tk.Button(root, text='C', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: Clear () button_back = tk.Button(root, text='←', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: Button(root, text='/', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', button_division = tk.Button(root, text='/', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: Operator ('/')) button_x = tk.Button(root, text='x', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('*')) button_clear .grid(padx=4, row=1, column=0) button_back .grid(padx=4, row=1, column=1) button_division .grid(padx=4, row=1, column=2) button_multiplication .grid(padx=4, row=1, Column =3) button_seven = tk.Button(root, text='7', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', column=3) button_seven = tk.Button(root, text='7', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: Append_num ('7')) button_eight = tk.Button(root, text='8', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: Append_num ('8') button_nine = tk.Button(root, text='9', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: Append_num ('9')) button_subtraction = tk.Button(root, text=' -- ', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('-')) button_seven .grid(padx=4, row=2, column=0) button_eight .grid(padx=4, row=2, column=1) button_nine .grid(padx=4, row=2, column=2) button_subtraction .grid(padx=4, row=2, Column =3) button_four = tk.Button(root, text='4', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('4')) button_four.grid(padx=4, pady=4, row=3, column=0) button_five = tk.Button(root, text='5', width=5, Font =('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: Append_num ('5') button_five. Grid (padx=4, row=3, column=1) button_six = tk. grid(root, text='6', width=5, font=('宋体', column=1) 16), relief='flat', background='#FFDEAD', command=lambda: append_num('6')) button_six.grid(padx=4, row=3, column=2) button_addition = tk.Button(root, text='+', width=5, Font =('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('+')) button_addition.grid(padx=4, row=3, column=3) button_one = tk.Button(root, text='1', width=5, Font =('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: Append_num ('1') button_one.grid(padx=4, row=4, column=0) button_two = tk. grid(root, text='2', width=5, font=('宋体',) 16), relief='flat', background='#FFDEAD', command=lambda: Append_num ('2') button_two. Grid (padx=4, row=4, column=1) button_three = tk. grid(root, text='3', width=5, font=('宋体', column=1) 16), relief='flat', background='#FFDEAD', command=lambda: append_num('3')) button_three.grid(padx=4, row=4, column=2) button_equal = tk.Button(root, text='=', width=5, height=3, Font =('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: equal()) button_equal.grid(padx=4, row=4, rowspan=5, column=3) button_zero = tk.Button(root, text='0', width=12, Font =('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('0')) button_zero.grid(padx=4, pady=4, row=5, column=0, columnspan=2) button_decimal = tk.Button(root, Text ='.', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('.')) button_decimal.grid(padx=4, row=5, column=2)Copy the code

Now we get to the interface

function

Add Numbers

def append_num(i):
    lists.append(i)
    result_num.set(''.join(lists))
Copy the code

Select operation symbol

def operator(i):
    if len(lists) > 0:
        if lists[-1] in ['+', '-', '*', '/']:
            lists[-1] = i
        else:
            lists.append(i)
        result_num.set(''.join(lists))
Copy the code

reset

def clear():
    lists.clear()
    result_num.set(0)
Copy the code

backspace

def back():
    del lists[-1]
    result_num.set(lists)
Copy the code

The equal sign

def equal():
    a = ''.join(lists)
    end_num = eval(a)
    result_num.set(end_num)
    lists.clear()
    lists.append(str(end_num))
Copy the code

Define a list to collect input

lists = []
result_num = tk.StringVar()
result_num.set(0)
Copy the code

Finally run the code, the result is shown below

First try

Calculate the result