学号:20211321 李心怡 实验二《Python程序设计》实验报告

学号:20211321李心怡 2021-2022-2 《Python程序设计》实验二报告

课程:《Python程序设计》
班级: 202113
姓名: 李心怡
学号:20211321
实验教师:王志强
实验日期:2022年4月5日
必修/选修: 公选课

1.实验内容

此处填写实验的具体内容;

from tkinter import *
def calculate():
    result = eval(equ.get())
    equ.set(equ.get() + "=\n" + str(result))

def show(buttonString):
    content = equ.get()
    if content =='0':
        content = ''
    equ.set(content + buttonString)

def backspace():
    equ.set(str(equ.get()[:-1]))

def clear():
    equ.set('0')

root = Tk()
root.title("计算器")

equ = StringVar()
equ.set('0')

label = Label(root, width=25, height=2, relief='raised', anchor=SE, textvariable=equ)
label.grid(row=0, column=0, columnspan=4, padx=5, pady = 5)

clearButton = Button(root, text='C', fg='blue', width=5, command=clear)
clearButton.grid(row = 1, column = 0)


Button(root, text='DEL', width=5, command=backspace).grid(row=1, column=1)
Button(root, text='%', width=5, command=lambda:show("%")).grid(row=1, column=2)
Button(root, text='/', width=5, command=lambda:show("/")).grid(row=1, column=3)
Button(root, text='7', width=5, command=lambda:show("7")).grid(row=2, column=0)
Button(root, text='8', width=5, command=lambda:show("8")).grid(row=2, column=1)
Button(root, text='9', width=5, command=lambda:show("9")).grid(row=2, column=2)
Button(root, text='*', width=5, command=lambda:show("*")).grid(row=2, column=3)
Button(root, text='4', width=5, command=lambda:show("4")).grid(row=3, column=0)
Button(root, text='5', width=5, command=lambda:show("5")).grid(row=3, column=1)
Button(root, text='6', width=5, command=lambda:show("6")).grid(row=3, column=2)
Button(root, text='-', width=5, command=lambda:show("-")).grid(row=3, column=3)
Button(root, text='1', width=5, command=lambda:show("1")).grid(row=4, column=0)
Button(root, text='2', width=5, command=lambda:show("2")).grid(row=4, column=1)
Button(root, text='3', width=5, command=lambda:show("3")).grid(row=4, column=2)
Button(root, text='+', width=5, command=lambda:show("+")).grid(row=4, column=3)
Button(root, text='0', width=12, command=lambda:show("0")).grid(row=5, column=0, columnspan= 2)
Button(root, text='.', width=5, command=lambda:show(".")).grid(row=5, column=2)
Button(root, text='=', width=5,bg='yellow',  command=lambda:calculate()).grid(row=5, column=3)

root.mainloop()

2. 实验过程及结果

3. 实验过程中遇到的问题和解决过程

  • 问题1:不知道tkinter 的具体使用方式
  • 问题1解决方案:在自己以前买的书上面找到了tkinter 的介绍以及如何制作一个简易的计算器

其他(感悟、思考等)

要学好代码一定要多动手,要勤于翻资料。

posted @ 2021-12-11 16:41  风雾里  阅读(17)  评论(0编辑  收藏  举报