20214201 实验二 《Python程序设计》实验报告
2022/4/6 1:20:09
本文主要是介绍20214201 实验二 《Python程序设计》实验报告,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
学号 2021-2022-2 《Python程序设计》实验二报告
课程:《Python程序设计》
班级: 2142
姓名: 刘嘉铭
学号: 20214201
实验教师:王志强
实验日期:2022年3月31日
必修/选修: 公选课
1.实验内容
- 设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
- 考核基本语法、判定语句、循环语句、逻辑运算等知识点
2. 实验过程及结果
在本次试验基础上通过自学tkinter实现简单计算器的程序
1.调用tkinter库
import tkinter as tk
2.配置初始窗口设置
root = tk.Tk()
创建tk界面
root.geometry('250x380')
设置界面分辨率
root.title('Calculate')
设置界面标题
3.配置计算器显示栏
frame_show = tk.Frame(width=300, height=150 ,bg='#dddddd')
命名并创建Frame栏并进行初始设置
a = tk.StringVar()
初始化字符串a为显示在frame_show上的内容
a.set('0')
初始化字符串a的值
show_label = tk.Label(frame_show,textvariable = a, bg = 'white', width=12, height=1, font=("Times New Roman", 20))
命名并创建label,其在Frame_show中,字符为a,背景颜色为白色,宽12高1,字体为新罗马且字号20
show_label.pack(padx = 10, pady = 10)
pack这个label
frame_show.pack()
pack整个Frame_show
4.初始化
calcul = []
创建空列表用于最终计算
ispress = False
判断是否按下按钮,初始化为空
5.自定义函数改变数值
点击查看代码
def changenumber(num): # 改变数值 global ispress # 声明全局变量,下同 if ispress == False: if a.get() == '0'or a.get() == '00': a.set('') a.set(num) else: a.set(a.get()+num) else: a.set(num) ispress = False
6.自定义函数选择运算符
点击查看代码
def operation(sign): # 选择运算符 global ispress global calcul ispress = True calcul.append(a.get()) if sign == '+': calcul.append('+') elif sign == '-': calcul.append('-') elif sign == '*': calcul.append('*') elif sign == '/': calcul.append('/') elif sign == '%': calcul.append('%') print(calcul)
7.自定义函数进行计算
点击查看代码
def equal(): # 进行计算 global calcul calcul.append(a.get()) print(calcul) calculstr = ''.join(calcul) if calculstr[-1] in'+-*/%': calculstr = calculstr[0:-1] result = eval(calculstr) a.set(result) calcul.clear()
8.自定义函数删除字符串
点击查看代码
def delete(): # 删除 if a.get() == '' or a.get() =='0' or a.get() == '00': a.set('0') return else: num = len(a.get()) if num > 1: strnum = a.get() strnum = strnum[0:-1] a.set(strnum) else: a.set('0')
9.自定义函数清空
点击查看代码
def clearboard(): # 清空 global calcul calcul = [] a.set('0') ispress = False
10.自定义函数正负颠倒
点击查看代码
def signa(): # 正负颠倒 strnum = a.get() if strnum[0] == '-': a.set(strnum[1:]) elif strnum[0] != '-' and strnum != '0': a.set('-'+strnum)
11.自定义函数配置按钮
点击查看代码
# 配置按键符 frame_board = tk.Frame(width = 400, height = 350, bg = '#cccccc') button_del = tk.Button(frame_board, text = '<-', width = 5, height = 1, command = delete).grid(row = 0, column = 0) button_signa = tk.Button(frame_board,text = '±', width = 5, height = 1, command = signa).grid(row = 0, column = 2) button_clearb = tk.Button(frame_board,text = 'CE', width = 5, height = 1, command = clearboard).grid(row = 0, column = 1) button_mo = tk.Button(frame_board, text = '%', width = 5, height = 1, command = lambda:operation('%')).grid(row = 0, column = 3) button_1 = tk.Button(frame_board, text = '1', width = 5, height = 2, command = lambda:changenumber('1')).grid(row = 1, column = 0) button_2 = tk.Button(frame_board, text = '2', width = 5, height = 2, command = lambda:changenumber('2')).grid(row = 1, column = 1) button_3 = tk.Button(frame_board, text = '3', width = 5, height = 2, command = lambda:changenumber('3')).grid(row = 1, column = 2) button_plus = tk.Button(frame_board, text = '+', width = 5, height = 2, command = lambda:operation('+')).grid(row = 1, column = 3) button_4 = tk.Button(frame_board, text = '4', width = 5, height = 2, command = lambda:changenumber('4')).grid(row = 2, column = 0) button_5 = tk.Button(frame_board, text = '5', width = 5, height = 2, command = lambda:changenumber('5')).grid(row = 2, column = 1) button_6 = tk.Button(frame_board, text = '6', width = 5, height = 2, command = lambda:changenumber('6')).grid(row = 2, column = 2) button_minus = tk.Button(frame_board, text = '-', width = 5, height = 2, command = lambda:operation('-')).grid(row = 2, column = 3) button_7 = tk.Button(frame_board, text = '7', width = 5, height = 2, command = lambda:changenumber('7')).grid(row = 3, column = 0) button_8 = tk.Button(frame_board, text = '8', width = 5, height = 2, command = lambda:changenumber('8')).grid(row = 3, column = 1) button_9 = tk.Button(frame_board, text = '9', width = 5, height = 2, command = lambda:changenumber('9')).grid(row = 3, column = 2) button_mult = tk.Button(frame_board, text = '*', width = 5, height = 2, command = lambda:operation('*')).grid(row = 3, column = 3) button_00 = tk.Button(frame_board, text = '00', width = 5, height = 2, command = lambda:changenumber('00')).grid(row = 4, column = 0) button_0 = tk.Button(frame_board, text = '0', width = 5, height = 2, command = lambda:changenumber('0')).grid(row = 4, column = 1) button_dot = tk.Button(frame_board, text = '.', width = 5, height = 2, command = lambda:changenumber('.')).grid(row = 4, column = 2) button_equal = tk.Button(frame_board, text = '=', width = 5, height = 2, command = lambda:equal()).grid(row = 4, column = 3) frame_board.pack(padx = 10,pady = 10)
12.使该窗口永久显示
root.mainloop()
3. 实验过程中遇到的问题和解决过程
- 问题1:无法进行运算
- 问题1解决方案:需先初始化一个列表,后将数据赋值其中
- 问题2:自定义函数无法改变数值
- 问题2解决方案:缩进问题,判断的逻辑不对。
- ...
其他(感悟、思考等)
Python的内置函数和库功能非常强大,通过调用就能利用一行代码实现较为复杂的算法过程。利用lambda函数对代码进行简化也能使其看起来更加富有可读性。
计算器效果过于简陋和单一,无历史操作的保留等常规计算器拥有的功能,未来还需进一步完善并debug。
实验二码云Gitee地址跳转
参考资料
Tkinter教程
这篇关于20214201 实验二 《Python程序设计》实验报告的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门