猕猴桃提取密码
A simple calculator
一个简单的计算器
为什么要使用计算器?(Why calculator?)
If you have started learning kivy and your all basic concepts related to kivy are cleared, then you will be excited to build your very first app and looking for a basic idea to build an app.
如果您已经开始学习kivy,并且所有与kivy相关的基本概念都已被清除,那么您将很高兴构建自己的第一个应用程序并寻找构建应用程序的基本想法。
My suggestion to build a basic app like a calculator because it is easy to build a calculator for a person who’s new to the framework and as well as it will help you understand various new concepts related to the kivy framework.
我建议构建一个像计算器这样的基本应用程序,因为它很容易为刚接触该框架的人构建一个计算器,并且它将帮助您理解与kivy框架相关的各种新概念。
And one of the main reasons i chose calculator is that when we fail to do our first task successfully we tend to drop learning the language. So my advice is to start with an easy task that can be completed and that will motivate you to learn this framework further, as small achievement gives you great motivation to strive for more
我选择计算器的主要原因之一是,当我们未能成功完成第一个任务时,我们倾向于放弃学习该语言。 因此,我的建议是从一个容易完成的任务开始,这将激发您进一步学习该框架的能力,因为小的成就会给您很大的动力去争取更多
建立有吸引力的界面(Build an attractive Interface)
Before building your application interface, you must keep in mind that your application interface should look attractive and also be simple enough which can be operated by a non-technical user.
在构建应用程序界面之前,必须记住,您的应用程序界面应该看起来很吸引人,并且也足够简单,可以由非技术用户进行操作。
开始建设 (Start building)
We all are very much familiar with a calculator and must know that a calculator interface consist of numbers of buttons like count(0–9), operators(+,_,*,/), etc.
我们都非常熟悉计算器,并且必须知道计算器界面由许多按钮组成,例如count(0–9),运算符(+,_,*,/)等。
问题 (Problem)
Now, most of the newbies will start creating each button separately by writing each button object but this will increase the lines of code and will make your program complex.
现在,大多数新手将通过编写每个按钮对象来分别创建每个按钮,但这将增加代码行,并使程序复杂。
如何解决这个问题呢? (How to solve this problem?)
If you have experience of any programming language you must be aware of the loop concept and we are going to apply that concept here.By using for loop we can create multiple buttons by writing a few lines of code and can set different parameters for each button separately.
如果您有任何编程语言的经验,您必须了解循环概念,我们将在此处应用该概念。通过使用for循环,我们可以通过编写几行代码来创建多个按钮,并且可以为每个按钮设置不同的参数分别。
let me show you how it will work:
让我向您展示它如何工作:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.window import Window#window size
w,h=Window.size#class for first screenclass Display(FloatLayout):
string=""
def __init__(self,*args):
super().__init__(*args)
self.text_field=TextInput(font_size=60,hint_text="Enter the values here",text="",size_hint=(0.8,0.05),pos_hint={'x':0.1,'y':0.85})
self.add_widget(self.text_field)
btn_list=['C','(',')','/','7','8','9','*','4','5','6','+','1','2','3','-','0','','.','=']
Y=0.5
X=0.025
n=1
for i in range(20):
if n==18:
pass
else:
self.btn=Button(text=btn_list[i],size_hint=(0.2,0.1),pos_hint={'x':X,'y':Y},)
self.add_widget(self.btn)
self.btn.background_color = (1, 1, 1, 1)
self.btn.color=(0,0,0,1)
self.btn.font_size=w*0.05
self.btn.bind(on_press=self.calculation)
if n%4==0:
Y-=0.125
X=0.025
self.btn.background_color=(1,0.49,0.31,1)
self.btn.color=(1,1,1,1)
else:
X+=0.25
if n==17:
self.btn.size_hint_x=0.45
if n==20:
self.btn.background_color=(0,1,0,1)
n+=1
def calculation(self,instance):
pass
#Main class
class calci(App):
def build(self):
return Display()
calci().run()
Output
输出量
Explanation
说明
In the above code, we have created one text field where all the calculations part will be displayed.
在上面的代码中,我们创建了一个文本字段,其中将显示所有计算部分。
And then we’ve created a list carrying the button name we want to add in the interface and then started a for loop in which button is being created one by one but we need to put text and position as variable because every button will have different text as well as different position. As you can see we are putting text from the above list that we have created and position is also varying from button to button. We have also given different colours. Like this different parameter is passed to different buttons.
然后,我们创建了一个列表,其中包含要在界面中添加的按钮名称,然后启动for循环,在该循环中逐个创建按钮,但是我们需要将文本和位置作为变量,因为每个按钮都有不同的位置文字以及其他位置。 如您所见,我们从上面创建的列表中放入文本,并且位置在每个按钮之间也有所不同。 我们还提供了不同的颜色。 像这样,将不同的参数传递给不同的按钮。
Now, these all buttons should perform a function but if you have noticed that we have bound all the buttons with the same function. So how will the function differentiate which button is clicked by the user?
现在,所有这些按钮都应该执行功能,但是如果您注意到我们已经将所有按钮绑定为相同的功能。 那么该功能如何区分用户单击哪个按钮?
For that, you will have to understand the concept of instance.
为此,您将必须了解实例的概念。
什么是实例? (What is an instance?)
When we bind a button with a function then it will send all its information like its text, size, position, etc. on doing that binded action.
当我们将按钮与函数绑定时,它将在执行绑定操作时发送其所有信息,如文本,大小,位置等。
And all of the information will be store as an instance in that function, so you have to mention a parameter in that function as instance or by any other name.
并且所有信息将作为实例存储在该函数中,因此您必须在该函数中以实例或任何其他名称提及一个参数。
Now, we will prepare that function so that our application will work according to the demand of the user.
现在,我们将准备该功能,以便我们的应用程序可以根据用户的需求运行。
def calculation(self,instance):
a=instance.text
if a=="=":
try:
self.string=self.text_field.text
self.text_field.text=str(eval(self.string))
self.string=self.text_field.text
except SyntaxError:
self.text_field.text="syntax error!"
self.string=""
except TypeError:
self.text_field.text="pass wrong input!"
self.string=""
except NameError:
self.text_field.text="syntax error!"
self.string=""
except:
self.text_field.text="syntax error!"
self.string=""
elif a=="C":
self.string=self.string[0:-1]
self.text_field.text=self.string
else:
self.string+=a
self.text_field.text=self.string
Replace this function with the function of the above code and your application will start working.
用上面的代码替换此函数,您的应用程序将开始运行。
Output
输出量
Explanation
说明
So what’s going on in this function is, initially, the function is identifying which button is clicked by the user by checking it’s text which is present in instance parameter.And then if the equal button is clicked, the function is picking the text field input and solving it using an inbuilt function in python which is eval().
因此,此函数的功能是,首先,该函数通过检查实例参数中存在的文本来识别用户单击了哪个按钮,然后如果单击了相等按钮,则该函数将选择文本字段输入并使用python中的内置函数eval()来解决它。
eval() function is used to solve the mathematical expression by BODMAS rule.If it will face any kind of exception then it will be handled by exception handling and will show output according to the type of error.
eval()函数用于通过BODMAS规则求解数学表达式,如果遇到任何异常,将通过异常处理进行处理,并根据错误的类型显示输出。
If the Clear button is clicked, it will decrease the text field value by one letter.Else every button will do the same thing that adds their value in the text field. Similarly like this you can allot functioning to every button.
如果单击清除按钮,则会将文本字段的值减少一个字母,否则每个按钮都会执行相同的操作,将其值添加到文本字段中。 同样,您可以为每个按钮分配功能。
That’s how this function works and calculates the values. In this way you can build your own calculator and add more functions to it like trigonometry, power, root etc and also you can make it more attractive.
这就是该函数的工作方式并计算值。 这样,您可以构建自己的计算器并为其添加更多功能,例如三角函数,幂,根等,也可以使其更具吸引力。
Previous article: Hello, in the world of kivy
上一篇:您好,在猕猴桃世界
For more such posts, do follow out our Publication:https://medium.com/tek-society
有关更多此类帖子,请遵循我们的出版物: https : //medium.com/tek-society
Also do clap! It encourages me to write better!
还拍手! 它鼓励我写得更好!
Thank you!
谢谢!
翻译自: https://medium.com/tek-society/my-first-application-in-kivy-bdf2a560287f
猕猴桃提取密码