首页 编程设计Python编程正文

[Kivy教程]简易计算器程序1

云水 Python编程 2023-09-09 22:26:52 36 0 kivy

在本文中, 我们将学习如何使用Kivy创建一个简单的计算器。

先建立一个main.py文件

# Program to create a calculator
# Program to Show how to create a switch
# import kivy module
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require('1.9.0')
# for making multiple bttons to arranging
# them we are using this
from kivy.uix.gridlayout import GridLayout
# for the size of window
from kivy.config import Config
# Setting size to resizable
Config.set('graphics', 'resizable', 1)
## Config.set('graphics', 'width', '400')
## Config.set('graphics', 'height', '400')
# Creating Layout class
class CalcGridLayout(GridLayout):
    # Function called when equals is pressed
    def calculate(self, calculation):
        if calculation:
            try:
                # Solve formula and display it in entry
                # which is pointed at by display
                self.display.text = str(eval(calculation))
            except Exception:
                self.display.text = "Error"
 # Creating App class
class CalculatorApp(App):
    def build(self):
        return CalcGridLayout()
# creating object and running it
calcApp = CalculatorApp()
calcApp.run()

在同目录下新建一个CalculatorApp.kv文件(文件名要与class名一致),程序如下:

# Custom button
<CustButton@Button>:
    font_size: 32
# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
    id: calculator
    display: entry
    rows: 6
    padding: 10
    spacing: 10
    # Where input is displayed
    BoxLayout:
        TextInput:
            id: entry
            font_size: 32
            multiline: False
    # When buttons are pressed update the entry
    BoxLayout:
        spacing: 10
        CustButton:
            text: "7"
            on_press: entry.text += self.text
        CustButton:
            text: "8"
            on_press: entry.text += self.text
        CustButton:
            text: "9"
            on_press: entry.text += self.text
        CustButton:
            text: "+"
            on_press: entry.text += self.text
    BoxLayout:
        spacing: 10
        CustButton:
            text: "4"
            on_press: entry.text += self.text
        CustButton:
            text: "5"
            on_press: entry.text += self.text
        CustButton:
            text: "6"
            on_press: entry.text += self.text
        CustButton:
            text: "-"
            on_press: entry.text += self.text
    BoxLayout:
        spacing: 10
        CustButton:
            text: "1"
            on_press: entry.text += self.text
        CustButton:
            text: "2"
            on_press: entry.text += self.text
        CustButton:
            text: "3"
            on_press: entry.text += self.text
        CustButton:
            text: "*"
            on_press: entry.text += self.text
    # When equals is pressed pass text in the entry
    # to the calculate function
    BoxLayout:
        spacing: 10
        CustButton:
            text: "AC"
            on_press: entry.text = ""
        CustButton:
            text: "0"
            on_press: entry.text += self.text
        CustButton:
            text: "="
            on_press: calculator.calculate(entry.text)
        CustButton:
            text: "/"
            on_press: entry.text += self.text
    BoxLayout:
        CustButton:
            font_size: 20
            text: "Scientific calculator"
            on_press: entry.text = ""

输出如下:

http://47.105.145.143/nat123CacheFolder/61706F642E6363/8687387e9dd44ab8b258c1bed73ae569C62FCC30D032CF32CC20CA3ACC31C531CC20BE4D_220b2151521d007e4f2d39b655ab1b01/index.php/post/image.png


版权声明

1.本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行测试。
2.本站资源仅供学习和交流使用,版权归资源原作者所有,请在下载后24小时之内自觉删除。
3.若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。
4.若内容涉及侵权或违法信息,请联系本站管理员进行下架处理,邮箱ganice520@163.com(本站不支持其他投诉反馈渠道,谢谢合作)

本文链接:http://apod.cc/index.php/post/655.html

发表评论

评论列表(0人评论 , 36人围观)
☹还没有评论,来说两句吧...