[Tutor] Tkinter and canvas question

Phil phil_lor at bigpond.com
Mon Apr 17 19:13:21 EDT 2017


Thank you for reading this.

How do I reference the_canvas from my solve() method? Despite hours of searching I haven't been able to solve this or find a similar example. All that I've gained is a headache.

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
    return self.func(*args)
  File "/home/pi/sudoku.py", line 64, in solve
    self.the_canvas.create_text(20,20,text="5")
AttributeError: 'Sudoku' object has no attribute 'the_canvas'

from tkinter import *

class Sudoku(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent

        parent.title("Sudoku solver")    
        
    	#create canvas
        the_canvas = Canvas(width = 300, height = 300)                                         
        the_canvas.pack(side = TOP, anchor = NW, padx = 10, pady = 10)

    	#create grid
                
        #create solve button
        solve_button = Button(the_canvas, text = "Solve", command = self.solve,
                                                                anchor = W)
        solve_button.configure(width = 5, activebackground = "#33B5E5",
                                                            relief = FLAT)
        solve_button.pack(side = TOP)
        solve_button_window = the_canvas.create_window(250, 250, anchor=NW, window=solve_button)

    def solve(self):
        print("solve called")
        self.the_canvas.create_text(20,20,text="5")
            

def main():
    root = Tk()
    app = Sudoku(root)
    app.mainloop()

if __name__ == '__main__':
    main()


-- 
Regards,
Phil


More information about the Tutor mailing list