【问题标题】:Kivy ~ AttributeError: 'TextInput' object has no attribute 'replace'Kivy ~ AttributeError: 'TextInput' 对象没有属性 'replace'
【发布时间】:2021-05-29 22:05:04
【问题描述】:

我一直在搞乱 python kivy,我偶然发现了标题中给出的错误。我正在尝试通过 kivy 使用 yfinance 包来打印任意股票的股价。我得到错误:AttributeError:'TextInput'对象没有属性'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

'

提前致谢!!

【问题讨论】:

    标签: python user-interface attributes kivy yfinance


    【解决方案1】:

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

    那么罪魁祸首大概是:

    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)

    【讨论】:

    • 谢谢,它现在可以工作了。我认为它已经与它期待一个字符串有关,但我并没有想到你的解决方案。干杯;)
    猜你喜欢
    • 2014-09-02
    • 2019-09-12
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2016-04-21
    • 2018-05-10
    相关资源
    最近更新 更多