Issue
tkinter code
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,...
Solution
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:
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:
table.insert(parent='', index=index, iid=index, text='', values=(i, price, random.choice(["", random.randint(0, 100)])))
Answered By - MendelG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.