Replace Canvas Image in Tkinter











up vote
0
down vote

favorite
1












I have a program and I want when someone clicks a button, the canvas image will change. My code is below:



from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END


root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)

im=Image.open("red.jpg")
photo=ImageTk.PhotoImage(im)
cv = tkinter.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')

def changepic():
###place where I want to change the Canvas Image
print("change color")#I added this because python wouldn't let me run thee function without something.


a2=tkinter.Button(root,text='change color',bd=0, command=changepic)
a2.config(highlightbackground='black')
a2.place(x=135, y=70)









share|improve this question









New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Why a canvas image instead of a standard Label image?
    – Novel
    Nov 17 at 20:38










  • I got it, thanks Novel for the idea of using a standard Label image!
    – Peppa
    Nov 18 at 20:08















up vote
0
down vote

favorite
1












I have a program and I want when someone clicks a button, the canvas image will change. My code is below:



from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END


root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)

im=Image.open("red.jpg")
photo=ImageTk.PhotoImage(im)
cv = tkinter.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')

def changepic():
###place where I want to change the Canvas Image
print("change color")#I added this because python wouldn't let me run thee function without something.


a2=tkinter.Button(root,text='change color',bd=0, command=changepic)
a2.config(highlightbackground='black')
a2.place(x=135, y=70)









share|improve this question









New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Why a canvas image instead of a standard Label image?
    – Novel
    Nov 17 at 20:38










  • I got it, thanks Novel for the idea of using a standard Label image!
    – Peppa
    Nov 18 at 20:08













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have a program and I want when someone clicks a button, the canvas image will change. My code is below:



from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END


root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)

im=Image.open("red.jpg")
photo=ImageTk.PhotoImage(im)
cv = tkinter.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')

def changepic():
###place where I want to change the Canvas Image
print("change color")#I added this because python wouldn't let me run thee function without something.


a2=tkinter.Button(root,text='change color',bd=0, command=changepic)
a2.config(highlightbackground='black')
a2.place(x=135, y=70)









share|improve this question









New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a program and I want when someone clicks a button, the canvas image will change. My code is below:



from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END


root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)

im=Image.open("red.jpg")
photo=ImageTk.PhotoImage(im)
cv = tkinter.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')

def changepic():
###place where I want to change the Canvas Image
print("change color")#I added this because python wouldn't let me run thee function without something.


a2=tkinter.Button(root,text='change color',bd=0, command=changepic)
a2.config(highlightbackground='black')
a2.place(x=135, y=70)






python python-3.x tkinter python-imaging-library tkinter-canvas






share|improve this question









New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 18 at 19:20





















New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 17 at 19:50









Peppa

286




286




New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Why a canvas image instead of a standard Label image?
    – Novel
    Nov 17 at 20:38










  • I got it, thanks Novel for the idea of using a standard Label image!
    – Peppa
    Nov 18 at 20:08














  • 1




    Why a canvas image instead of a standard Label image?
    – Novel
    Nov 17 at 20:38










  • I got it, thanks Novel for the idea of using a standard Label image!
    – Peppa
    Nov 18 at 20:08








1




1




Why a canvas image instead of a standard Label image?
– Novel
Nov 17 at 20:38




Why a canvas image instead of a standard Label image?
– Novel
Nov 17 at 20:38












I got it, thanks Novel for the idea of using a standard Label image!
– Peppa
Nov 18 at 20:08




I got it, thanks Novel for the idea of using a standard Label image!
– Peppa
Nov 18 at 20:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:



from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END


root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)

img = ImageTk.PhotoImage(Image.open("red.jpg"))
panel = tkinter.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")

def changepic(imagename):
img2 = ImageTk.PhotoImage(Image.open(imagename))
panel.configure(image=img2)
panel.image = img2


a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
a2.config(highlightbackground='black')
a2.place(x=135, y=70)


I got my information from: How to update the image of a Tkinter Label widget?



And: https://www.tutorialspoint.com/python/tk_label.htm






share|improve this answer










New contributor




Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Peppa is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354959%2freplace-canvas-image-in-tkinter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:



    from PIL import ImageTk,Image, ImageFont, ImageDraw
    import tkinter
    import textwrap
    from tkinter import Frame, Canvas, Text, INSERT, END


    root = tkinter.Tk()
    root.geometry("296x337")
    root.resizable(False, False)

    img = ImageTk.PhotoImage(Image.open("red.jpg"))
    panel = tkinter.Label(root, image=img)
    panel.pack(side="bottom", fill="both", expand="yes")

    def changepic(imagename):
    img2 = ImageTk.PhotoImage(Image.open(imagename))
    panel.configure(image=img2)
    panel.image = img2


    a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
    a2.config(highlightbackground='black')
    a2.place(x=135, y=70)


    I got my information from: How to update the image of a Tkinter Label widget?



    And: https://www.tutorialspoint.com/python/tk_label.htm






    share|improve this answer










    New contributor




    Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote



      accepted










      Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:



      from PIL import ImageTk,Image, ImageFont, ImageDraw
      import tkinter
      import textwrap
      from tkinter import Frame, Canvas, Text, INSERT, END


      root = tkinter.Tk()
      root.geometry("296x337")
      root.resizable(False, False)

      img = ImageTk.PhotoImage(Image.open("red.jpg"))
      panel = tkinter.Label(root, image=img)
      panel.pack(side="bottom", fill="both", expand="yes")

      def changepic(imagename):
      img2 = ImageTk.PhotoImage(Image.open(imagename))
      panel.configure(image=img2)
      panel.image = img2


      a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
      a2.config(highlightbackground='black')
      a2.place(x=135, y=70)


      I got my information from: How to update the image of a Tkinter Label widget?



      And: https://www.tutorialspoint.com/python/tk_label.htm






      share|improve this answer










      New contributor




      Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:



        from PIL import ImageTk,Image, ImageFont, ImageDraw
        import tkinter
        import textwrap
        from tkinter import Frame, Canvas, Text, INSERT, END


        root = tkinter.Tk()
        root.geometry("296x337")
        root.resizable(False, False)

        img = ImageTk.PhotoImage(Image.open("red.jpg"))
        panel = tkinter.Label(root, image=img)
        panel.pack(side="bottom", fill="both", expand="yes")

        def changepic(imagename):
        img2 = ImageTk.PhotoImage(Image.open(imagename))
        panel.configure(image=img2)
        panel.image = img2


        a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
        a2.config(highlightbackground='black')
        a2.place(x=135, y=70)


        I got my information from: How to update the image of a Tkinter Label widget?



        And: https://www.tutorialspoint.com/python/tk_label.htm






        share|improve this answer










        New contributor




        Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:



        from PIL import ImageTk,Image, ImageFont, ImageDraw
        import tkinter
        import textwrap
        from tkinter import Frame, Canvas, Text, INSERT, END


        root = tkinter.Tk()
        root.geometry("296x337")
        root.resizable(False, False)

        img = ImageTk.PhotoImage(Image.open("red.jpg"))
        panel = tkinter.Label(root, image=img)
        panel.pack(side="bottom", fill="both", expand="yes")

        def changepic(imagename):
        img2 = ImageTk.PhotoImage(Image.open(imagename))
        panel.configure(image=img2)
        panel.image = img2


        a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
        a2.config(highlightbackground='black')
        a2.place(x=135, y=70)


        I got my information from: How to update the image of a Tkinter Label widget?



        And: https://www.tutorialspoint.com/python/tk_label.htm







        share|improve this answer










        New contributor




        Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited Nov 18 at 23:30





















        New contributor




        Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 18 at 20:06









        Peppa

        286




        286




        New contributor




        Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Peppa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            Peppa is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Peppa is a new contributor. Be nice, and check out our Code of Conduct.













            Peppa is a new contributor. Be nice, and check out our Code of Conduct.












            Peppa is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354959%2freplace-canvas-image-in-tkinter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga