使用关键字yield来使用tkinter可视化快速排序的问题?

问题描述 投票:0回答:1

对于我的课堂项目,我必须实现快速排序并使用带有tkinter的图形界面对其进行可视化。

但是我对关键字yield存有疑问,这对于交流的动画至关重要。当我单击“快速排序”按钮以​​调用我的排序功能时,

出现错误消息:

TypeError: unsupported operand type (s) for -: 'generator' and 'int'

但是当我删除yield时,该程序可以正常工作,除了动画没有完成。

您能帮我吗?

import tkinter as tk
import random

#QUICK SORT
def partition(low, high):
    i = (low - 1)  # index of smaller element
    pivot = longueur_liste[high]  # pivot

    for j in range(low, high):

        # If current element is smaller than or
        # equal to pivot
        if longueur_liste[j] <= pivot:
            # increment index of smaller element
            i = i + 1

            longueur_liste[i], longueur_liste[j] = longueur_liste[j], longueur_liste[i]
            barre_liste[i], barre_liste[j ] = barre_liste[j ], barre_liste[i]
            echange_position(barre_liste[i], barre_liste[j ])  # Met à jour l'affichage
            yield
    longueur_liste[i + 1], longueur_liste[high] = longueur_liste[high], longueur_liste[i + 1]
    barre_liste[i+1], barre_liste[high] = barre_liste[high], barre_liste[i+1]
    echange_position(barre_liste[i+1], barre_liste[high])  # Met à jour l'affichage
    yield
    return i + 1


# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low  --> Starting index,
# high  --> Ending index

# Function to do Quick sort
def quickSort(low, high):
    if low < high:
        # pi is partitioning index, arr[p] is now
        # at right place

        pi = partition(low, high)


        # Separately sort elements before
        # partition and after partition
        quickSort(low, pi - 1)
        quickSort(pi + 1, high)




def _quickSort():     # Command start sorting and animation
    global utilisateur
    utilisateur = quickSort(0, 78)
    animate()

# ECHANGE POSITION
def echange_position(pos_0, pos_1):
    Bar_1_1, _, Bar_1_2, _ = canvas.coords(pos_0)
    Bar_2_1, _, Bar_2_2, _ = canvas.coords(pos_1)
    canvas.move(pos_0, Bar_2_1 - Bar_1_1, 0)
    canvas.move(pos_1, Bar_1_2 - Bar_2_2, 0)

# ANIMATION
def animate():
    global utilisateur
    if utilisateur is not None:
        try:
            next(utilisateur)
            window.after(10, animate)    # Repeat until sorting is complete
        except StopIteration:            # Repeat until sorting is complete
            utilisateur = None
        finally:
            window.after_cancel(animate) # Stop reminders

# GENERATION OF RANDOM VALUES BETWEEN 1 AND 390
def generer():
    global barre_liste
    global longueur_liste
    canvas.delete('all')
    x_start = 5
    x_end = 15
    barre_liste = []
    longueur_liste = []

    for x in range(1, 80):
        random_Y = random.randint(1, 400)
        x = canvas.create_rectangle(x_start, random_Y, x_end, 450, fill='red')
        barre_liste.append(x)
        x_start += 10
        x_end += 10

    for barre in barre_liste:
        x = canvas.coords(barre)
        length = x[3] - x[1]
        longueur_liste.append(length)

    for i in range(len(longueur_liste) - 1):
        if longueur_liste[i] == min(longueur_liste):
            canvas.itemconfig(barre_liste[i], fill='blue')
        elif longueur_liste[i] == max(longueur_liste):
            canvas.itemconfig(barre_liste[i], fill='yellow')


window = tk.Tk()
window.title('Algorithmes de tri')
window.geometry('820x485')
canvas = tk.Canvas(window, width='820', height='450')
canvas.grid(column=0,row=0, columnspan = 50)
gen = tk.Button(window, text='generate', command=generer)
quick = tk.Button(window, text='Quicksort', command=_quickSort)
gen.grid(column=0, row=1)
quick.grid(column=1, row=1)
generer()
window.mainloop()
python generator yield
1个回答
0
投票

我不太理解yield在此特定示例中的功能,但是我很确定您可以通过首先了解yield来解决问题。为此,我强烈建议您阅读this-并且您没有借口不这样做,这是法语:p

© www.soinside.com 2019 - 2024. All rights reserved.