您好我正在制作一个使用walmarts api的python tkinter gui,并允许您根据ID号搜索任何项目。我无法工作的是提交按钮,它什么都不做。非常感谢任何帮助。
import tkinter as tk
from tkinter import *
import tkinter.messagebox
def displayText():
global queryurl
global ItemIdEntry
global GetItemId
if ItemIdEntry.get().strip() == "":
tkinter.messagebox.showerror("Item Checker", "Enter an item id number! ")
else:
try:
import urllib.request as urllib2
except ImportError:
import urllib2
import json
GetItemId = ItemIdEntry.get().strip()
queryurl = ('http://walmartlabs.api.mashery.com/v1/items/'+GetItemId+'?format=json&apiKey=phutmkv9t262zj7dmbk8hkec')
f = urllib2.urlopen(queryurl)
json_string = f.read().decode('utf-8' )
parsed_json = json.loads(json_string)
itemName = parsed_json['name']
currentprice = parsed_json['salePrice']
upc = parsed_json['upc']
producturl = parsed_json['productUrl']
description = parsed_json['shortDescription']
online = parsed_json['availableOnline']
modelnumber = parsed_json['modelNumber']
print ('Name:', itemName)
print ('Current Price:', '$', currentprice)
print ('Upc Number:', upc)
print ('Model Number:', modelnumber)
print ('Purchaseable Online:', online)
print ('Description:', description)
print ('Product Url:', producturl)
f.close()
if __name__ == "__main__":
root = tk.Tk()
root.geometry("%dx%d+%d+%d" % (250, 100, 250, 100))
var = tk.StringVar(root)
root.title("Item Checker")
itemName = Label(root, text="Please enter an id number")
itemName.pack(side = TOP)
ItemIdEntry = Entry(root, bd =5)
ItemIdEntry.pack(side = TOP)
submitbutton = tkinter.Button(root, text ="Submit", command = displayText)#Submit button
submitbutton.pack()
root.mainloop()
答案 0 :(得分:1)
只是为了使它正式化。问题在于缩进。特别是代替:
if ItemIdEntry.get().strip() == "":
tkinter.messagebox.showerror("Item Checker", "Enter an item id number! ")
应该有:
if ItemIdEntry.get().strip() == "":
tkinter.messagebox.showerror("Item Checker", "Enter an item id number! ")
答案 1 :(得分:0)
你会期待什么?您在该功能中打印值。你不要显示它们。例如,而不是这个;
print ('Name:', itemName)
你应该使用它;
lbl = Label(text="{}".format(itemName), fg="red",font="Times 35 bold")
lbl.pack()
因此,无论何时单击按钮,您的功能都会显示它们。