0

This is a function that creates an image:

def footgraph(self):

        load = Image.open('centertext_out.png')
        load= load.resize((500, 500), Image.ANTIALIAS)
        render = ImageTk.PhotoImage(load)
        self.img = Label( image=render)
        self.img.image = render
        self.img.place(x=150, y=5)

        self.scale = tk.Scale(self.win, variable=self.value, orient="horizontal",length = 200, 
                         from_=self.df['index'].unique().min(), to=self.df['index'].unique().max(), resolution =1,command=self.updateScaleFoot)

        self.scale.place(x=250, y = 500)
        self.stop.pack(expand='true',fill='both')
        self.stop.place(x=200, y =500)
        self.play.pack(expand='true',fill='both')
        self.play.place(x=150, y = 500)

Here is a code for play button that keeps updating image:

while True:
            self.index += 1
            #update image(centertext_out.png) and save 
            load = Image.open('centertext_out.png')
            load= load.resize((500, 500), Image.ANTIALIAS)
            render = ImageTk.PhotoImage(load)
            img = Label( image=render)
            img.image = render
            img.place(x=150, y=5)
            time.sleep(10)

This loop is working fine. Self.index keeps updating. But image is not updating and screen hangs.

Edit: When I use slider new image gets appended to previous one like this : enter image description here

Edit:

I have narrowed down the problem. Below is the code for play function. When I click bar graph and if for bar graph turns true then code runs smoothly but it doesnt seem to be working for the second statement even if I have nothing inside of it.

 def startanimation(self):
        self.pause = 0 
        print("pause"+ str(self.pause))

        while True:

            if self.pause == 1:
                break
            self.index = self.index +1

            print ("scale is now %s" % (self.index))
            if "bar" in self.graphtype:

                #some code


                #draw canvas
                self.fig.canvas.draw()
                self.fig.canvas.flush_events()
                time.sleep(0.2)
            if "foot" in self.graphtype:

                print("inside")

                time.sleep(0.2)

Edit: Changed code according to one of the answers.

class Application:

    def __init__(self, master):

        self.win = master
        self.geo = self.win.geometry
        self.geo("800x800+400+400")
        self.win['bg'] = "black"
####################################################some code
def startanimation(self):
        self.pause = 0 
        print("pause"+ str(self.pause))
        if "foot" in self.graphtype:
            self.win.after(1,self.test)
def test(self):
            print("hi")
            self.pause = 0
            guide = pd.read_csv("guide.csv")

            print("hey")
            self.index +=1

test is called only once. hey is printed only once

Solved: check this link Python Tkinter after() Only Executing Once

pink puffles
  • 93
  • 1
  • 10
  • Am I understanding this wrong, or are you constantly loading the same image? – Reti43 Mar 22 '20 at 17:00
  • I perform operations to image and then save it with the same name. Each time image is different with same file name – pink puffles Mar 22 '20 at 17:01
  • `sleep` dos exactly what it says: it puts the entire program to sleep, including its ability to refresh the window. – Bryan Oakley Mar 22 '20 at 18:03
  • @BryanOakley I put sleep in other if in the same function but it isnt working. Let me put the entire code – pink puffles Mar 22 '20 at 18:06
  • 1
    Read [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Mar 22 '20 at 18:28
  • @pinkpuffles I didn't get how your edit will improve `def startanimation(...` ? Are your issue solved? – stovfl Mar 22 '20 at 19:39

0 Answers0