2017-09-23 75 views
0

我正在嘗試創建一個程序,讓用戶選擇特定的時間範圍並顯示相關的股票價格圖。但是現在,即使我輸入了一些正常的數字,Python也只會返回此錯誤消息。任何幫助是極大的讚賞。即使當我輸入正數時,Tkinter輸入框的輸入值爲零

第二塊代碼是我的按鈕和條目。理想情況下,在輸入開始日期和結束日期後,點擊提交按鈕後,應該觸發顯示功能的第一個代碼塊。

Exception in Tkinter callback 
Traceback (most recent call last): 
File "D:\Casual Softwares\python\lib\tkinter\__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() 

回答

2

這樣做entry1.insert(0, 1)你在你IntVar s工作,因此其價值永遠不會與初始值進行更新。相反,請使用startD.set(1)。對於startMstartY,endDendMendY也是如此。

+0

你是對的我完全錯過了!因爲你的答案是正確的,我會刪除我的答案。 – ROAR

+0

謝謝tobias_k –