我想在我的 tkinter 项目中的 table 中添加虚拟数据

I want to add dummy data in a table in my tkinter project

tkinter 代码

from tkinter import *
from tkinter import ttk
import requests
from bs4 import BeautifulSoup

# making the window
root = Tk()
# initiating the table
table = ttk.Treeview(root)
table['columns'] = ('Name', 'Price', 'Dummy')
table.column('#0', width=0, stretch=NO)
table.column('Name', anchor=CENTER, width=250)
table.column('Price', anchor=CENTER, width=150)
table.column('Dummy', anchor=CENTER, width=150)
# table.heading('#0', text='', anchor=CENTER)
table.heading('Name', text='Name', anchor=CENTER)
table.heading('Price', text='Price', anchor=CENTER)
table.heading('Dummy', text='Stock: ', anchor=CENTER)

drink_price = {}

index = 0

table.pack()

网络抓取工具开始

webpage = requests.get(
  "https://www.spinneyslebanon.com/catalogsearch/result/?q=pepsi")

soup = BeautifulSoup(webpage.content, "html.parser")

title = soup.find_all("a", "product-item-link")
price = soup.find_all("span", class_="price")

titles = []
prices = []
for index, tp in enumerate(zip(title, price)):
    if index >= 10:
        break
    drink_price[tp[0].get_text(strip=True)] = tp[1].get_text(strip=True)

print(titles)
print(prices)

网络抓取工具结束

for i in drink_price:
    price = drink_price.get(i)
    table.insert(parent='', index=index, iid=index, text='', values=(i, price))
    index += 1

    root.mainloop()

输出

我想要股票下的任何虚拟数据:part

名称和价格都是来自网络爬虫的合法数据,如代码所示 虚拟数据可以是任何东西,但最好是数字,如 97、33、...

我真的不知道 Tkinter,但一种选择是使用 randint method available from the built-in random 模块插入一个随机数:

import random
[...]

table.insert(parent='', index=index, iid=index, text='', values=(i, price, random.randint(0, 100)))

编辑: 如果您想随机插入一个空白值或一个数字,您可以使用 random.choice 来选择一个空字符串 "" 或一个数字:

table.insert(parent='', index=index, iid=index, text='', values=(i, price, random.choice(["", random.randint(0, 100)])))