3

Tkinter を使用してデータ ポイントを視覚化しています。私の問題は、データ ポイントをキャンバスの中央に表示できず、キャンバスが十分に大きいことです。

キャンバスの見栄えを良くするために、約(単位はピクセル800*600だと思います)に固定したいと思います。だから私は次のことをしました:

class DisplayParticles(Canvas):
    def __init__(self):
        # Canvas
        Canvas.__init__(self)
        self.configure(width=800, height=600)
        # Particles
        self.particle_radius = 1
        self.particle_color = 'red'
        # User
        self.user_radius = 4
        self.user_color = 'blue'
        self.ghost_color = None

ただし、プロットするデータはメートル単位です。さらに、それらは原点 を中心にしています。(0, 0)つまり、との両方に負の座標xがありyます。

次に、それらをキャンバスにプロットすると、次のようになります

ここに画像の説明を入力

明らかに、データ ポイントはピクセル単位でプロットされています。

キャンバスが画面上で十分に大きく、その間にデータがキャンバスを中心とした適切な縮尺でプロットされることを望みます。(自分の原点(0, 0)をキャンバスの中心に置く)

どうすればいいですか?

4

3 に答える 3

-1

キャンバスは、描画したものに合わせて自動的に拡大縮小されません。適切なサイズを把握して設定する必要があります。

また、キャンバスの座標は常に左上隅の (0, 0) から始まります。これを変更する方法はありません。つまり、キャンバスにプロットするすべてのポイントを変換する必要があります。幸いなことに、それは簡単です。

width = ...   # Determine the correct width
height = ...  # Determine the correct height
self.configure(width=width, height=height)

coords = (-20, -30, 10, 60)  # The coordinates of a shape you want to draw

# add width/2 and height/2 to x and y coordinates respectively so that the (0, 0) coordinate is shifted to the center of the canvas:
newCoords = (coords[0]+width/2, coords[1]+height/2, coords[2]+width/2, coords[3]+height/2)  

self.create_oval(*newCoords)  # Create the translated shape
于 2013-10-25T09:56:12.547 に答える