[Tutor] Tkinter class question

Phil phil_lor at bigpond.com
Fri Apr 7 12:00:38 EDT 2017


Thank you for reading this.

I've progressed a little further but I'm now having a problem knowing when to use the "self" reference. In the following code, the function "ckeck" is called without the need to press the "check" button. This didn't occur before I sprinkled "selfs" into the code and added "array" to the "ckeck" function. I found that I needed "self" to point "array" to my list array "e" and I think that is where the fault is. 

from tkinter import *

class TestGUI:
    def __init__(self, master):
        self.master = master
        master.title("Testing")

        num_rows = 6
        num_cols = 3

        self.e = [[None] * num_cols for _ in range(num_rows)]

        for i in range(num_rows):
            for j in range(num_cols):
                self.e[i][j] = Entry(master, width=4, justify=CENTER, foreground="gray")
                self.e[i][j].grid(row=i, column=j)
                self.e[i][j].insert(0,"6")

        self.check_button = Button(master, text="Check", command=self.check(self.e))
        self.check_button.grid(row=7, column=7)
        
    def check(self, array):
        print("checked")
        array[2][2].insert(0, "4")
        
root = Tk()
my_gui = TestGUI(root)
root.mainloop()


-- 
Regards,
Phil


More information about the Tutor mailing list