Kivy 简明教程
Kivy - Calculator App
本章中,我们将学习如何使用 Kivy 库构建计算器应用程序。计算器包含用于每个数字和运算符的按钮。它应该具有一个标题为“=”的按钮,用于计算运算,以及一个用于清除结果的按钮。
In this chapter, we shall learn to build a calculator app using the Kivy library. A calculator consists of buttons for each digit and operators. It should have a button with "=" caption that computes the operation, and a button to clear the result.
我们以以下设计作为起点 −
Let us start with the following design in mind −

以上布局显示了顶部的输入框,随后是按钮的 3 列布局,四种运算符按钮在它们旁边以一列排列。
The above layout shows a input box at the top, followed by a 3-column layout for buttons, besides which the four operator buttons are arranged in one column.
我们将使用一列的顶部网格布局,然后添加右对齐的 TextInput,在它下面我们将放置另一个 2 列网格。此网格的左侧单元包含三列中的数字、= 和 C 按钮。第二列是用于所有算术运算符的另一个一列网格。
We shall use a top grid layout with one column, and add the right-aligned TextInput below which we shall put another 2-column grid. The left cell of this grid houses digit, = and C buttons in three columns. The second column is another one-column grid for all the arithmetic operators.
以下“kv”文件改编此逻辑 −
The following "kv" file adapts this logic −
<calcy>:
GridLayout:
cols:1
TextInput:
id:t1
halign:'right'
size_hint:1,.2
font_size:60
GridLayout:
cols:2
GridLayout:
cols:3
size:root.width, root.height
Button:
id:one
text:'1'
on_press:root.onpress(*args)
Button:
id:two
text:'2'
on_press:root.onpress(*args)
Button:
id:thee
text:'3'
on_press:root.onpress(*args)
Button:
id:four
text:'4'
on_press:root.onpress(*args)
Button:
id:five
text:'5'
on_press:root.onpress(*args)
Button:
id:six
text:'6'
on_press:root.onpress(*args)
Button:
id:seven
text:'7'
on_press:root.onpress(*args)
Button:
id:eight
text:'8'
on_press:root.onpress(*args)
Button:
id:nine
text:'9'
on_press:root.onpress(*args)
Button:
id:zero
text:'0'
on_press:root.onpress(*args)
Button:
id:eq
text:'='
on_press:root.onpress(*args)
Button:
id:clr
text:'C'
on_press:root.onpress(*args)
GridLayout:
cols:1
size_hint:(.25, root.height)
Button:
id:plus
text:'+'
on_press:root.onpress(*args)
Button:
id:minus
text:'-'
on_press:root.onpress(*args)
Button:
id:mult
text:'*'
on_press:root.onpress(*args)
Button:
id:divide
text:'/'
on_press:root.onpress(*args)
请注意,每个按钮在其 on_press 事件上与 onpress() 方法绑定。
Note that each button is bound with onpress() method on its on_press event.
onpress() 方法基本上读取按钮标题(它的文本属性),并决定行动路线。
The onpress() method basically reads the button caption (its text property) and decides the course of action.
-
If the button has a digit as its caption, it is appended to the text property of the TextInput box.
self.ids.t1.text=self.ids.t1.text+instance.text
-
If the button represents any arithmetic operator (+, -, *, /) the number in the textbox at that time is stored in a variable for further operation and the textbox is cleared.
if instance.text in "+-*/":
self.first=int(self.ids.t1.text)
self.ids.t1.text='0'
self.op=instance.text
-
If the button’s text property is '=', the number in the text box at that time is the second operand. Appropriate arithmetic operation is done and the result is displayed in the text box.
if instance.text=='=':
if self.first==0: return
self.second=int(self.ids.t1.text)
if self.op=='+': result=self.first+self.second
if self.op=='-': result=self.first-self.second
if self.op=='*': result=self.first*self.second
if self.op=='/': result=self.first/self.second
self.ids.t1.text=str(result)
self.first=self.second=0
-
Finally, if the button has C as the caption, the text box is set to empty.
if instance.text=='C':
self.ids.t1.text=''
self.first=self.second=0
Example
以上“kv”文件加载在 App 类的 build() 方法中。以下是 complete code −
The above "kv" file is loaded inside the build() method of the App class. Here is the complete code −
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
Window.size = (720,400)
class calcy(GridLayout):
def __init__(self, **kwargs):
super(calcy, self).__init__(**kwargs)
self.first=self.second=0
def onpress(self, instance):
if instance.text=='C':
self.ids.t1.text=''
self.first=self.second=0
elif instance.text in "+-*/":
self.first=int(self.ids.t1.text)
self.ids.t1.text='0'
self.op=instance.text
elif instance.text=='=':
if self.first==0: return
self.second=int(self.ids.t1.text)
if self.op=='+': result=self.first+self.second
if self.op=='-': result=self.first-self.second
if self.op=='*': result=self.first*self.second
if self.op=='/': result=self.first/self.second
self.ids.t1.text=str(result)
self.first=self.second=0
else:
self.ids.t1.text=self.ids.t1.text+instance.text
class calculatorapp(App):
def build(self):
return calcy(cols=3)
calculatorapp().run()