我正在制作一个程序来跟踪印度的新冠病例。我的程序的命令行版本也取得了成功。于是,我想到了升级到GUI版本。但我收到一个错误。
这是Python代码:-
from bs4 import BeautifulSoup
import urllib.request
from tkinter import *
root = Tk()
print("Retrieving Source....")
site_data = urllib.request.urlopen("https://covidindia.org")
site_html = BeautifulSoup(site_data , 'html.parser')
data = site_html.find_all(style = "text-align: center;")
data2= site_html.find_all("h1")
# GUI Version
updated_as = Label("Updated As:-", data[0].get_text()[1:-1])
Total_Cases = Label(data2[1].get_text())
Active_Cases = Label("Active Cases:", data[2].get_text(), "("+data[3].get_text()+")")
Recov_Cases = Label("Recovered Cases:", data[5].get_text(), "("+data[6].get_text()+")")
Deaths = Label("Deaths:", data[8].get_text(), "("+data[9].get_text()+")")
Tests_Done = Label("Tests Done:", data[11].get_text(), "("+data[12].get_text()+")")
# Command Line Version
# print("Updated As:-", data[0].get_text()[1:-1])
# print(".................................................")
# print(data2[1].get_text())
# print("Active Cases:", data[2].get_text(), "("+data[3].get_text()+")")
# print("Recovered Cases:", data[5].get_text(), "("+data[6].get_text()+")")
# print("Deaths:", data[8].get_text(), "("+data[9].get_text()+")")
# print("Tests Done:", data[11].get_text(), "("+data[12].get_text()+")")
# input("Press Enter to Exit!")
这是错误堆栈:
Traceback (most recent call last):
File "E:\Files\python\CovidTracker\main.py", line 15, in <module>
updated_as = Label("Updated As:-", data[0].get_text()[1:-1])
File "B:\Users\adity\AppData\Local\Programs\Python\Python38-
32\lib\tkinter\__init__.py", line 3143, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "B:\Users\adity\AppData\Local\Programs\Python\Python38-3
2\lib\tkinter\__init__.py", line 2561, in __init__
BaseWidget._setup(self, master, cnf)
File "B:\Users\adity\AppData\Local\Programs\Python\Python38-
32\lib\tkinter\__init__.py", line 2530, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
qq_花开花谢_0
慕莱坞森