0

Tkinter を使用して Python で記述されたグラフィカルな解析アプリケーションを維持しています。オプションで、Canvas ウィジェットを使用して作成したスクロール可能なスプレッドシートに結果を表示します。ただし、スケーラビリティに問題があるようです。メイン キャンバスが大きすぎる場合、結果が表示されるウィンドウを破棄しようとすると、プログラムが応答を停止し、強制終了する必要があります。表示されるデータが少ない場合は、通常どおり破棄できます。これは Tkinter/大きな Canvi の既知の問題ですか? これらのフリーズを防ぐために何かできることはありますか?

#Function being called to destroy window
def justexit():
    savepopup.destroy() #Destroy the popup asking to save (that called this function)
    deleteall() #Destroy canvi and their contents
    popup.destroy() #Destroy the window containing all the canvi

def deleteall():
    for i, itemset in enumerate(items): #Items stores lists containing every item added to the canvi
        for item in itemset:
            canvi[i].delete(item)
        canvi[i].destroy()

そして、私の見積もりは少し外れていました。実際には何万ものアイテムがプレイされていますが、スローダウンがこれほど明白になるとは思いません.

4

1 に答える 1

1

はい、キャンバスウィジェットにはスケーラビリティの問題があります。通常、これは、キャンバスに数万のアイテムが存在するまで開始されません。アイテムはいくつありますか?

キャンバスを削除する前にアイテムを削除すると、処理が速くなる可能性があると聞いたのをぼんやりと覚えています。でも、それが事実かどうかはわかりません。

次のコードを実行してみてください。5000個のランダムな長方形を作成します。削除ボタンをクリックすると、キャンバスウィジェットが削除されます。私のマシンでは、これは瞬時に行われます。

import Tkinter as tk
import random

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.wm_geometry("400x400")
        self.del_button = tk.Button(text="delete", command=self.delete)
        self.del_button.pack()
        self.create_button = tk.Button(text="create", command=self.create)
        self.create_button.pack()
        self.canvas = None
        self.create()

    def create(self):
        if self.canvas is not None:
            self.canvas.destroy()
        self.canvas = tk.Canvas(self)
        self.canvas.pack(side="top", fill="both", expand=True)
        colors = ["red","orange","green","blue", "violet","bisque","white"]
        for i in range(5000):
            x0 = random.random()*400
            y0 = random.random()*400
            width = random.randint(10,100)
            height = random.randint(10,100)
            x1 = x0 + width
            y1 = y0 + height
            color = colors.pop(0)
            colors.append(color)
            self.canvas.create_rectangle(x0,y0,x1,y1, outline="black", fill=color)
        
    def delete(self):
        self.canvas.destroy()
        self.canvas = None

app = SampleApp()
app.mainloop()
于 2012-06-19T21:54:57.267 に答える