-1

I am trying to create a stock app (as usual), and I keep getting an error when I click the search button, saying my text box has no get method. The api is okay, and everything is running fine, but the input box is a bit concerning for the functionality of my app. Here is the code:

import http.client as client
from tkinter import *
import json

root = Tk()
root.geometry('500x500')

conn = client.HTTPSConnection("realstonks.p.rapidapi.com")

headers = {
    'X-RapidAPI-Key': "[CLASSIFIED]",
    'X-RapidAPI-Host': "realstonks.p.rapidapi.com"
}
root.destroy()
root = Tk()
root.geometry('500x500')
inputbox = Text(root, padx = 40, pady = 30).pack()
name = ""

def layout(data_obj):
  vol = data_obj['total_vol']
  price = data_obj['price']
  percent = data_obj['change_percentage']
  if percent < 0:
      fg = 'red'
  elif percent > 0:
      fg = 'green'
  elif percent == 0:
      fg = 'gray'
  title_widget = Label(root, text = name, font = ("arial bold", 50), underline = True).pack()
  vol_widget = Label(root, text = vol + " in total.", font = ("arial bold", 40)).pack()
  price_widget = Label(root, text = str(price) + " dollars per share.", font = ("arial bold", 30)).pack()
  percent_widget = Label(root, text = str(percent) + "%", font = ("arial bold", 20), fg = fg).pack()

def search_function():
  name = inputbox.get("1.0","end-1c")
  conn.request("GET", "/" + name.upper(), headers=headers)
  res = conn.getresponse()
  data = res.read()
  # print(data)
  data_obj = json.loads(data.decode('utf-8'))
  layout(data_obj)

search = Button(root, text = "Search Info", background = "gray", command = search_function).pack()

root.mainloop()

And here is the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "main.py", line 45, in search_function
    name = inputbox.get("1.0","end-1c")
AttributeError: 'NoneType' object has no attribute 'get'

1 Answers1

0

Call .pack() separately, after the assignment:

inputbox = Text(...)
inputbox.pack()
pts
  • 80,836
  • 20
  • 110
  • 183