我一直在努力创建一个 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 函式放在其他两个函式中的任何一个中都会产生相同的错误。
uj5u.com热心网友回复:
您已loadChart()
通过以下行覆写了函式:
loadChart = Button(chartingInput,text="Load Chart",command=loadChart)
为按钮使用其他名称:
loadChartBtn = Button(chartingInput,text="Load Chart",command=loadChart)
loadChartBtn.grid(row=2)
0 评论