[英]I want to add dummy data in a table in my tkinter project
tkinter code 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()
start of web scraper网页抓取工具的启动
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)
end of web scraper网络刮板的末端
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()
I want any dummy data under the stock: part我想要股票下的任何虚拟数据:部分
Name and Price are all legit data from a web scraper as shown in the code the dummy data can be any thing but most preferably numbers like 97, 33,...名称和价格都是来自网络爬虫的合法数据,如代码所示,虚拟数据可以是任何东西,但最好是数字,如 97、33、...
I don't really know Tkinter
, but one option would be to insert a random number using the randint
method available from the built-in random
module:我真的不知道
Tkinter
,但一种选择是使用内置random
模块提供的randint
方法插入一个随机数:
import random
[...]
table.insert(parent='', index=index, iid=index, text='', values=(i, price, random.randint(0, 100)))
EDIT: If you want to randomly insert a blank value or a digit, you can use random.choice
to pick either an empty string ""
or a number:编辑:如果你想随机插入一个空白值或一个数字,你可以使用
random.choice
来选择一个空字符串""
或一个数字:
table.insert(parent='', index=index, iid=index, text='', values=(i, price, random.choice(["", random.randint(0, 100)])))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.