我的目标是创建一个简单的应用程序为基础的计算器-从一种货币到另一种货币转换,但事情是,转换率应该自动从网站检索.为此,我决定使用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

你能看到关于英镑的信息吗?如何获得这一点,并包括在猕猴桃?我知道我可以通过美味的汤得到它,但是如何包含在猕猴桃中,现在清楚了吗?

推荐答案

您可以在CalcGridLayout类中使用DictProperty实例变量来存储擦除后的汇率,并在.kv文件中使用该变量.汇率在后台的单独线程上每分钟更新一次.

def get_exchange_rates():
  # code where you scrape using beautifulsoup and return a dict of 
  # exchange rates
  return {}


class CalcGridLayout(GridLayout):
  exchange_rates = DictProperty(get_exchange_rates())

  def _update_exchange_rates(self):
    while True:
      self.exchange_rates = DictProperty(get_exchange_rates())
      time.sleep(60). # update every minute

  # 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"


class CalculatorApp(App):
  def build(self):
    layout = CalcGridLayout()
    update_thread = threading.Thread(target=layout._update_exchange_rates)
    update_thread.daemon = True
    update_thread.start()
    return layout

在.kv文件中使用exchange_rates,如下所示:

CustButton:
  text: "GBP"
  on_press: entry.text = entry.text + '/' + str(calculator.exchange_rates['GBP'])

Python相关问答推荐

数字梯度的意外值

如何使用上下文管理器创建类的实例?

通过交换 node 对链接列表进行 Select 排序

Python -根据另一个数据框中的列编辑和替换数据框中的列值

分组数据并删除重复数据

多处理代码在while循环中不工作

如何使用scipy从频谱图中回归多个高斯峰?

如何计算两极打印机中 * 所有列 * 的出现次数?

使用numpy提取数据块

抓取rotowire MLB球员新闻并使用Python形成表格

非常奇怪:tzLocal.get_Localzone()基于python3别名的不同输出?

将数据框架与导入的Excel文件一起使用

为什么默认情况下所有Python类都是可调用的?

如何设置视频语言时上传到YouTube与Python API客户端

如何启动下载并在不击中磁盘的情况下呈现响应?

如何从列表框中 Select 而不出错?

Polars将相同的自定义函数应用于组中的多个列,

干燥化与列姆化的比较

并行编程:同步进程

查看pandas字符列是否在字符串列中