Search code examples
pythontkintertypeerrorpython-3.5yahoo-finance

Python Tkinter TypeError: 'int' object is not callable


I am trying to take user input from main.py and then use that information to produce an output on runAnalytics. The problem that I am running into is that sometimes, the user input will produce a value of None (maybe null, I don't know) when the dividen_yield is not available and then my whole program will stop. It works flawlessly when there is a dividend-yield though.

main.py

import runAnalytics
from tkinter import *
import os
import centerWindow

loadApplication = Tk()
loadApplication.title("Stock Analytics")
loadApplication.geometry("1080x720")

label1 = Label(loadApplication, text = "Ticker")
input1 = Entry(loadApplication)

loadAnalytics = Button(loadApplication, text = "Load Analytics", command=lambda: runAnalytics.run(input1))

centerWindow.center(loadApplication)


label1.pack()
input1.pack()
loadAnalytics.pack()

loadApplication.mainloop()

runAnalytics.py

from yahoo_finance import Share
from tkinter import *
import os
import centerWindow

def run(input1):
    ticker = Share(input1.get())
    loadAnalytics = Tk()
    loadAnalytics.title("$" + "ticker" +  "Data")
    loadAnalytics.geometry("1080x720")
    centerWindow.center(loadAnalytics)

    ticker.refresh()

    if ticker.get_dividend_yield() is None:
        ticker.get_dividend_yield == 0

    share_price    = Label(loadAnalytics, text = "Share Price: " + ticker.get_price()).pack()
    prev_open      = Label(loadAnalytics, text = "Previous Open: " + ticker.get_open()).pack()
    prev_close     = Label(loadAnalytics, text = "Previous CLose: " + ticker.get_prev_close()).pack()
    dividend_yield = Label(loadAnalytics, text = "Dividend Yield: " + ticker.get_dividend_yield()).pack()
    year_low       = Label(loadAnalytics, text = "52 Week Low: " + ticker.get_year_low()).pack()
    year_high      = Label(loadAnalytics, text = "52 Week High: " + ticker.get_year_high()).pack()
    volume         = Label(loadAnalytics, text = "Volume: " + ticker.get_volume()).pack()

    loadAnalytics.mainloop()

[Error]

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\MyName\AppData\Local\Programs\Python\Python35-32\lib\tkinter__init__.py", line 1550, in call return self.func(*args) File "C:\Users\MyName\Documents\Python Projects\DataAnalytics\main.py", line 13, in loadAnalytics = Button(loadApplication, text = "Load Analytics", command=lambda: runAnalytics.run(input1)) File "C:\Users\MyName\Documents\Python Projects\DataAnalytics\runAnalytics.py", line 21, in run dividend_yield = Label(loadAnalytics, text = "Dividend Yield: " + ticker.get_dividend_yield()).pack() TypeError: 'int' object is not callable


Solution

  • I tried to execute your code and got a different error than you got:

    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
        return self.func(*args)
      File "./main.py", line 15, in <lambda>
        loadAnalytics = Button(loadApplication, text = "Load Analytics", command=lambda: runAnalytics.run(input1))
      File "/Users/Sven/temp/stackexchange/python/runAnalytics.py", line 22, in run
        dividend_yield = Label(loadAnalytics, text = "Dividend Yield: " + ticker.get_dividend_yield()).pack()
    TypeError: Can't convert 'NoneType' object to str implicitly
    

    So I was not able to reproduce the traceback you got, but I changed your Label declaration in the runAnalytics.py file using Python's Format Specification Mini-Language giving (double line indentation for direct copy and paste of the snippet):

        share_price    = Label(loadAnalytics,text='Share Price: {}'.format(ticker.get_price())).pack()
        prev_open      = Label(loadAnalytics,text='Previous Open: {}'.format(ticker.get_open())).pack()
        prev_close     = Label(loadAnalytics,text='Previous CLose: {}'.format(ticker.get_prev_close())).pack()
        dividend_yield = Label(loadAnalytics,text='Dividend Yield: {}'.format(ticker.get_dividend_yield())).pack()
        year_low       = Label(loadAnalytics,text='52 Week Low: {}'.format(ticker.get_year_low())).pack()
        year_high      = Label(loadAnalytics,text='52 Week High: {}'.format(ticker.get_year_high())).pack()
        volume         = Label(loadAnalytics,text='Volume: {}'.format(ticker.get_volume())).pack()
    

    This made the code runnable without any errors on my system since the .format() statement does all type conversions to the desired string output if necessary:

    GUI working

    Note that there is a small typo in the Label's text which I did not fix (CLose should be Close)