Python+tkinter实现一个带界面的计算机软件

前置:python 3.5   ;

          pycharm

点击下载源码及文件

1.运行效果图

Python+tkinter实现一个带界面的计算机软件

2.源码


  1. from tkinter import *
  2. root = Tk()
  3. # 计算机窗口大小 (宽x高)
  4. root.geometry("250x380")
  5. # 设置计算机title
  6. root.title("计算器")
  7. # Frame就是在屏幕上的一块矩形区域 多用来作为容器使用
  8. frame_show = Frame(width=250, height=150, bg='#ddd')
  9. # 添加到主窗体
  10. frame_show.pack()
  11. # 主窗体
  12. # 实例化一个产生变量的类
  13. v = StringVar()
  14. # 初始化赋值'0'
  15. v.set('0')
  16. # Lable(用于存放父组件,属性参数 )
  17. # anchor 文本相对于标签中心的位置 默认是center N S W E
  18. show_label = Label(frame_show, textvariable=v, bg='white', width='30', height='1', anchor='e', font=("黑体", 20, "bold"))
  19. # 添加到主窗体
  20. show_label.pack(padx=10, pady=10)
  21. frame_bord = Frame(width=250, height=350, bg='#ccc')
  22. frame_bord.pack(padx=10, pady=10)
  23. calc = []
  24. isoperate = False
  25. def change(num):
  26. global isoperate
  27. if isoperate == False:
  28. if v.get() == "0" and num == '.':
  29. v.set(length(v.get() + num))
  30. elif v.get() == "0":
  31. v.set(length(num))
  32. else:
  33. if num == ".":
  34. if v.get().count(".")
  35. v.set(v.get() + num)
  36. else:
  37. v.set(v.get() + num)
  38. else:
  39. v.set(length(num))
  40. isoperate = False
  41. # 检验字符串的长度的函数
  42. def length(str):
  43. if len(str) > 12:
  44. return str[0:12]
  45. else:
  46. return str
  47. # 清空函数
  48. def clear():
  49. global calc
  50. calc = []
  51. # 屏幕窗口恢复到0
  52. v.set("0")
  53. # 操作 + - * /
  54. def operate(sign):
  55. global calc
  56. global isoperate
  57. isoperate = True
  58. calc.append(v.get())
  59. if sign == "+":
  60. calc.append(sign)
  61. elif sign == "-":
  62. calc.append(sign)
  63. elif sign == "*":
  64. calc.append(sign)
  65. elif sign == "/":
  66. calc.append(sign)
  67. # 运算
  68. global calcstr
  69. def equal():
  70. global calc
  71. # 获取当前界面的值
  72. calc.append(v.get())
  73. print(calc)
  74. # 列表变字符串 join 把列表用什么拼接成字符串
  75. calcstr = "".join(calc)
  76. print(calcstr)
  77. print(type(calcstr))
  78. # 运算操作 eval()把str当成有效的表达式进行计算
  79. result = eval(calcstr)
  80. if len(str(result)) > 12:
  81. result = str(result)
  82. result = result[0:12]
  83. v.set(result)
  84. else:
  85. v.set(result)
  86. print(result)
  87. # 定义退格函数
  88. def delete():
  89. # 获取v.get()长度
  90. num = len(v.get())
  91. # 如果长度>1 怎么办
  92. if num > 1:
  93. strnum = v.get()
  94. strnum = strnum[0:num - 1]
  95. v.set(strnum)
  96. # 小于等于1的时候
  97. else:
  98. v.set("0")
  99. #正负操作
  100. def fan():
  101. strnum = v.get()
  102. if strnum[0] == '-':
  103. v.set(strnum[1:])
  104. elif strnum[0] != '-' and strnum != '0' :
  105. v.set('-'+strnum)
  106. # Button(父组件,属性参数)
  107. button_del = Button(frame_bord, text='←', width='5', height='1', command=lambda: delete()).grid(row='1', column='0')
  108. button_del = Button(frame_bord, text='CE', width='5', height='1', command=lambda: clear()).grid(row='1', column='1')
  109. button_del = Button(frame_bord, text='C', width='5', height='1', command=lambda: clear()).grid(row='1', column='2')
  110. button_del = Button(frame_bord, text='±', width='5', height='1', command=lambda: fan()).grid(row='1', column='3')
  111. button_del = Button(frame_bord, text='7', width='5', height='1', command=lambda: change("7")).grid(row='2', column='0')
  112. button_del = Button(frame_bord, text='8', width='5', height='1', command=lambda: change("8")).grid(row='2', column='1')
  113. button_del = Button(frame_bord, text='9', width='5', height='1', command=lambda: change("9")).grid(row='2', column='2')
  114. button_del = Button(frame_bord, text='/', width='5', height='1', command=lambda: operate("/")).grid(row='2', column='3')
  115. button_del = Button(frame_bord, text='4', width='5', height='1', command=lambda: change("4")).grid(row='3', column='0')
  116. button_del = Button(frame_bord, text='5', width='5', height='1', command=lambda: change("5")).grid(row='3', column='1')
  117. button_del = Button(frame_bord, text='6', width='5', height='1', command=lambda: change("6")).grid(row='3', column='2')
  118. button_del = Button(frame_bord, text='-', width='5', height='1', command=lambda: operate("-")).grid(row='3', column='3')
  119. button_del = Button(frame_bord, text='1', width='5', height='1', command=lambda: change("1")).grid(row='4', column='0')
  120. button_del = Button(frame_bord, text='2', width='5', height='1', command=lambda: change("2")).grid(row='4', column='1')
  121. button_del = Button(frame_bord, text='3', width='5', height='1', command=lambda: change("3")).grid(row='4', column='2')
  122. button_del = Button(frame_bord, text='*', width='5', height='1', command=lambda: operate("*")).grid(row='4', column='3')
  123. button_del = Button(frame_bord, text='0', width='5', height='1', command=lambda: change("0")).grid(row='5', column='0')
  124. button_del = Button(frame_bord, text='.', width='5', height='1', command=lambda: change(".")).grid(row='5', column='1')
  125. button_del = Button(frame_bord, text='+', width='5', height='1', command=lambda: operate("+")).grid(row='5', column='2')
  126. button_del = Button(frame_bord, text='=', width='5', height='1', command=lambda: equal()).grid(row='5', column='3')
  127. root.mainl

    声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2018年4月15日
下一篇 2018年4月15日

相关推荐