kivy小程序——计算器

kivy小程序——计算器fromkivy appimportApp coreimportwi uix widgetimport propertiesim langimportBu core windowimport size 500 700Builder load string

from kivy.app import App from kivy.core import window from kivy.uix.widget import Widget from kivy.properties import ObjectProperty from kivy.lang import Builder from kivy.core.window import Window Window.size = 500, 700 Builder.load_string(""" <MyLayout>: BoxLayout: orientation: 'vertical' size:root.width,root.height TextInput: id:calc_input text:"0" font_name:"./SIMLI.TTF" halign:"right" font_size: 65 size_hint: 1, .15 GridLayout: cols:4 rows:5 Button: size_hint: .2, .2 font_size:32 text:"%" font_name:"./SIMLI.TTF" Button: size_hint: .2, .2 font_size:32 text:"C" font_name:"./SIMLI.TTF" on_press:root.clear() Button: id:clear size_hint: .2, .2 font_size:32 text:"<<" font_name:"./SIMLI.TTF" on_press:root.remove() Button: size_hint: .2, .2 font_size:32 text:"/" font_name:"./SIMLI.TTF" on_press:root.math_sign("/") Button: size_hint: .2, .2 font_size:32 text:"7" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(7) Button: size_hint: .2, .2 font_size:32 text:"8" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(8) Button: size_hint: .2, .2 font_size:32 text:"9" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(9) Button: size_hint: .2, .2 font_size:32 text:"x" font_name:"./SIMLI.TTF" on_press:root.math_sign("*") Button: size_hint: .2, .2 font_size:32 text:"4" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(4) Button: size_hint: .2, .2 font_size:32 text:"5" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(5) Button: size_hint: .2, .2 font_size:32 text:"6" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(6) Button: size_hint: .2, .2 font_size:32 text:"-" font_name:"./SIMLI.TTF" on_press:root.math_sign("-") Button: size_hint: .2, .2 font_size:32 text:"1" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(1) Button: size_hint: .2, .2 font_size:32 text:"2" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(2) Button: size_hint: .2, .2 font_size:32 text:"3" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(3) Button: size_hint: .2, .2 font_size:32 text:"+" font_name:"./SIMLI.TTF" on_press:root.math_sign("+") Button: size_hint: .2, .2 font_size:32 text:"+/-" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.pos_neg() Button: size_hint: .2, .2 font_size:32 text:"0" font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.button_press(0) Button: size_hint: .2, .2 font_size:32 text:"." font_name:"./SIMLI.TTF" background_color:157/255, 157/255, 157/255,1 on_press:root.dot() Button: size_hint: .2, .2 font_size:32 text:"=" font_name:"./SIMLI.TTF" on_press:root.equals() """ ) class MyLayout(Widget): def clear(self): self.ids.calc_input.text = '0' def button_press(self, button): prior = self.ids.calc_input.text if "Error" in prior: prior = '' if prior == "0": self.ids.calc_input.text = '' self.ids.calc_input.text = f'{button}' else: self.ids.calc_input.text = f'{prior}{button}' def remove(self): prior = self.ids.calc_input.text prior = prior[:-1] self.ids.calc_input.text = prior def dot(self): prior = self.ids.calc_input.text num_list = prior.split("+") if "+" in prior and "." not in num_list[-1]: prior = f'{prior}.' self.ids.calc_input.text = prior elif "." in prior: pass else: prior = f'{prior}.' self.ids.calc_input.text = prior def math_sign(self, sign): prior = self.ids.calc_input.text self.ids.calc_input.text = f'{prior}{sign}' def equals(self): prior = self.ids.calc_input.text try: answer = eval(prior) self.ids.calc_input.text = str(answer) except: self.ids.calc_input.text = 'Error' """if "+" in prior: num_list = prior.split("+") answer = 0 for number in num_list: answer = answer+float(number) self.ids.calc_input.text = str(answer)""" def pos_neg(self): prior = self.ids.calc_input.text if "-" in prior: self.ids.calc_input.text = f'{prior.replace("-", "")}' else: self.ids.calc_input.text = f'-{prior}' class CalculatorApp(App): def build(self): return MyLayout() if __name__ == '__main__': CalculatorApp().run() 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232998.html原文链接:https://javaforall.net

(0)
上一篇 2025年8月25日 下午3:01
下一篇 2025年8月25日 下午3:22


相关推荐

  • win10任务栏图标显示白色方块_win10图标左下角有白色方块

    win10任务栏图标显示白色方块_win10图标左下角有白色方块首先打开一个文件夹点击【查看】菜单,然后勾选【隐藏的项目】;使用【Win】+【R】打开【运行】输入%localappdata%;删除【Iconcache.db】;在任务栏右键的打开【任务管理器】;找到【Windows资源管理器】右键选择【重新启动】。…

    2022年8月31日
    8
  • MATLAB柱状图画法(详细)

    首先先给一张柱状图,请注意,ABC三个对比的数据,是分别对应一个数组的每一列,然后贴代码figure(1)axes2=axes(‘position’,[0.1,0.1,0.86,0.8]);%这个是figure里面图的位置和大小,分别为离下边,左边的距离,还有图的高和宽bar(mse_lowhigh);set(gca,’XTickLabel’,{‘0.5′,’1’,’…

    2022年4月5日
    4.2K
  • tomcat部署war包 接口404 网页能访问「建议收藏」

    tomcat部署war包 接口404 网页能访问「建议收藏」后端新手部署项目被自己坑哭了。访问网页都知道加一个打包的war包名字,访问接口的时候没有加。http://xxx.xxx.xxx/项目名/页面路径页面基地址http://xxx.xxx.xxx/shop/例:http://xxx.xxx.xxx/shop/index.jsp接口基地址:http://xxx.xxx.xxx/shop例:http://xxx.xxx.xxx/shop/goods/getAllGoods…

    2022年6月1日
    34
  • c语言生成随机数数组

    c语言实现获得从0~num-1的随机数组(数组元素不重复,内容是0~num-1),实现的原理是数组乱序,效率高!

    2022年4月7日
    56
  • SpringCloud系列之eurake集群

    SpringCloud系列之eurake集群1 场景还原通俗的讲 eurake 在微服务架构中最大的功能就是将项目划分的微服务收集起来 供服务间相互调用 如果 eurake 服务挂了 可想而知 整个项目的服务调用势必是过眼云烟 今天笔者就 eurake 集群做个详解 仅供参考 2 实现方案 eurake 服务的搭建在上篇笔者已经介绍 链接教程 https blog csdn net zhangxing520 article

    2026年3月18日
    3
  • 如何设置vimrc 配置文件_居中怎么设置

    如何设置vimrc 配置文件_居中怎么设置#HowToVimrc在炮制你自己的.vimrc配置时,这里只有一个原则.不要把任何你不理解的一行放到你的vimrc中.互联网上有以吨计的入门手册,诸如这篇文章.它们之中包含各种令人惊叹的Hack技巧,这可以记你的Vim更好,但为了让你的环境更好完全从别人那儿照搬那些配置,完全是**最糟糕**的做法.真真切切地花一些时间来学习你

    2022年5月3日
    66

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号