kucar
kucar

Reputation: 245

Python Tkinter Obtain Some Parameters on Canvas

Hope you are all doing well.

Right now, I created a Tkinter Canvas which displays a resizeable circle object. What I need to do is to print the following parameters on canvas:

Window location (according to the screen of the computer, should be Left and Top)

Center Point of the circle image (according to the window X, Y)

Width and Heigth of the circle image

Here is my code:

from tkinter import *
from PIL import ImageTk, Image

def locator():

    root = Tk()
    root.geometry("300x300")
    root.overrideredirect()

    bg = ImageTk.PhotoImage(file="Golden-circle (1).png")

    root.attributes('-alpha', 0.25)
    my_canvas = Canvas(root, width=300, height=300)
    my_canvas.pack(fill="both", expand=True)
    my_canvas.create_image(350,350, image=bg, anchor="nw",tags="circle")
    #Draw an Oval in the canvas

    def resizer(e):
        global bg1, resized_bg, new_bg
        # Open our image
        bg1 = Image.open("Golden-circle (1).png")
        # Resize the image
        resized_bg = bg1.resize((e.width, e.width), Image.ANTIALIAS)
        #resized_bg = bg1.resize((e.height, e.height), Image.ANTIALIAS) if e.height < e.width else bg1.resize((e.width, e.width), Image.ANTIALIAS)
        # Define our image again
        new_bg = ImageTk.PhotoImage(resized_bg)
        # Add it back to the canvas
        my_canvas.create_image(0,0, image=new_bg, anchor="nw")


    root.bind('<Configure>', resizer)
    root.mainloop()

locator()

What I should need to do in order to retrieve/print those parameters?

Upvotes: 1

Views: 214

Answers (1)

Doyousketch2
Doyousketch2

Reputation: 2147

I don't have that golden circle image that you had, so not entirely certain what it's supposed to look like. I know it's a spiral, but I don't know if you're trying to keep it's aspect-ratio during resize.

Also, I couldn't get it to display on the canvas. Don't know why that would be, but I know how to put images on tk.Button or tk.Label, so that's what I did for now. Display it however you want, but the main question was how to print out those coordinates, so you'll notice a couple print() statements that should show what you're looking for.

#! /usr/bin/python3

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import os
import math
import tkinter as tk
from PIL import ImageTk, Image

root = tk .Tk() ; root .title( 'resizer' )
w, h = 300, 300
border = 50
root .geometry( f'{w}x{h}' )
root .attributes( '-alpha', 0.25 )

cwd  = os .getcwd()
filename  = os .path .join( cwd, 'image.jpg' )
img  = Image .open( filename )
resize  = img .resize(  ( w +border, h +border ),  Image .ANTIALIAS  )
bg  = ImageTk .PhotoImage( image = resize )

canvas = tk .Label( root,  width = w,  height = h,  image = bg )
canvas .pack()

def resizer( event ):
    global img, bg, border
    print( f'x: {event .x}, y: {event .y}   w: {event .width}, h: {event .height}' )
    print( f'mid x: { math .floor( event .x +(event .width /2) ) },\
             mid y: { math .floor( event .y +(event .height /2) ) }\n' )
    resize  = img .resize(  ( event .width +border,  event .height +border ),  Image .ANTIALIAS  )
    bg  = ImageTk .PhotoImage( image = resize )
    canvas .config( image = bg,  width = event .width,  height = event .height )

root .bind( '<Configure>', resizer )
root .mainloop()

##  eof  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create circular image PIL Tkinter

Upvotes: 1

Related Questions