我的目标是创建一个简单的应用程序为基础的计算器-从一种货币到另一种货币转换,但事情是,转换率应该自动从网站检索.为此,我决定使用kivy库.基于以下示例: kivy calculator个 我设法更改了一些小部分,得到了以下代码和结果:
以下是.py文件和.kv文件的代码
`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 buttons 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()`
和相应的.kv文件
<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 =entry.text+'/'+ str(2.54)
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: "yen"
on_press: entry.text =entry.text+'*'+ str(54.21)
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: "euro"
on_press: entry.text =entry.text+'/'+ str(2.8070)
# 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: "GBP"
on_press: entry.text =entry.text+'/'+ str(3.1790)
BoxLayout:
CustButton:
font_size: 20
text: "Scientific calculator"
on_press: entry.text = ""
`
当我运行.py文件时,我得到以下结果: result of my code个
但问题是,如果我们查看代码:
text: "GBP" on_press: entry.text =entry.text+'/'+ str(3.1790)
个
GBP是静态的,如果我想用美汤从网络上获取这些数据,如何将结果结合到我们的课堂上?请给我一个提示
编辑:由于之前的问题有不清楚的 comments ,也就是我问的问题不清楚,我将重复问题: 请看下面的代码:
text: "GBP"
on_press: entry.text =entry.text+'/'+ str(3.1790)
除了直接写汇率,我如何从网站上读取它并将其包含在Kivy中,这里有什么不清楚的?例如,请查看以下链接: Exchange rates个
你能看到关于英镑的信息吗?如何获得这一点,并包括在猕猴桃?我知道我可以通过美味的汤得到它,但是如何包含在猕猴桃中,现在清楚了吗?