【问题标题】:I was trying to display the data using tkinter.But suddenly this occured我试图使用 tkinter 显示数据。但突然发生了这种情况
【发布时间】:2022-07-30 18:16:21
【问题描述】:

我试图使用 tkinter 将数据提供给 gui

def get_country_data():
      name=textfield.get()
      url = 'https://www.worldometers.info/coronavirus/country/'+name
      html_data = get_html_data(url)
      dta = BeautifulSoup(html_data.text, 'lxml')
      covi = dta.find("div", class_="content-inner").find_all('div', id='maincounter-wrap')
      data = ""
      for block in covi:
           text = block.find('h1').get_text()  #this is where the error is occuring
           count = block.find('span').get_text()
           data = data + text + count + '\n'
      mainlabel['text']=data

这就是我将 tkinter 连接到数据的地方

    gbtn=tk.Button(root,text='Get Data',font=f,relief='solid',command=get_country_data)
    gbtn.pack()

我得到了错误

    Exception in Tkinter callback
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'get_text'

我对 tkinter 知之甚少。我不知道它是如何工作的。请帮我整理一下

【问题讨论】:

    标签: python tkinter beautifulsoup


    【解决方案1】:

    错误与 Tkinter 无关,与您的 get_country_data 函数有关。

    block.find('h1') 返回的是 NoneType 对象,因此当您调用 .get_text() 时,它会抛出 AttributeError

    【讨论】:

      猜你喜欢
      • 2022-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2018-07-28
      • 1970-01-01
      相关资源
      最近更新 更多