I'm trying to show graph with pandas and FigureCanvasTkAgg by ttk.spinbox.
my code looks like getting increse or decrease event infinitly. so if I click button for event, spinbox's value increases(or decr) until the end of it's range.
but sometimes it works as I wanted, even I didn't touch anything about this
whats the problem of this? is there any idea to limit to getting event for a moment? I've tried to give a term to get next event, but it just wait for a term and works infinitly..
from tkinter import *
import tkinter.ttk as ttk
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class Data:
def __init__(self, data):
self.__data = data
@property
def data(self):
return self.__data
@data.setter
def data(self, data):
self.__data = data
root = Tk()
frame_spinbox = Frame(root)
frame_spinbox.pack()
frame_fig = Frame(root)
frame_fig.pack()
spinbox = ttk.Spinbox(frame_spinbox, from_=-10, to=10, validate='key')
spinbox.pack()
data_index = Data(1)
spinbox.set(data_index.data)
figure = plt.Figure(figsize=(5,4), dpi=100)
ax = figure.add_subplot(111)
line = FigureCanvasTkAgg(figure, frame_fig)
line.get_tk_widget().pack(side="left", fill="both")
data = {1 : {"a1" : 11,
"b1" : 12,
"c1" : 13},
2 : {"a2" : 21,
"b2" : 22,
"c2" : 23},
3 : {"a3" : 31,
"b3" : 32,
"c3" : 33}}
def sb_Incr(event, data_index):
print("sb_Incr")
data_index.data += 1
return
def sb_Decr(event, data_index):
print("sb_Decr")
data_index.data -= 1
return
def cmd_Update(ax, figure, data, data_index, sb):
print("cmd_Update")
print("data_index :", data_index.data)
sb.set(data_index.data)
try:
selected_data = data[data_index.data]
except:
print("key Error Occured")
return
df = pd.DataFrame(selected_data, index=[0]).T
print(df)
if df.empty:
print("Empy_DataFrame------")
return
ax.clear()
ax.bar(x=df.index, height=df[0], width=.5, label=df[0])
figure.canvas.draw()
figure.canvas.flush_events()
return
cmd_Update(ax, figure, data, data_index, spinbox)
spinbox.bind("<<Increment>>", lambda x, y=data_index : sb_Incr(x, y))
spinbox.bind("<<Decrement>>", lambda x, y=data_index : sb_Decr(x, y))
spinbox.config(command=lambda w=ax, x=figure, y=data, z=data_index : cmd_Update(w,x,y,z, spinbox))
root.mainloop()