如何将从 Beautifulsoup 收集的数据打印到 TKinter GUI 上的标签上

问题描述 投票:0回答:1

我正在开发一个应用程序,该应用程序使用 Beautiful Soup 从网站获取产品价格信息。 获得数据后,我想使用 TKinter 在标签上显示所有数据。我将来自网站的所有数据都放入列表中,但由于某种原因,当我尝试使用 print_data 方法打印到标签时,它只打印一行。请参阅下面的我的代码。 有人可以帮忙吗?

    def fetch_price_data(self):
        self.URL = ""
        from bs4 import BeautifulSoup
        num = 1
        for i in self.PRODUCT_URL_LIST:
            self.URL = i
            response = self.requests.get(self.URL)
            soup = BeautifulSoup(response.text, "html.parser")
            data = (str(num) + " " + soup.find_all("h1")[0].text + " " +
                    soup.find_all(class_="product-price")[0].text.split("€")[1])
            self.listed_products.append(data)
            num += 1
            print(data)
    def print_data(self):
        for i in range(len(Fetchdata.listed_products)):
            return Fetchdata.listed_products[i] + "\n"
    def make_gui(self):
        self.window = Tk()
        self.app_menu()
        self.window.config(width=1100, height=560, background="#68BB59")
        self.window.minsize(width=1100, height=560)
        self.window.title("ALEO & MORE DATA MANAGER")
        self.window.resizable(width=False, height=False)
        # canvas = Canvas()
        # canvas.config(width=1075, height=480)
        # canvas.place(x=10, y=10)
        get_data_btn = Button(master=self.window, text="SYNC", width=15, height=2, font=("arial", 10)
                              , background="#D4D4D4")
        get_data_btn.place(x=960, y=505)
        checkInternetHttplib()
        # canvas.create_text(200, 60, text="Hello Everyone", font=("Arial", 10, "bold"))
        lable = Label(width=153, height=32, text=self.print_data() + "\n")
        lable.place(x=10, y=10)

我尝试使用画布来打印信息,但这也不起作用。 我是 python 新手,如果我的代码到处都是,很抱歉。

python user-interface tkinter label
1个回答
0
投票

不要在

return
循环中使用
for
,因为它立即存在循环 - 并且它无法为列表上的下一个元素运行代码。

但是你不需要

for
循环。您必须将列表中的所有字符串转换为一个字符串

def print_data(self):
    return "\n".join(Fetchdata.listed_products)
© www.soinside.com 2019 - 2024. All rights reserved.