0

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()
H.P Na
  • 1
  • 1
  • That would help if you could transform your code samples into a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), that is a simplified version of your project that can be *run* and reproduces your issue without containing the irrelevant parts. – j_4321 Mar 10 '21 at 10:43
  • thanks for your advice! I edit this with a sample what i want to do. its still pretty long but this might be the one what i want to show.. – H.P Na Mar 13 '21 at 08:27
  • This is indeed really weird, the problem does not always happen and I don't understand why. However, I don't get why you need both to set the `command` of the spinbox and use bindings to `"<>"` and `"<>"`, you could just get the new value of the spinbox in `cmd_Update()` with `.get()` and update `data_index.data` accordingly. – j_4321 Mar 13 '21 at 22:01

0 Answers0