【发布时间】:2020-03-21 22:40:14
【问题描述】:
我正在尝试使用来自 JSON 文件的超链接在 tkinter 循环中添加按钮。当我按下按钮时,所有按钮的超链接都保持不变。它不会通过带有 JSON id 的 URL 链接循环。
当我打印它们时,它们都是不同的。
from urllib.request import urlopen
import json
import webbrowser
import tkinter as tk
with urlopen("https:example") as response:
source = response.read()
data=json.loads(source)
#this is the function that should be triggered with different url each time
def openweb():
webbrowser.open(url,new=1)
count=0
for product in data:
id = product['id']
name = product['name']
price = product['price']
aciklama = product['description']
#these are the links for the buttons
url = "https://www.example.com/tr-tr/i/"+id
#and here are the buttons with the command openweb defined above
element = tk.Button(canvasFrame, text='Button', borderwidth=0, bg="#EBEBEB",command=openweb)
element.grid(row=count,column=1,padx=5, pady=5, sticky="nsew")
T = tk.Text(canvasFrame, height=2, width=30)
T.insert(tk.INSERT,count)
T.grid(row=count,column=2,padx=5, pady=5)
count=count+1
root.mainloop()
【问题讨论】:
-
当您尝试打印时,它正在 for 循环内打印。但是每次您的 ID 在 URL 中被覆盖时,最后一个 ID 都会出现在最终 URL 中。我也看不到 URL 在 for 循环中的任何地方都被分配了
-
是什么让您认为您的按钮会打印不同的东西?所有这些都有相同的确切命令。到仅在按下按钮时调用的外部函数。
标签: python for-loop url tkinter hyperlink