运行 一个 tkinter 按钮上的两个函数,得到一个 tkinter 回调错误
Running Two Functions on one tkinter button, Getting a tkinter callback error
我一直致力于创建一个 matplotlib 图,您可以在 tkinter GUI 中动态调整它。要更改图表的各个方面,例如我计划使用可以更改显示的蜡烛数以及给定数据集中显示哪些蜡烛的按钮的视图框架。我的代码没有折射,因为我已经改变了很多东西但它粘贴在下面。
# Defining the chart
chart = Frame(charting)
width=.6
figure = Figure( figsize=(6,4),dpi=100)
canvas = FigureCanvasTkAgg(figure, chart)
canvas.get_tk_widget().pack(side=TOP, fill=BOTH)
plot = figure.add_axes([0.07,0.15,.8,.8])
candleCount = 30
startCandle = 0
def loadChart() :
chartData = loadData(ticker.get(),timeframe.get(),length.get())["chart"]
plot.clear()
plot.bar(x=chartData["positions"][startCandle:startCandle+candleCount],height=chartData["hlheights"][startCandle:startCandle+candleCount],bottom=chartData["hlbottoms"][startCandle:startCandle+candleCount],color="black",width=width/15)
plot.bar(x=chartData["positions"][startCandle:startCandle+candleCount],height=chartData["ocheights"][startCandle:startCandle+candleCount],bottom=chartData["ocbottoms"][startCandle:startCandle+candleCount],color=chartData['colors'][startCandle:startCandle+candleCount],width=width)
plot.set_xticks(range(candleCount))
mplcursors.cursor(plot).connect("add", lambda sel: sel.annotation.set_text(chartData["summary"][sel.index]))
plot.set_xticklabels(chartData["labels"][startCandle:startCandle+candleCount],rotation=65)
figure.canvas.draw()
print("Chart Loaded")
loadChart()
# Place Chart buttons
def shiftChart(shift=1) :
global startCandle
startCandle += shift
print("shifted Chart")
def zoomChart(zoom=1) :
global candleCount
candleCount += zoom
print("zoomed Chart")
leftButton = Button(chart,text = "<<",command=lambda:[shiftChart(-1),loadChart()]).pack(side=LEFT)
rightButton = Button(chart,text=">>",command=lambda:[shiftChart(1),loadChart()]).pack(side=LEFT)
zoomoutButton = Button(chart,text="-",command=lambda:[zoomChart(-1),loadChart()]).pack(side=LEFT)
zoominButton = Button(chart,text="+",command=lambda:[zoomChart(1),loadChart()]).pack(side=LEFT)
loadChart = Button(chartingInput,text="Load Chart",command=loadChart)
loadChart.grid(row=2)
charting.add(chart)
charting.add(chartingInput)
如果您想在此处测试代码,可以查看用于构建图表的股票数据示例
[(1640077200000, 171.84, 171.84, 171.6, 171.83, 1894), (1640077260000, 171.64, 171.8, 171.64, 171.8, 1007), (1640077320000, 171.68, 171.68, 171.28, 171.59, 2048), (1640077380000, 171.64, 171.7, 171.64, 171.7, 6521), (1640077500000, 171.69, 171.75, 171.69, 171.75, 823), (1640077560000, 171.81, 171.81, 171.81, 171.81, 476), (1640077620000, 171.8, 171.8, 171.8, 171.8, 625), (1640077740000, 171.78, 171.8, 171.78, 171.8, 2854), (1640077800000, 171.71, 171.71, 171.67, 171.67, 647)]
当我 运行 代码正确加载图表时,但是当尝试使用按钮更改图表的某个方面(例如视图框架或显示的蜡烛数)时出现此错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:<my file path>", line 1884, in __call__
return self.func(*args)
File "c:<my file path>", line 85, in <lambda>
leftButton = Button(chart,text = "<<",command=lambda:[shiftChart(-1),loadChart()]).pack(side=LEFT)
TypeError: 'Button' object is not callable
我似乎无法弄清楚如何编写代码来在单击按钮时接受这两个函数,并且将 loadChart 函数放在其他两个函数中的任何一个中都会产生相同的错误。
您已通过以下行覆盖函数 loadChart()
:
loadChart = Button(chartingInput,text="Load Chart",command=loadChart)
为按钮使用其他名称:
loadChartBtn = Button(chartingInput,text="Load Chart",command=loadChart)
loadChartBtn.grid(row=2)
我一直致力于创建一个 matplotlib 图,您可以在 tkinter GUI 中动态调整它。要更改图表的各个方面,例如我计划使用可以更改显示的蜡烛数以及给定数据集中显示哪些蜡烛的按钮的视图框架。我的代码没有折射,因为我已经改变了很多东西但它粘贴在下面。
# Defining the chart
chart = Frame(charting)
width=.6
figure = Figure( figsize=(6,4),dpi=100)
canvas = FigureCanvasTkAgg(figure, chart)
canvas.get_tk_widget().pack(side=TOP, fill=BOTH)
plot = figure.add_axes([0.07,0.15,.8,.8])
candleCount = 30
startCandle = 0
def loadChart() :
chartData = loadData(ticker.get(),timeframe.get(),length.get())["chart"]
plot.clear()
plot.bar(x=chartData["positions"][startCandle:startCandle+candleCount],height=chartData["hlheights"][startCandle:startCandle+candleCount],bottom=chartData["hlbottoms"][startCandle:startCandle+candleCount],color="black",width=width/15)
plot.bar(x=chartData["positions"][startCandle:startCandle+candleCount],height=chartData["ocheights"][startCandle:startCandle+candleCount],bottom=chartData["ocbottoms"][startCandle:startCandle+candleCount],color=chartData['colors'][startCandle:startCandle+candleCount],width=width)
plot.set_xticks(range(candleCount))
mplcursors.cursor(plot).connect("add", lambda sel: sel.annotation.set_text(chartData["summary"][sel.index]))
plot.set_xticklabels(chartData["labels"][startCandle:startCandle+candleCount],rotation=65)
figure.canvas.draw()
print("Chart Loaded")
loadChart()
# Place Chart buttons
def shiftChart(shift=1) :
global startCandle
startCandle += shift
print("shifted Chart")
def zoomChart(zoom=1) :
global candleCount
candleCount += zoom
print("zoomed Chart")
leftButton = Button(chart,text = "<<",command=lambda:[shiftChart(-1),loadChart()]).pack(side=LEFT)
rightButton = Button(chart,text=">>",command=lambda:[shiftChart(1),loadChart()]).pack(side=LEFT)
zoomoutButton = Button(chart,text="-",command=lambda:[zoomChart(-1),loadChart()]).pack(side=LEFT)
zoominButton = Button(chart,text="+",command=lambda:[zoomChart(1),loadChart()]).pack(side=LEFT)
loadChart = Button(chartingInput,text="Load Chart",command=loadChart)
loadChart.grid(row=2)
charting.add(chart)
charting.add(chartingInput)
如果您想在此处测试代码,可以查看用于构建图表的股票数据示例
[(1640077200000, 171.84, 171.84, 171.6, 171.83, 1894), (1640077260000, 171.64, 171.8, 171.64, 171.8, 1007), (1640077320000, 171.68, 171.68, 171.28, 171.59, 2048), (1640077380000, 171.64, 171.7, 171.64, 171.7, 6521), (1640077500000, 171.69, 171.75, 171.69, 171.75, 823), (1640077560000, 171.81, 171.81, 171.81, 171.81, 476), (1640077620000, 171.8, 171.8, 171.8, 171.8, 625), (1640077740000, 171.78, 171.8, 171.78, 171.8, 2854), (1640077800000, 171.71, 171.71, 171.67, 171.67, 647)]
当我 运行 代码正确加载图表时,但是当尝试使用按钮更改图表的某个方面(例如视图框架或显示的蜡烛数)时出现此错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:<my file path>", line 1884, in __call__
return self.func(*args)
File "c:<my file path>", line 85, in <lambda>
leftButton = Button(chart,text = "<<",command=lambda:[shiftChart(-1),loadChart()]).pack(side=LEFT)
TypeError: 'Button' object is not callable
我似乎无法弄清楚如何编写代码来在单击按钮时接受这两个函数,并且将 loadChart 函数放在其他两个函数中的任何一个中都会产生相同的错误。
您已通过以下行覆盖函数 loadChart()
:
loadChart = Button(chartingInput,text="Load Chart",command=loadChart)
为按钮使用其他名称:
loadChartBtn = Button(chartingInput,text="Load Chart",command=loadChart)
loadChartBtn.grid(row=2)