Web Analytics Made Easy - Statcounter
Skip to content

tkinter Canvas Widget

The Canvas Element is one of the few tkinter objects that are directly accessible. The tkinter Canvas widget itself can be retrieved from a Canvas Element like this:

    can = sg.Canvas(size=(100,100))      
    tkcanvas = can.TKCanvas      
    tkcanvas.create_oval(50, 50, 100, 100)      

While it's fun to scribble on a Canvas Widget, try Graph Element makes it a downright pleasant experience. You do not have to worry about the tkinter coordinate system and can instead work in your own coordinate system.

canvas

    import PySimpleGUI as sg      

    layout = [      
        [sg.Canvas(size=(100, 100), background_color='red', key= 'canvas')],      
        [sg.T('Change circle color to:'), sg.Button('Red'), sg.Button('Blue')]      
        ]      

    window = sg.Window('Canvas test', layout, finalize=True)      

    canvas = window['canvas')      
    cir = canvas.TKCanvas.create_oval(50, 50, 100, 100)      

    while True:      
        event, values = window.read()      
        if event == sg.WIN_CLOSED:      
            break      
        if event == 'Blue':      
            canvas.TKCanvas.itemconfig(cir, fill="Blue")      
        elif event == 'Red':      
            canvas.TKCanvas.itemconfig(cir, fill="Red")