用户输入要在以后的功能中使用的变量

时间:2019-03-07 04:47:45

标签: python pandas tkinter

我想制作一个程序,允许用户选择股票行情自动收录器和其他输入,然后获取实时数据,该数据立即返回程序并显示股票信息和物品。

然后,用户应该能够挑选股票以及开始和结束日期,并从这些日期中查看一些基本的股票信息。该代码本身可以工作,但是我一直在努力使用GUI来向股票程序提供输入。

from tkinter import *
#import tkinter
from tkinter import ttk
import random
import random
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
from optparse import OptionParser

compname = 'Company Name'
tckersymbl = 'Ticker Symbol'
strtdte = 'Start Date (y, m, d)'
numbday = 'Number of Days'


#fields = 'Company Name', 'Ticker Symbol', 'Start Date (y, m, d)', 'Number of Days'

def fetch(entries):
   for entry in entries:
      field = entry[0]
      text  = entry[1].get()
      companyname = entry[0]
      print('%s: "%s"' % (field, text))


def makeform(root, tckersymbl):
   entries = []
   for field in compname:
      companyname = entry[0]
      row = Frame(root)
      lab = Label(row, width=15, text=field, anchor='w')
      ent = Entry(row)
      row.pack(side=TOP, fill=X, padx=5, pady=5)
      lab.pack(side=LEFT)
      ent.pack(side=RIGHT, expand=YES)
      entries.append((field, ent))
   return entries

def fetch1(entries):
   for entry in entries:
      field1 = entry[2]
      text1  = entry[3].get()
      ticker = entry[2]
      print('%s: "%s"' % (field1, text1)) 

def makeform1(root, tckersymbl):
   entries = []
   for field1 in tckersymbl:
      ticker = entry[2]
      row = Frame(root)
      lab = Label(row, width=15, text=field1, anchor='w')
      ent = Entry(row)
      row.pack(side=TOP, fill=X, padx=5, pady=5)
      lab.pack(side=LEFT)
      ent.pack(side=RIGHT, expand=YES)
      entries.append((field1, ent))
   return entries

def fetch2(entries):
   for entry in entries:
      field2 = entry[4]
      text2  = entry[5].get()
      startday = entry[4]
      startday1 = startday.int()
      print('%s: "%s"' % (field2, text2)) 

def makeform2(root, strtdte):
   entries = []
   for field2 in strtdte:
      startday = entry[4]
      startday1 = startday.int()
      row = Frame(root)
      lab = Label(row, width=15, text=field2, anchor='w')
      ent = Entry(row)
      row.pack(side=TOP, fill=X, padx=5, pady=5)
      lab.pack(side=LEFT)
      ent.pack(side=RIGHT, expand=YES)
      entries.append((field2, ent))
   return entries

def fetch3(entries):
   for entry in entries:
      field3 = entry[6]
      text3  = entry[7].get()
      numbdays = entry[6]
      print('%s: "%s"' % (field3, text3)) 

def makeform3(root, numbday):
   entries = []
   for field3 in numbday:
      numbdays = entry[6]
      row = Frame(root)
      lab = Label(row, width=15, text=field3, anchor='w')
      ent = Entry(row)
      row.pack(side=TOP, fill=X, padx=5, pady=5)
      lab.pack(side=LEFT)
      ent.pack(side=RIGHT, expand=YES)
      entries.append((field3, ent))
   return entries

#companyname = entry[0]
#ticker = entry[2]
#startday = entry[4]
#startday1 = startday.int()
#numbdays = entry[6]

print(companyname)
style.use('ggplot')

start = dt.datetime(startday1)
end = dt.datetime.now()

df = web.DataReader(ticker, 'iex', start, end)
df.reset_index(inplace=True)

print(df.head(numbdays)) #change value to change number of days you get. default is five


if __name__ == '__main__':
   root = Tk()
   root.geometry("350x175")
   ents = makeform(root, compname, tckersymbl, strtdte, numbday)
   root.bind('<Return>', (lambda event, e=ents: fetch(e)))   
   b1 = Button(root, text='Show',
          command=(lambda e=ents: fetch(e)))
   b1.pack(side=LEFT, padx=5, pady=5)
   b2 = Button(root, text='Quit', command=root.quit)
   b2.pack(side=LEFT, padx=5, pady=5)

   root.mainloop()





#compname = 'Company Name'
#tckersymbl = 'Ticker Symbol'
#strtdte = 'Start Date (y, m, d)'
#numbday = 'Number of Days'

1 个答案:

答案 0 :(得分:-1)

 class MyDocTemplate(BaseDocTemplate):
        def __init__(self, filename, **kw):
            self.allowSplitting = 0
            apply(BaseDocTemplate.__init__, (self, filename), kw)
            template = PageTemplate('normal', [Frame(1.0*cm, 1*cm, 19*cm, 27*cm, id='F1')])
            self.addPageTemplates(template)

        def afterFlowable(self, flowable):
            if flowable.__class__.__name__ == 'Paragraph':
            text = flowable.getPlainText()
            style = flowable.style.name
            if style == 'Heading1':
                level = 0
            elif style == 'Heading2':
                level = 1
            else:
                return
            E = [level, text, self.page]
    #if we have a bookmark name append that to our notify data
            bn = getattr(flowable,'_bookmarkName',None)
            if bn is not None:
                E.append(bn)
            self.notify('TOCEntry', tuple(E))

 class FooterCanvas(canvas.Canvas):

     def __init__(self, *args, **kwargs):
         canvas.Canvas.__init__(self, *args, **kwargs)
         self.pages = []

     def showPage(self):
         self.pages.append(dict(self.__dict__))
         self._startPage()

     def save(self):
         page_count = len(self.pages)
         for page in self.pages:
             self.__dict__.update(page)
             self.draw_canvas(page_count)
             canvas.Canvas.showPage(self)
         canvas.Canvas.save(self)

     def draw_canvas(self, page_count):
         page = "Page %s" % (self._pageNumber)
         self.saveState()
         self.setFont('Times-Roman', 10)
         self.line(40,23,560,23)
         self.drawString(280, 25, page)
         self.line(40, 810, 560, 810)
         self.drawString(40,813,"abc")
         self.restoreState()