简体   繁体   English

在一个 tkinter 按钮上运行两个函数,出现 tkinter 回调错误

[英]Running Two Functions on one tkinter button, Getting a tkinter callback error

I've been working on creating a matplotlib plot that you can dynamically adjust inside of a tkinter GUI.我一直在努力创建一个 matplotlib plot ,您可以在 tkinter GUI 内动态调整它。 To change aspects of the chart such as the view frame I planned on using buttons that can change the number of candles shown, and what candles are shown in a given dataset.要更改图表的各个方面,例如我计划使用可以更改显示的蜡烛数量的按钮的视图框架,以及给定数据集中显示的蜡烛。 My code is not refractored since I've been changing many things on it but it is pasted below.我的代码没有被折射,因为我已经改变了很多东西,但它被粘贴在下面。

# 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)

Incase you would like to test the code here is a sample of the stock data that is used to build the chart如果您想测试这里的代码是用于构建图表的股票数据样本

[(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)]

When I run the code the chart loads correctly, but when attempting to use a button to change an aspect of the chart such as the view frame or the number of candles displayed gives this error当我运行代码时,图表加载正确,但是在尝试使用按钮更改图表的某个方面(例如视图框架或显示的蜡烛数)时会出现此错误

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

I can't seem to figure out how to write the code to accept both functions when the button is clicked, and placing the loadChart function inside of either of the other two functions produces the same error.我似乎无法弄清楚如何编写代码以在单击按钮时接受这两个函数,并将 loadChart function 放在其他两个函数中的任何一个函数中都会产生相同的错误。

You have overwritten function loadChart() by the following line:您已通过以下行覆盖 function loadChart()

loadChart = Button(chartingInput,text="Load Chart",command=loadChart)

Use other name for the button:为按钮使用其他名称:

loadChartBtn = Button(chartingInput,text="Load Chart",command=loadChart)
loadChartBtn.grid(row=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM