即使我输入正数,Tkinter 输入框也会返回零值
Tkinter Entry Box Returning Value Zero Even When I Typed In Positive Numbers
我正在尝试创建一个程序,让用户 select 特定的时间范围并显示相关的股票价格图。但是现在即使我输入了一些正常的数字,Python 也只会 return 这个错误信息。非常感谢任何帮助。
第二个代码块是我的按钮和条目。理想情况下,在输入开始日期和结束日期后,单击提交按钮后,应该触发第一个代码块,即显示功能。
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()
通过执行 entry1.insert(0, 1)
,您正在解决 IntVar
的问题,因此它的值永远不会更新为初始值。相反,使用 startD.set(1)
。 startM
、startY
、endD
、endM
和 endY
.
相同
我正在尝试创建一个程序,让用户 select 特定的时间范围并显示相关的股票价格图。但是现在即使我输入了一些正常的数字,Python 也只会 return 这个错误信息。非常感谢任何帮助。
第二个代码块是我的按钮和条目。理想情况下,在输入开始日期和结束日期后,单击提交按钮后,应该触发第一个代码块,即显示功能。
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()
通过执行 entry1.insert(0, 1)
,您正在解决 IntVar
的问题,因此它的值永远不会更新为初始值。相反,使用 startD.set(1)
。 startM
、startY
、endD
、endM
和 endY
.