我一直在努力創建一個 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409455.html
標籤:
