繁体   English   中英

Kivy ~ AttributeError: 'TextInput' object 没有属性 'replace'

[英]Kivy ~ AttributeError: 'TextInput' object has no attribute 'replace'

我一直在搞乱 python kivy,我偶然发现了标题中给出的错误。 我正在尝试使用 yfinance package 到 kivy 打印任意股票的股价。 我收到错误:AttributeError: 'TextInput' object has no attribute 'replace'。 你们有没有人知道我在这里做错了什么:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.widget import Widget
from kivy.config import Config
import yfinance as yf 
import numpy as np
   

    
class MyStock(FloatLayout):
    
    enterticker = ObjectProperty(None)
    enterstartdate = ObjectProperty(None)
    enterenddate = ObjectProperty(None)
    
    def DownloadStockData(self, ticker, starting, ending):

        Download = yf.download(ticker, start = starting, end = ending)
        StockData1 = Download["Close"]        
        StockValue = [0]*len(StockData1)
    
        for i in range(len(StockValue)):
            StockValue[i] = StockData1[i]
            
        
        return StockValue   

    def btn(self):        
        self.stockvalue.text = self.DownloadStockData(self.enterticker, self.enterstartdate, self.enterenddate)                        
        print(self.enterticker.text, self.enterstartdate.text, self.enterenddate.text, self.stockvalue.text)
    

class FinancialDashboard(App):    
     def build(self):
         Window.clearcolor = (1, 1, 1, 1)
         return MyStock()
   
                   
    
if __name__ == "__main__":
    FinancialDashboard().run()

和我的 kivy 文件:

<MyStock>:

    enterticker: enterticker
    enterstartdate: enterstartdate
    enterenddate: enterenddate
        

    FloatLayout:
        
        Button:
            pos_hint: {"x":0.41, "y":0.9}
            text: "Confirm" 
            font_size:14
            color:0, 0, 0, 1
            size_hint:0.2, 0.1
            background_color:0.9, 0.9, 0.9,1
            on_press: root.btn()     
            
            
                        
            
        Label:
            color: 0, 0, 0, 1
            pos_hint: {"x":0.01, "y":0.9}
            text: "Enter ticker"
            size_hint:0.2, 0.1
            
        TextInput:
            id: enterticker                 
            color: 0, 0, 0, 1
            pos_hint: {"x":0.19, "y":0.9}
            multiline:False
            size_hint:0.2, 0.1
            
            
        Label:
            color: 0, 0, 0, 1
            pos_hint: {"x":0.01, "y":0.8}
            text: "Enter Start Date "
            size_hint:0.2, 0.1
            
        TextInput:
            id: enterstartdate                    
            color: 0, 0, 0, 1
            pos_hint: {"x":0.19, "y":0.8}
            multiline:False
            size_hint:0.2, 0.1


        Label:
            color: 0, 0, 0, 1
            pos_hint: {"x":0.01, "y":0.7}
            text: "Enter End Date "
            size_hint:0.2, 0.1
            
        TextInput:
            id: enterenddate                 
            color: 0, 0, 0, 1
            pos_hint: {"x":0.19, "y":0.7}
            multiline:False
            size_hint:0.2, 0.1
            
            
            

        Label:
            color:0, 0, 0, 1
            pos_hint: {"x":0.01, "y":0.6}
            text: "stockvalue"
            size_hint:0.2, 0.1

带有错误消息:

   File "C:\Users\xxxx\Documents\Python xxxx\OneDrive-2021-02-26\StockGUI.py", line 22, in DownloadStockData
     Download = yf.download(ticker, start = starting, end = ending)
   File "C:\Users\xxxx\anaconda3\lib\site-packages\yfinance\multi.py", line 71, in download
     tickers, (list, set, tuple)) else tickers.replace(',', ' ').split()
   File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
 AttributeError: 'TextInput' object has no attribute 'replace

'

提前致谢!!

您的错误文本清楚地表明您正在将TextInput object 传递给期望接收字符串的内容。

那么罪魁祸首大概是:

self.stockvalue.text = self.DownloadStockData(self.enterticker, self.enterstartdate, self.enterenddate) 

您可能想要:

self.stockvalue.text = self.DownloadStockData(self.enterticker.text, self.enterstartdate.text, self.enterenddate.text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM