我的目标是创建一个简单的基于应用程序的计算器——从一种货币转换为另一种货币,但转换率应该从网站自动检索。为此,我决定使用 kivy library.based on the following example: kivy 计算器 我设法改变了一些小部分并得到了以下代码和结果:
这里是.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 = ""
`
但问题是,如果我们看一下代码:
text: "GBP" on_press: entry.text =entry.text+'/'+ str(3.1790)
GBP 是静态的,如果我想使用 beautifull soup 从 web 获取这些数据,如何将结果合并到我们的类中?请给我一个提示
编辑:由于对问题的评论不明确,我不清楚我在问什么,我会重复问题: 看看下面的代码:
text: "GBP"
on_press: entry.text =entry.text+'/'+ str(3.1790)
不是直接写汇率,我如何从网站上读取它并包含在 kivy 中,这里有什么不清楚的?寻找例如以下链接: 汇率
你能看到关于英镑的信息吗?如何得到这个并包含在kivy中?我知道我可以通过 beuatifulsoup 得到它,但是如何包含在 kivy 中,现在清楚了吗?