好吧,最近学python经常和C语言弄混,习惯太不一样了
#coding:utf-8
import tkinter #引入界面设计库
import sys #引入系统交互操作
import os #方便路径访问
import re #引入正则化
#利用此函数实现资源路径的定位
def get_resources_path(relative_path):
if getattr (sys, "frozen", False):
base_path = sys._MEIPASS #获取临时资源
else:
base_path = os.path.abspath(".") #获取当前路径
return os.path.join(base_path, relative_path) #获取绝对路径
LOGO_PATH = get_resources_path(os.path.join("resources" ,"cc1.ico"))
IMAGE_PATH = get_resources_path(os.path.join("resources" ,"cc2.png"))
class MainForm:
def __init__(self):
self. root = tkinter.Tk() #新建一个窗体
self.root.title ( "好用的计算器") #标题
self.root.geometry("231x280") #初始窗口大小,为小写x
self.root.maxsize (1000,1000) #最大化后窗口大小
self.root.iconbitmap(LOGO_PATH) #图标,bmp格式可直接修改后缀
self.root["background"] = "LightPink" #界面颜色设置,其他颜色可以百度
self.input_frame()#显示输入组
self.button_frame() #显示按钮组
self.root.mainloop() #显示整个界面
#定义输入组件
def input_frame(self):
self.input_frame = tkinter.Frame(self.root, width = 20) #创建一个内部容器
self.content = tkinter.StringVar() #标签的显示
#单行输入使用entry组件
self.entry = tkinter.Entry(self.input_frame,
width = 14,
font = ("微软雅黑", 20),
textvariable = self.content)
#entry组件显示
self.entry.pack(fill = "x",expand ="1")
self.clean = False
self.input_frame.pack (side = "top")
def button_frame(self):
#创建一个button组
self.button_frame = tkinter.Frame(self.root, width = 50)
#申明一个列表存button组件
self.button_list = [[],[],[],[]]
self.button_list[0].append(tkinter.Button( self.button_frame,text = "1", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[0].append(tkinter.Button( self.button_frame,text = "2", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[0].append(tkinter.Button( self.button_frame,text = "3", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[0].append(tkinter.Button( self.button_frame,text = "+", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[1].append(tkinter.Button( self.button_frame,text = "4", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[1].append(tkinter.Button( self.button_frame,text = "5", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[1].append(tkinter.Button( self.button_frame,text = "6", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[1].append(tkinter.Button( self.button_frame,text = "-", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[2].append(tkinter.Button( self.button_frame,text = "7", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[2].append(tkinter.Button( self.button_frame,text = "8", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[2].append(tkinter.Button( self.button_frame,text = "9", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[2].append(tkinter.Button( self.button_frame,text = "*", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[3].append(tkinter.Button( self.button_frame,text = "0", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[3].append(tkinter.Button( self.button_frame,text = ".", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[3].append(tkinter.Button( self.button_frame,text = "=", fg = "black", width =3, font = ("微软雅黑",20)))
self.button_list[3].append(tkinter.Button( self.button_frame,text = "/", fg = "black", width =3, font = ("微软雅黑",20)))
#规划button的位置
self.row = 0
for group in self.button_list:
self.column = 0
for button in group:
button.bind ("<Button-1>", lambda event: self.button_handle(event)) #绑定事件
button.grid(row = self.row, column = self.column) #放置每个按钮的位置
self.column += 1
self.row += 1
self.button_frame.pack (side ="bottom") #放置整个frame
def button_handle(self, event):
#获取输入的字符
oper = event.widget ["text"]
#清除操作
if self.clean :
self.content.set("")
self.clean = False
#显示输入的字符
if oper != "=":
self.entry.insert ("end", oper)
else:
#计算表达式
result =0
exp = self.entry.get()
pattern = r"\+|\-|\*|\/"
nums = re.split(pattern, exp)
flag = re.findall (pattern, exp)[0]
if flag == "+":
result = float(nums[0] )+ float(nums[1])
if flag == "-":
result = float(nums[0] )- float(nums[1])
if flag == "*":
result = float(nums[0]) * float(nums[1])
if flag == "/":
result = float(nums[0]) / float(nums[1])
#显示计算结果
self.entry.insert("end", "=%s" %result)
self.clean = True
def main():
MainForm()
if __name__ == "__main__":
main()
太难了,我快要忘记C怎么写的了,啊啊啊啊啊啊,可不可以统一一下啊,我要精神分裂了
来源:CSDN
作者:吃不胖的粥
链接:https://blog.csdn.net/weixin_41485242/article/details/104718864