我正在尝试创建一个程序,使用户可以选择特定的时间范围并显示相关的股票价格图。但是现在,即使我已经输入了一些正常数字,Python也只会返回此错误消息。任何帮助都非常感谢。
第二个代码的块是我的按钮和条目。理想情况下,在键入启动和结束日期后,单击"提交"按钮后,应触发Show函数的第一个代码块。
Exception in Tkinter callback
Traceback (most recent call last):
File "D:Casual Softwarespythonlibtkinter__init__.py", line 1699, in
__call__
return self.func(*args)
File "D:/Casual Softwares/python/untitled/UPDATING GRAPH.py", line 56, in
show
start = dt.datetime(sY, sM, sD)
ValueError: year 0 is out of range
这是相关代码
def show():
global startD
global startM
global startY
global endD
global endM
global endY
global coy
global firm
sY=startY.get()
sM=startM.get()
sD=startD.get()
eY=endY.get()
eM=endM.get()
eD=endD.get()
if firm==False:
popupmsg('Please Select A Company First')
else:
fig = plt.figure(figsize=(7, 5))
ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=4, colspan=1)
ax2 = plt.subplot2grid((6, 1), (4, 0), rowspan=2, colspan=1, sharex=ax1)
ax1.clear()
ax2.clear()
start = dt.datetime(sY, sM, sD)
end = dt.datetime(eY, eM, eD)
df = web.get_data_yahoo(coy, start, end)
ax1.plot(df.index, df['Adj Close'], label='Adjusted Close')
ax2.bar(df.index, df['Volume'], label='Volume', width=25, color='c')
for label in ax2.xaxis.get_ticklabels():
label.set_rotation(45)
ax1.set_title(coy)
ax1.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2,
borderaxespad=0)
ax2.legend()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=1, column=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas.show()
canvas.draw()
def changePeriod():
global startD
global startM
global startY
global endD
global endM
global endY
startD = IntVar()
startM = IntVar()
startY = IntVar()
endD = IntVar()
endM = IntVar()
endY = IntVar()
period = tk.Tk()
period.wm_title('Periods')
button1 = ttk.Button(period, text='Submit', width=10,command=show)
button1.grid(row=5, columnspan=4, sticky='s')
label = ttk.Label(period, text='Enter Dates',font=MID_FONT)
label.grid(row=0,columnspan=4,sticky='n')
label2=ttk.Label(period,text='Days')
label3=ttk.Label(period,text='Months')
label4=ttk.Label(period,text='Years')
label5=ttk.Label(period,text='Start',font=MID_FONT)
label2.grid(row=2,column=0)
label3.grid(row=3, column=0)
label4.grid(row=4, column=0)
label5.grid(row=1, column=0)
entry1 = ttk.Entry(period,textvariable=startD,width=10)
entry1.insert(0, 1)
entry2 = ttk.Entry(period,textvariable=startM,width=10)
entry2.insert(0, 1)
entry3 = ttk.Entry(period,textvariable=startY,width=10)
entry3.insert(0, 2010)
entry1.grid(row=2,column=1)
entry2.grid(row=3, column=1)
entry3.grid(row=4, column=1)
entry1.focus_set()
label6 = ttk.Label(period, text='Days')
label7 = ttk.Label(period, text='Months')
label8 = ttk.Label(period, text='Years')
label9 = ttk.Label(period, text='End',font=MID_FONT)
label6.grid(row=2, column=2)
label7.grid(row=3, column=2)
label8.grid(row=4, column=2)
label9.grid(row=1, column=2)
entry4 = ttk.Entry(period,textvariable=endD,width=10)
entry4.insert(0, 20)
entry5 = ttk.Entry(period,textvariable=endM,width=10)
entry5.insert(0, 9)
entry6 = ttk.Entry(period,textvariable=endY,width=10)
entry6.insert(0, 2017)
entry4.grid(row=2, column=3)
entry5.grid(row=3, column=3)
entry6.grid(row=4, column=3)
period.mainloop()
通过执行entry1.insert(0, 1)
,您正在围绕IntVar
s工作,因此其值永远不会随着初始值而更新。而是使用startD.set(1)
。startM
,startY
,endD
,endM
和endY
。