'wxPython: How can I display information from a database with information from a url?
I am doing an assignment for my python class and I cannot figure out where to go from here. I have tried asking, but did not get an answer. I made a GUI using wxPython to display stock information from a database, along with the price information from a url for each company(each company is represented by a different symbol)
When I run the program and hit the display button, The company name, symbol, and purchase price show in the correct list section, but the values for the "Shares" are displayed in the "current price" section.
I'm not sure what I'm supposed to do to display the current price and how to calculate the Gain/loss and have it displayed in the correct spot. This is the url with my token 'https://finnhub.io/api/v1/quote?symbol=' + 'c95o1aiad3iek697puk0'
This is the table named 'dow_stock' from the database named 'tech_stocks'
This is the code I have so far:
import wx
import sqlite3 as db
import datetime
import requests
class MyDialog(wx.Dialog):
def __init__(self):
wx.Dialog.__init__(self, None, title="Dialog")
lbl = wx.StaticText(self, label='Read Data', pos=(120, 10))
self.com = wx.TextCtrl(self, -1, '', pos=(30, 40))
wx.StaticText(self, -1, 'Company', pos=(150, 40))
self.sym = wx.TextCtrl(self, -1, '', (30, 80))
wx.StaticText(self, -1, 'Symbol', (150, 80))
self.pur = wx.TextCtrl(self, -1, '', (30, 120))
wx.StaticText(self, -1, 'Purchase Price', (150, 120))
self.cur = wx.TextCtrl(self, -1, '', (30, 160))
wx.StaticText(self, -1, 'Current Price', (150, 160))
self.sha = wx.TextCtrl(self, -1, '', pos=(200, 40))
wx.StaticText(self, -1, 'Shares', pos=(320, 40))
self.gl = wx.TextCtrl(self, -1, '', pos=(200, 80))
wx.StaticText(self, -1, 'Gain/Loss', pos=(330, 80))
okBtn = wx.Button(self, id=wx.ID_OK, pos=(135, 200))
class DataFrame(wx.Frame):
def __init__(self):
super().__init__(None, id=-1, title='read data', size=(680, 520))
self.companyNames = { 'MMM': '3M', 'AAPL': 'Apple Inc.', 'BA': 'Boeing',
'CAT': 'Caterpillar Inc.', 'CSCO': 'Cisco Systems',
'HON': 'Honeywell', 'IBM': 'IBM', 'INTC': 'Intel',
'MSFT': 'Microsoft'}
panel = wx.Panel(self, -1)
# Top Widgets
self.date = wx.StaticText(panel, -1, "Today's Date: ")
self.gl = wx.StaticText(panel, -1, "Total: ")
# List Control
self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT, size=(590, 280))
font1 = wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.NORMAL, wx.NORMAL, False, u'Consolas')
self.list.SetFont(font1) # use a monospace font
self.list.InsertColumn(0, 'Company', width=100)
self.list.InsertColumn(1, 'Symbol', width=80)
self.list.InsertColumn(2, 'Purchase Price', width=110)
self.list.InsertColumn(3, 'Current Price', width=110)
self.list.InsertColumn(4, 'Shares', width=80)
self.list.InsertColumn(5, 'Gain/Loss', wx.LIST_FORMAT_LEFT, width=110)
# Buttons
display = wx.Button(panel, -1, 'Display', size=(-1, 40))
cancel = wx.Button(panel, -1, 'Cancel', size=(-1, 40))
display.Bind(wx.EVT_BUTTON, self.OnDisplay)
cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
# Top Sizer
topRowHsizer = wx.BoxSizer(wx.HORIZONTAL)
topRowHsizer.Add(self.gl, 0, wx.ALIGN_CENTER | wx.ALL, border=10)
#Button Sizer
btnRowHsizer = wx.BoxSizer(wx.HORIZONTAL)
btnRowHsizer.Add(display, 0, wx.ALIGN_CENTER | wx.ALL, border=15)
btnRowHsizer.Add(cancel, 0, wx.ALIGN_CENTER | wx.ALL, border=15)
#Main Sizer
mainBoxVsizer = wx.BoxSizer(wx.VERTICAL)
mainBoxVsizer.Add(topRowHsizer, 0, wx.ALIGN_CENTER | wx.ALL, border=15)
mainBoxVsizer.Add(self.date, 0, wx.ALIGN_CENTER | wx.BOTTOM, border=8)
mainBoxVsizer.Add(self.list, 0, wx.ALIGN_CENTER | wx.ALL, border=15) # add list ctrl & buttons to sizers
mainBoxVsizer.Add(btnRowHsizer, 0, wx.ALIGN_CENTER | wx.ALL, border=15)
panel.SetSizer(mainBoxVsizer)
self.Centre()
def OnCancel(self, event):
self.Close() # exit program
def OnDisplay(self,event):
self.list.DeleteAllItems()
con = db.connect('tech_stocks.db')
cur = con.cursor()
sqlite_select_query = """SELECT company, symbol, purchase_price, shares FROM dow_stocks"""
cur.execute(sqlite_select_query)
records = cur.fetchmany(9)
for row in records:
self.list.Append(row)
cur.close()
con.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

