简体   繁体   English

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

[英]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()

output输出在此处输入图片说明

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.

相关问题 在我的 treeview (python tkinter) 中,当我添加数据时,我希望根据父名称 (tkinter) 添加数据 - In my treeview (python tkinter), when I add data, I want the data to be added based on parent name (tkinter) 我想在 python 中使用 ORM 将数据添加到我的表中 - I want to add data to my table using ORM in python 我想在给定的虚函数中更新我的函数interpolate() - i want to update my function interpolate() in given dummy function 我想将“图像加载按钮”添加到我的opencv项目中 - I want to add 'image load button' to my opencv project Tkinter - 在我的项目中添加背景后出现问题 - Tkinter - Problem after add background in my project tkinter ttk 样式在 tkinter 中,我想让我的 label 出现 - tkinter ttk style In tkinter, I want to make my label appear 我想根据某些条件在我的数据框中添加一列 - I want to add a column in my Data Frame according to some condition 如何将闹钟 function 添加到我的 tkinter 简单时钟项目中? - How do I add an alarm function to my tkinter simple Clock project? 无法使用 tkinter 将我的数据添加到 mysql - cannot add my data to mysql using tkinter 我想用伪头做一个链表但是为什么代码打印反向!!。 这是我的代码 - I want to make a linked list by dummy headed But why the code is printing reverse!!. Here is my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM