Python Forum
[Tkinter] updating tkinter chart from within function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] updating tkinter chart from within function
#1
Hello,

I got a tkinter application with a "chart page".
My user has to click a button to point me to the location of an SPSS file, after which a calculation is triggered.
This calculation returns x- and y-values for a scatter plot, that I want to display to my users.

Now, my best bet was to create an initial blank scatter plot with blank x- and y-values, and then update these values from within my function.
So I first do this:
self.x_values = []
self.y_values = []
And then I do this from within my function:
self.x_values = contacts_main.iloc[:, 1]
self.y_values = contacts_main.iloc[:, 2]
The calculatun runs, but my scatter plot never updates.
Am I doing something wrong (well obviously, but what Smile )?
Is creating a blank scatter plot and then updating the values bad practice?
Any help is vastly appreciated!

Regards,
Mikis

The full code (or at least the part with the relevant page):
class MainProgram(tk.Tk):
    def __init__(self, *args, **kwargs):   
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}
        
        for F in (PageOne, PageTwo, PageThree):
            frame = F(container, self)    
            self.frames[F] = frame
            frame.grid(row=0, column= 0, sticky="nsew")
        
        self.show_frame(PageOne)
        
    def show_frame(self, cont):
        frame_to_activate = self.frames[cont]
        frame_to_activate.tkraise()
        
    def get_page(self, page_class):
        return self.frames[page_class]
    
class PageThree(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.x_values = []
        self.y_values = []
        label = tk.Label(self, text= "Chart page")
        label.pack(pady=10, padx=10)
        button1 = ttk.Button(self, text = "Back to first page",
                            command = lambda: controller.show_frame(PageOne))
        
        launch_calculation = tk.Button(self, text = "generate scatter"
                                        , command = self.contact_cluster)
        f = Figure(figsize=(5,5), dpi=100)
        a = f.add_subplot(111) # meaning there is only one chart
        a.scatter(self.x_values, self.y_values)
        a.set_title ("sales rep contacts vs all promo channel contacts", fontsize=16)
        a.set_ylabel("all promo channel contacts", fontsize=14)
        a.set_xlabel("avg sales rep contacts", fontsize=14)
        canvas = FigureCanvasTkAgg(f, self)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        toolbar = NavigationToolbar2Tk(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        
        button1.pack()
        launch_calculation.pack()
        
    def contact_cluster(self):
        page1 = self.controller.get_page(PageOne) #To get the file location entered on page 1
        df, meta = pyreadstat.read_sav(page1.spss_file_location.get(), encoding='latin1')
        # Run the calculation
        contacts = country_cluster_calc(df)
        # Attempt to update the chart
        self.x_values = contacts .iloc[:, 1]
        self.y_values = contacts .iloc[:, 2]
        
app = MainProgram()
app.geometry("700x650")
app.mainloop()
Reply
#2
How many dots do you expect to see on the scatter plot when this program is run? 2 or 102?
import matplotlib.pyplot as plt
from random import randint

x = [5, 10]
y = [5, 10]
plt.scatter(x, y)
for i in range(100):
    x.append(randint(0,100))
    y.append(randint(0,100))
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 4,858 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  [Tkinter] Updating tkinter text BliepMonster 5 5,661 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,053 Aug-15-2022, 07:10 PM
Last Post: menator01
  Tkinter won't run my simple function AthertonH 6 3,740 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,734 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,416 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,918 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 2,890 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,228 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 4,962 Nov-24-2020, 11:28 PM
Last Post: Dale22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020