• 全部
...

实验二-20222314陈振烨

落影_ 2023-06-07 19:06:28

20222314陈振烨 2022-2023-2 《Python程序设计》实验二报告
课程:《Python程序设计》
班级: 2223
姓名: 陈振烨
学号:20222314
实验教师:王志强
实验日期:2023年3月23日
必修/选修: 公选课

1.实验内容
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。

考核基本语法、判定语句、循环语句、逻辑运算等知识点。

2.代码实现:

  1. #实验二:设计一个计算器(带上交互界面吧不然没意思了)
  2. #20222314陈振烨
  3. from tkinter import *
  4. import tkinter.font
  5. from functools import partial
  6. def get_input(entry, argu):
  7. entry.insert(END, argu)
  8. def backspace(entry):
  9. input_len = len(entry.get())
  10. entry.delete(input_len - 1)
  11. def clear(entry):
  12. entry.delete(0, END)
  13. def calc(entry):
  14. input = entry.get()
  15. output = str(eval(input.strip()))
  16. clear(entry)
  17. entry.insert(END, output)
  18. def cal():
  19. root = Tk()
  20. root.title("Calc")
  21. root.resizable(0, 0)
  22. entry_font = tkinter.font.Font(size=12)
  23. entry = Entry(root, justify="right", font=entry_font)
  24. entry.grid(row=0, column=0, columnspan=4, sticky=N+W+S+E, padx=5, pady=5)
  25. button_font = tkinter.font.Font(size=10, weight=tkinter.font.BOLD)
  26. button_bg = '#ffffff'
  27. button_active_bg = '#0000ff'
  28. myButton = partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground = button_active_bg)
  29. button7 = myButton(text='7', command=lambda: get_input(entry, '7'))
  30. button7.grid(row=1, column=0, pady=5)
  31. button8 = myButton(text='8', command=lambda: get_input(entry, '8'))
  32. button8.grid(row=1, column=1, pady=5)
  33. button9 = myButton(text='9', command=lambda: get_input(entry, '9'))
  34. button9.grid(row=1, column=2, pady=5)
  35. button10 = myButton(text='+', command=lambda: get_input(entry, '+'))
  36. button10.grid(row=1, column=3, pady=5)
  37. button4 = myButton(text='4', command=lambda: get_input(entry, '4'))
  38. button4.grid(row=2, column=0, pady=5)
  39. button5 = myButton(text='5', command=lambda: get_input(entry, '5'))
  40. button5.grid(row=2, column=1, pady=5)
  41. button6 = myButton(text='6', command=lambda: get_input(entry, '6'))
  42. button6.grid(row=2, column=2, pady=5)
  43. button11 = myButton(text='-', command=lambda: get_input(entry, '-'))
  44. button11.grid(row=2, column=3, pady=5)
  45. button1 = myButton(text='1', command=lambda: get_input(entry, '1'))
  46. button1.grid(row=3, column=0, pady=5)
  47. button2 = myButton(text='2', command=lambda: get_input(entry, '2'))
  48. button2.grid(row=3, column=1, pady=5)
  49. button3 = myButton(text='3', command=lambda: get_input(entry, '3'))
  50. button3.grid(row=3, column=2, pady=5)
  51. button12 = myButton(text='*', command=lambda: get_input(entry, '*'))
  52. button12.grid(row=3, column=3, pady=5)
  53. button0 = myButton(text='0', command=lambda: get_input(entry, '0'))
  54. button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)
  55. button13 = myButton(text='.', command=lambda: get_input(entry, '.'))
  56. button13.grid(row=4, column=2, pady=5)
  57. button14 = Button(root, text='/', bg=button_bg, padx=10, pady=3,
  58. command=lambda: get_input(entry, '/'))
  59. button14.grid(row=4, column=3, pady=5)
  60. button15 = Button(root, text='<-', bg=button_bg, padx=10, pady=3,
  61. command=lambda: backspace(entry), activebackground=button_active_bg)
  62. button15.grid(row=5, column=0, pady=5)
  63. button16 = Button(root, text='C', bg=button_bg, padx=10, pady=3,
  64. command=lambda : clear(entry), activebackground=button_active_bg)
  65. button16.grid(row=5, column=1, pady=5)
  66. button17 = Button(root, text='=', bg=button_bg, padx=10, pady=3,
  67. command=lambda: calc(entry), activebackground=button_active_bg)
  68. button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)
  69. root.mainloop()
  70. cal()

运行截图:

 

 实验反思以及感想:交互页面看似复杂,搞懂写入原理后就是“先写一段,复制,粘贴,改参数,粘贴,改参数)

ps:调参数调了好久,还有那个颜色,最后发现还是黑白最好看

...全文
给本帖投票
213 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

144

社区成员

发帖
与我相关
我的任务
社区描述
开展Python教学和技术交流
python 高校 北京·丰台区
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部