# _*_ coding: utf-8 _*_ # @Time 2022/10/18 19:49 # @file dwj_计算器1.0.py # @software PyCharm # Author:Destiney
import tkinter as tk # tkinter是用于构建图形用户界面 (GUI) 的控件集 # 定义两个字体变量 font_20 = ('宋体', 20) font_16 = ('华文楷体', 16) # 布局 root = tk.Tk() # 实例化一个窗体对象 root.title('counting machine') # 设置窗口标题 root.geometry('300x280+100+100') """将root窗口的宽和高设置成300x250 注意x号而非*; 加号后的两个数字,就是调整窗口在电脑屏幕上的位置, 第1个加号是距离屏幕左边的宽,第2个加号是距离屏幕顶部的高。 注意加号后面可以跟负数,这是一种隐藏窗口的方式: """ # window_x = root.winfo_screenwidth() # 获取电脑屏幕窗口宽度 # window_y = root.winfo_screenheight() # 获取电脑屏幕窗口高度 # print(window_x) # print(window_y) # 以上四行目的是为了知道我的窗口尺寸以便设置计算器窗口的位置 得知之后便注释掉了 root.resizable(width=True, height=True) # 将窗口高宽设置为可更改 root.maxsize(1493, 933) # 设置计算器窗口可调整的最大最小尺寸, root.minsize(100, 150) root.attributes('-alpha', 0.9) # 设置透明度为0.9,参数为0~1 root['background'] = '#f0f8ff' # 设置背景颜色为古董白 result_num = tk.StringVar() # 设置一个字符串变量 result_num.set('') # 将初始值设置为0 # lable 用于显示不可编辑的文本和图标 tk.Label(root, textvariable=result_num, font=font_20, height=2, width=20, justify=tk.LEFT, anchor=tk.SE).grid(row=1, column=1, columnspan=4) # textvariable 指定一个变量,GUI组件负责展示该变量值转换得到的字符串, # font 指定字体颜色 justify 针对多行文字对齐,对齐方式有left,center,right # anchor对齐方式s底对齐e右对齐 # grid布局:行列 # 设置第一行的按钮 Button()函数 button_clear = tk.Button(root, text='C', bg='pink', width=5, # 按钮组件 font=font_16, relief=tk.FLAT) # button 用来执行用户的单击操作 # relief 指定组件的3D效果,FLAT为扁平 button_back = tk.Button(root, text='←', bg='pink', width=5, font=font_16, relief=tk.FLAT) button_division = tk.Button(root, text='/', bg='pink', width=5, font=font_16, relief=tk.FLAT) button_multiplication = tk.Button(root, text='x', bg='pink', width=5, font=font_16, relief=tk.FLAT) button_clear.grid(row=2, column=1, padx=4, pady=2) # padx 指定组件内部水平方向两边的空白 pady:指定组件内部垂直方向两边的空白 # row表示单元格的行号,column 单元格的列号 button_back.grid(row=2, column=2, padx=4, pady=2) button_division.grid(row=2, column=3, padx=4, pady=2) button_multiplication.grid(row=2, column=4, padx=4, pady=2) # 设置第二行的按钮 button_seven = tk.Button(root, text='7', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_eight = tk.Button(root, text='8', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_nine = tk.Button(root, text='9', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_subtraction = tk.Button(root, text='-', bg='pink', width=5, font=font_16, relief=tk.FLAT) button_seven.grid(row=3, column=1, padx=4, pady=2) button_eight.grid(row=3, column=2, padx=4, pady=2) button_nine.grid(row=3, column=3, padx=4, pady=2) button_subtraction.grid(row=3, column=4, padx=4, pady=2) # 设置第3行的按钮 button_four = tk.Button(root, text='4', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_five = tk.Button(root, text='5', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_six = tk.Button(root, text='6', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_addition = tk.Button(root, text='+', bg='pink', width=5, font=font_16, relief=tk.FLAT) button_four.grid(row=4, column=1, padx=4, pady=2) button_five.grid(row=4, column=2, padx=4, pady=2) button_six.grid(row=4, column=3, padx=4, pady=2) button_addition.grid(row=4, column=4, padx=4, pady=2) # 设置第4行的按钮 button_one = tk.Button(root, text='1', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_two = tk.Button(root, text='2', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_three = tk.Button(root, text='3', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) button_equal = tk.Button(root, text='=', bg='pink', width=5, height=3, font=font_16, relief=tk.FLAT) button_one.grid(row=5, column=1, padx=4, pady=2) button_two.grid(row=5, column=2, padx=4, pady=2) button_three.grid(row=5, column=3, padx=4, pady=2) button_equal.grid(row=5, column=4, padx=4, pady=2, rowspan=2) # grid布局 rowspan:组件跨多少列表格框,默认1个组件占用1行1列 # 设置第5行的按钮 button_zero = tk.Button(root, text='0', bg='#b1b2b2', width=12, font=font_16, relief=tk.FLAT) button_dot = tk.Button(root, text='.', bg='#b1b2b2', width=5, font=font_16, relief=tk.FLAT) # button_equal2 = tk.Button(root, text='=', bg='#b1b2b2',width=5, # font=font_16, relief=tk.FLAT) button_zero.grid(row=6, column=1, padx=4, pady=2, columnspan=2) # grid布局 columnspan :组件跨多少行表格框,默认1个组件占用1行1列 button_dot.grid(row=6, column=2 and 3, padx=4, pady=2) # button_equal2.grid(row=6,column=4,padx=4,pady=2) # 运算逻辑 def click_button(x): # 连接字符串 print('x:\t') result_num.set((result_num.get() + x)) def calculation(): # 字符串转为算术运算 opt_str = result_num.get() result = eval(opt_str) result_num.set(str(result)) # 起初result_num设置的是字符串因而在screen上显示的时候需要str()函数 def back(): # 删除 a = result_num.get() a = a [:-1] # 删除结尾 result_num.set(a) def clear(): # 清空 a = result_num.get() a = '' # 删除结尾 result_num.set(a) button_one.config(command=lambda: click_button('1')) button_two.config(command=lambda: click_button('2')) button_three.config(command=lambda: click_button('3')) button_four.config(command=lambda: click_button('4')) button_five.config(command=lambda: click_button('5')) button_six.config(command=lambda: click_button('6')) button_seven.config(command=lambda: click_button('7')) button_eight.config(command=lambda: click_button('8')) button_nine.config(command=lambda: click_button('9')) button_zero.config(command=lambda: click_button('0')) button_dot.config(command=lambda: click_button('.')) button_division.config(command=lambda: click_button('/')) button_multiplication.config(command=lambda: click_button('*')) #因为后面eval()的关系而用*而不是x # eval()函数 将字符串str当成有效的表达式来求值并返回计算结果。 button_addition.config(command=lambda: click_button('+')) button_subtraction.config(command=lambda: click_button('-')) button_equal.config(command=lambda: click_button('=')) # 计算 button_equal.config(command=calculation) # 使用command=lambda: 的形式传参 button_back.config(command=back) # 使用command=lambda: 的形式传参 # config用来配置tkinter中控件和字体的样式,比如颜色、大小等。 button_clear.config(command=clear) # 使用command=lambda: 的形式传参 root.mainloop() # 进入事件循环
"""" 标注:该代码来自于b站up主 python学习者 的教学视频进行了二次理解和完善 视频链接附上:【Python&tkinter案例教学:制作网络调试工具-哔哩哔哩】 https://b23.tv/LfTGU51"""