欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python编写一个简易计算器

程序员文章站 2022-06-22 15:50:08
import tkinter as tkroot = tk.Tk()root.title("简易计算器--Test")def display_label(numbers): if numbers == "C": Label_display["text"]="" elif numbers == "=": Label_display["text"] = str(eval(Label_display["text"])) else: L...
import tkinter as tk

root = tk.Tk()
root.title("简易计算器--Test")
def display_label(numbers):
    if numbers == "C":
        Label_display["text"]=""
    elif numbers == "=":
        Label_display["text"] = str(eval(Label_display["text"]))
    else:
        Label_display["text"] =Label_display["text"] + str(numbers)
Label_JL = tk.Label(root,text = "在这里输出").grid(row=0,columnspan = 4)
Label_display = tk.Label(root,width=10)
Label_1 = tk.Button(root,command = lambda :display_label(1),text='1',width=10).grid(row=2,column=0)
Label_2 = tk.Button(root,command = lambda :display_label(2),text='2',width=10).grid(row=2,column=1)
Label_3 = tk.Button(root,command = lambda :display_label(3),text='3',width=10).grid(row=2,column=2)
Label_4 = tk.Button(root,command = lambda :display_label(4),text='4',width=10).grid(row=3,column=0)
Label_5 = tk.Button(root,command = lambda :display_label(5),text='5',width=10).grid(row=3,column=1)
Label_6 = tk.Button(root,command = lambda :display_label(6),text='6',width=10).grid(row=3,column=2)
Label_7 = tk.Button(root,command = lambda :display_label(7),text='7',width=10).grid(row=4,column=0)
Label_8 = tk.Button(root,command = lambda :display_label(8),text='8',width=10).grid(row=4,column=1)
Label_9 = tk.Button(root,command = lambda :display_label(9),text='9',width=10).grid(row=4,column=2)
Label_add = tk.Button(root,command = lambda :display_label("+"),text='+',width=10).grid(row=5,column=0)
Label_delease = tk.Button(root,command = lambda :display_label("-"),text='-',width=10).grid(row=5,column=1)
Label_mutipy = tk.Button(root,command = lambda :display_label("*"),text='X',width=10).grid(row=5,column=2)
Label_result = tk.Button(root,command = lambda :display_label("="),text='=',width=10).grid(row=6,column=0)
Label_C = tk.Button(root,command = lambda :display_label("C"),text='c',width=10).grid(row=6,column=1)
Label_display.grid(row=1,columnspan=4)


root.mainloop()

本文地址:https://blog.csdn.net/FurceireDown/article/details/108857262
相关标签: 小练习 python