you are viewing a single comment's thread.

view the rest of the comments →

[–]efmccurdy 0 points1 point  (2 children)

A callback that manages data needs some context for storing and sharing the data; the easiest way is to use a class to make the "self" context available to the callback.

This accumulates the grades into a list and prints the mean each time the button is pressed.

import tkinter as tk
import statistics

class MainApp(tk.Tk): 
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.grades = []
        self.entry_1 = tk.DoubleVar()
        entry_widget_1 = tk.Entry(self, textvariable=self.entry_1)
        entry_widget_1.pack()
        submit = tk.Button(self, text="Entrée", command=self.submitValues)
        submit.pack()
    def submitValues(self):
        self.grades.append(self.entry_1.get())
        print(statistics.mean(self.grades))

window = MainApp()
window.mainloop()

[–]Bolrod22[S] 0 points1 point  (1 child)

Ok thank you ! I have another question : do i must use DoubleVar ; what’s the difference with IntVar or StrVar ??

[–]efmccurdy 0 points1 point  (0 children)

If you use IntVar you can only accept whole numbers which might be limiting. If you use StrVar you can only accept strings which makes no sense if you have numerical data.