当前位置: 首页 > news >正文

Tkinter Frame布局笔记--做一个简易的计算器

#encoding=utf-8
import tkinter
import re
import tkinter.messagebox
import tkinter.simpledialog
import sys
import os
def get_resources_path(relative_path):if getattr(sys,'frozen', False):base_path=sys._MEIPASS#获取临时文件else:base_path=os.path.dirname(".")return os.path.join(base_path,relative_path)
Logopath=get_resources_path(os.path.join("resources",'heart.ico'))
startpath=get_resources_path(os.path.join("resources",'3.png'))
circlepath=get_resources_path(os.path.join("resources",'4.png'))
duopath=get_resources_path(os.path.join("resources",'5.png'))
class MainForm:#定义窗体类def __init__(self):self.root=tkinter.Tk()#创建一个窗体self.root.title("Katetesting")#设置标题self.root.iconbitmap(Logopath)self.root.geometry("231x280")self.root.maxsize(1000,1000)self.root["background"]="#312432"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)self.entry.pack(fill="x",expand=1)self.clean=False#清除标记,每一次计算完成之后清除self.input_frame.pack(side="top")def button_frame(self):self.button_frame=tkinter.Frame(self.root,width=8)self.button_list=[[],[],[],[]]#定义了一个4组组件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')))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+=1self.row+=1self.button_frame.pack(side="bottom")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)elif oper =="=":#执行运算result=0#保存程序的计算结果exp=self.entry.get()print(exp)pattern=r"\+|\-|\*|\\"nums=re.split(pattern,exp)print(nums)flag=re.findall(pattern,exp)[0]if flag=="+":result=float(nums[0])+float(nums[1])elif flag=="-":result = float(nums[0]) - float(nums[1])elif flag=="*":result = float(nums[0]) * float(nums[1])elif flag==r"/":result = float(nums[0]) / float(nums[1])self.entry.insert("end","=%s"%result)self.clean=Truedef main():MainForm()
if __name__=="__main__":main()

http://www.lryc.cn/news/464338.html

相关文章:

  • 算法专题八: 链表
  • MySQL中关于NULL值的六大坑!你被坑过吗?
  • 学生学习动机测试:激发潜能,引领未来
  • 基于SSM党务政务服务热线管理系统的设计
  • OSI参考模型详解:初学者指南与实践案例
  • S7-200 SMART 与 S7-1200 之间 TCP 通信— S7-200 SMART 作为服务器
  • Java @RequestPart注解:同时实现文件上传与JSON对象传参
  • 深度学习基础知识-02 数据预处理
  • 【CTF刷题9】2024.10.19
  • WPF中的Setter
  • RabbitMQ下载与配置
  • 【数据结构与算法】力扣 54. 螺旋矩阵
  • 速通不了的人工智能
  • 微信新功能上线,找工作也能“附近”搞定
  • CANoe与C#联合仿真方案
  • 公交信息在线查询系统|基于java和小程序的公交信息在线查询系统小程序设计与实现(源码+数据库+文档)
  • [LeetCode] 1162. 地图分析
  • CentOS 上安装 MySQL(附卸载教程)
  • 如何在Matlab界面中添加日期选择器?
  • 保险系统的部分模式01
  • 用你的手机/电脑运行文生图方案
  • L1正则化详解
  • C语言在数据库开发中的应用及其代码实践
  • java maven
  • Java爬虫:获取直播带货数据的实战指南
  • python 列表、元组、字典易误区
  • wireshark或tshark提取tcpdump捕获的数据包(附python脚本自动解析文件后缀)
  • 了解EasyNVR及EasyNVS,EasyNVR连接EasyNVS显示授权超时如何解决?什么原因?
  • 【AUTOSAR标准文档】服务类型介绍
  • Axure垂直菜单展开与折叠