您当前的位置: 首页 >  Python

哆啦A梦_i

暂无认证

  • 2浏览

    0关注

    629博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python:使用Kivy制作计算器?

哆啦A梦_i 发布时间:2021-04-13 11:24:23 ,浏览量:2

Kivy是Python中与平台无关的GUI工具。

由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。

👉🏽Kivy教程–通过示例学习Kivy。

【相关知识点:python:Kivy基础】

【相关文章:Python:计算器】

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

先决条件:

  • 数学基础知识
  • Python
  • Kivy
  • 小部件和代码对Kivy的理解
Basic approach to make A calculator:

import kivy
import kivyApp
import Gridlayout
import config(to configure/adjust the window size)
Set minimum version(optional)
Create Layout class :
      define Calculator function in it
         : In this i am using try-catch because if any arithmetic 
            exception occur it will through the error  

create App class
create .kv file (name same as the app class):
        create buttons
        Add the style to the buttons
        Add fuctionalties of the button 
return Layout/widget/Class(according to requirement)
Run an instance of the class

该方法的实施:

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()
计算器
# Custom button
:
     font_size: 32
   
# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
:
     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 = ""

输出如下:

如何使用基维制作计算器?蟒蛇1

关注
打赏
1556978864
查看更多评论
立即登录/注册

微信扫码登录

0.1069s