2

I'm having a little problem with this code! Basically, I'm testing a feature for a piece of software, and the idea was to animate these windows. So far, it's working fine. When the top window expands, the bottom one shrinks. However, this creates a small bug in frame 2. It kind of flickers, and this happens simply because the position doesn't keep up with the window's expansion and vice versa. However, I haven't been able to find a solution to this issue yet, so I decided to ask the community for help. I've uploaded a video to YouTube so you can visualize the bug and understand it better (https://youtu.be/AGQQ_u_qvD4).

import customtkinter as CTk

main_layout = CTk.CTk()
main_layout.geometry(f"{600}x{350}")

h1, h2 = 50, 170
y2 = 150
animation_running = False

def start_animation(to_frame):
    global h1, h2, y2, animation_running
    if animation_running:
        return
    
    animation_running = True

    def step():
        global h1, h2, y2, animation_running
        done = False

        if to_frame and h1 < 170:
            y2 += 10
            h1 += 10
            h2 -= 10
        elif not to_frame and h2 < 170:
            y2 -= 10
            h1 -= 10
            h2 += 10
        else:
            done = True

        frame_1.configure(height=h1)
        frame_2.configure(height=h2)
        frame_2.place(y=y2)

        if not done:
            main_layout.after(10, step)
        
        else:
            animation_running = False
    
    step()

frame_1 = CTk.CTkFrame(master=main_layout, width=240, height=h1, corner_radius=25)
frame_1.place(x=40, y=80)

button1= CTk.CTkButton(master=frame_1, text='1', width=20, height=20, command=lambda:start_animation(True))
button1.place(x=0, y=0)

frame_2 = CTk.CTkFrame(master=main_layout, width=240, height=h2, corner_radius=25)
frame_2.place(x=40, y=y2)

button2= CTk.CTkButton(master=frame_2, text='2', width=20, height=20, command=lambda:start_animation(False))
button2.place(x=0, y=0)

main_layout.mainloop()
11
  • next time put starting ``` in separate line to correctly format and highlight code because text directly after ``` is treated as information what language was used in code - ie. ```python or ```html to select python or html for highlightning. Commented Sep 29 at 23:32
  • next time you could remove repeating empty lines - to make code shorter. Commented Sep 29 at 23:32
  • maybe you could create video only with CTk window without IDE. And later you could convert it to animated GIF - and it allows to put it directly in question instead of link to external server. Commented Sep 29 at 23:34
  • 2
    I also tested these options and the result is the same, it seems to be a limitation of the library itself, however, I decided to disable the animation of frame 2, frame 1 still remains animated, but frame 2 expands instantly, the bug disappears and it looks relatively interesting in my opinion. Commented Sep 30 at 13:56
  • 1
    if you found method which works for you then maybe you could describe it as answer below - maybe it would be useful for others. Commented Sep 30 at 14:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.