JanvE
Posts: 4
Joined: Sun Feb 11, 2018 3:21 pm

Update Image in Tkinter

Sun Feb 11, 2018 3:51 pm

Hi all,
I am really a new with Python programming but managed so far. I am trying to update an image with Tkinter & Python. I found a program that does this but only after pressing the enter button. Now I am looking for a solution that not the enter key (line 9) must be pressed but that the program wait for 3 seconds and then update the image.
I tried to place the 'self.change.Img' without the button commands but that gives an error. I hope that someone can point me in the right directions.

Thanks for any help.

from tkinter import *

class Testi():
def __init__(self):
self.canvas = Canvas(root, width = 300, height = 100)
self.img = PhotoImage(file="2.png")
self.imgArea = self.canvas.create_image(0, 0, anchor = NW, image = self.img)
self.canvas.pack()
self.but1 = Button(root, text="press me", command=lambda: self.changeImg())
self.but1.place(x=10, y=500)


def changeImg(self):
self.img = PhotoImage(file="2.png")
self.canvas.itemconfig(self.imgArea, image = self.img)


root = Tk()
root.geometry("800x600")
app = Testi()

root.mainloop()

scotty101
Posts: 2798
Joined: Fri Jun 08, 2012 6:03 pm

Re: Update Image in Tkinter

Sun Feb 11, 2018 8:32 pm

Code: Select all

self.after(2000, changeImg)
Stick that in your init method.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

JanvE
Posts: 4
Joined: Sun Feb 11, 2018 3:21 pm

Re: Update Image in Tkinter

Mon Feb 12, 2018 11:11 am

Dear Scotty101,
Thanks for your answer. Unfortunately I don't understand how to put this in the init section. I googled and found out that it is possible to give extra parameters in the __init__ section but I can't find how to place self.after(2000, changeImg) in this section. Can you please be so kind to point out how to place this code in the program. From there I am able to finish te rest of my program. I really appreciate your help.

Regards,
Jan van Eeden
Noordwijk
The Netherlands

scotty101
Posts: 2798
Joined: Fri Jun 08, 2012 6:03 pm

Re: Update Image in Tkinter

Mon Feb 12, 2018 11:51 am

Something like this should work. After 2000ms (2 seconds) the changeImg function will be called.
(I've changed it from self.after to root.after since you haven't written your class in the 'traditional' way for a tkinter application.

Code: Select all

from tkinter import *

class Testi():
    def __init__(self):
        self.canvas = Canvas(root, width = 300, height = 100)
        self.img = PhotoImage(file="2.png")
        self.imgArea = self.canvas.create_image(0, 0, anchor = NW, image = self.img)
        self.canvas.pack()
        self.but1 = Button(root, text="press me", command=lambda: self.changeImg())
        self.but1.place(x=10, y=500)
        root.after(2000,changeImg)


    def changeImg(self):
        self.img = PhotoImage(file="2.png")
        self.canvas.itemconfig(self.imgArea, image = self.img)


root = Tk()
root.geometry("800x600")
app = Testi()

root.mainloop()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 2 guests