Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

debugging - python - Tkinter flickering canvas/rectangles in my sorting visualizer

Im trying to make a sorting visualizer and everything is going smoothly, but i have a problem. First of all, i know that there's a lot of similar post but not a single one that can help me. I have this problem that when the size of the sorting data is to high(>40) the screen starts to flicker when I run the sorting algorithm

Here's a video I recorded with the problem i have

I have some ideas on what the problem might be, but since im kinda new to tkinter with python im struggling to find out what exactly is. Here's my python code on the graphing/drawing data function

def runSort():
    global data
    data=Algorithm.bubblesort(data, graphData)
    print("Sorting...")

def graphData(data, colorData):
    canvas.delete("all")
    can_height = 530
    can_width  = 1177
    x_width    = can_width/(len(data)+1)
    offset=5
    spacing=10
    normalized_data=[i /max(data) for i in data]
    for i, height in enumerate(normalized_data):
        x0=i*x_width+offset+spacing
        y0=can_height- height * 480

        x1=(i+1)*x_width+offset
        y1=can_height

        canvas.create_rectangle(x0,y0,x1,y1,fill=colorData[i])
        if(len(data)<=50):
            canvas.create_text(x0+2,y0, anchor=SW,text=str(data[i]))
        root.update_idletasks()

And here it is my sorting algorithm code

import time

class Algorithm():
    def bubblesort(array, drawData):
        for i in range(len(array)):
            for j in range(len(array)-1-i):
                if array[j] > array[j+1]:
                    array[j], array[j+1] = array[j+1], array[j]
                    drawData(array, ["cyan2" if x==j or x==j+1 else "coral3" for x in range(len(array))])
                    if len(array)<50:
                        time.sleep(0.5)
                    else:
                        time.sleep(0.1)


        drawData(array, ["OliveDrab2" for x in range(len(array))])
        return array

I hope someone can help me because I have no idea on how to solve this, or if it is possible. If you need more of my code be free to ask and i will gladly give you my code. Thanks! and Greetings from Mexico.

question from:https://stackoverflow.com/questions/66057786/python-tkinter-flickering-canvas-rectangles-in-my-sorting-visualizer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...