AttributeError:“str”对象在 tkinter 标签中没有属性“tk”

我正在制作一个程序来跟踪印度的新冠病例。我的程序的命令行版本也取得了成功。于是,我想到了升级到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'


森栏
浏览 122回答 2
2回答

qq_花开花谢_0

试试这个:updated_as = Label(root, text=("Updated As:-", data[0].get_text()[1:-1]))updated_as.pack()我只是使用关键字参数text或tkinter混淆parent位置参数,因为"Updated As:-", data[0].get_text()[1:-1]它是 a str,因此给出了错误。还要确保使用pack()(或任何其他布局管理器)将小部件放在屏幕上。小部件采用的第一个位置参数是master窗口(root在您的情况下),并且text作为关键字参数出现。您还忘记了,root.mainloop()如果没有它,窗口将不会显示。确保对所有小部件执行相同的操作。整个工作代码:from bs4 import BeautifulSoupimport urllib.requestfrom 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 Versionupdated_as = Label(root,text=("Updated As:-", data[0].get_text()[1:-1]))updated_as.pack()Total_Cases = Label(root,text=(data2[1].get_text()))Total_Cases.pack()Active_Cases = Label(root,text=("Active Cases:", data[2].get_text(), "("+data[3].get_text()+")"))Active_Cases.pack()Recov_Cases = Label(root,text=("Recovered Cases:", data[5].get_text(), "("+data[6].get_text()+")"))Recov_Cases.pack()Deaths = Label(root,text=("Deaths:", data[8].get_text(), "("+data[9].get_text()+")"))Deaths.pack()Tests_Done = Label(root,text=("Tests Done:", data[11].get_text(), "("+data[12].get_text()+")"))Tests_Done.pack()root.mainloop()我也认为摆脱它是可以的,root因为你只有一扇窗户,这很好。但关键字参数text很重要。

慕莱坞森

您是否尝试在将其分配给变量的同一指令中打包()updated_as?调用 pack() 方法意味着它将返回 None。也许尝试一下updated_as&nbsp;=&nbsp;Label(args) updated_as.pack()
打开App,查看更多内容
随时随地看视频慕课网APP