Python: Whole image not showing - python

Today I was trying to learn how the PhotoImage works but I run into errors and problems non stop. After lots of research I finally get an image to show up BUT its not the whole image, just a piece of it.
Heres my code:
from tkinter import*
root = Tk()
canvas = Canvas( root , width=720 , height=480 )
originallogo = PhotoImage( file="Picture1.gif" )
canvas.create_image( 0, 0, image=originallogo )
canvas.grid()
root.mainloop()
I would post a screenshot of the outcome but I am not level 10 yet. Heres a link of it instead: https://www.dropbox.com/s/iiwsdmgvlhyhlef/Screen%20shot%202014-11-24%20at%208.34.51%20PM.png?dl=0

By default the center of the image will be placed at the given coordinates. If you want the upper left corner of the image to be at 0,0, add anchor="nw"
canvas.create_image(..., anchor="nw", ...)

There is a very simple solution to this. Change the position of the drawing of the image from 0,0 to different numbers until you get it into the right position that you desire.

Related

How to show only a portion of the tkinter canvas by cropping the tkinter window?

I want to be able to zoom into my tkinter canvas. My tkinter canvas is 500x500px, and I only want my window to display the center 200x200px portion of this canvas. How do I do this? I know that I can just specify my window size as 200x200px using root.geometry("200x200+0+0"), but this causes my window to display the top left corner of my canvas, and not the center. Before I do anything, my entire canvas looks like this:
Ultimately, I want my window to look like this, with the canvas centered within the window:
This is my code:
import tkinter
root = tkinter.Tk()
root.title("")
root.geometry("200x200+0+0")
canvas = tkinter.Canvas(master = root, width = 500, height = 500)
canvas.create_oval(200, 200, 300, 300, outline = "black", fill = "blue")
canvas.pack()
which returns:
As you can see, the canvas is not centered, and the window is showing the upper left hand corner at the moment. Does anyone have any suggestions?
Ok, thanks to this stackoverflow post, I found out there is an option when creating a tkinter canvas called scrollregion. The format of the argument is "x0 y0 x1 y1" for anyone that is wondering, where (x0, y0) is the upper-left corner of the area of the canvas I want to show and (x1, y1) is the bottom-right corner of the same area. My code should be fixed to this:
canvas = tkinter.Canvas(master = root, width = 500, height = 500, scrollregion = "150 150 350 350")
Be wary that these coordinates do not account for a scrollbar...I'm still working on figuring that out. Much thanks to this stackoverflow post as well, specifically the following words:
I don't see any difference between putting the y-scrollbar to the bottom or putting the canvas view to the bottom because the two are linked.

Tkinter frame has dimensions different from dimensions given to and idk why

I'm struggling with Tkinter now. I wanted to create layout but if I define window dimensions (800x600) and create frame which have to be wide 800 too, it have just half.
I tried googling and changing code, if I multiply width 2 times (to 1600) then frame fit the screen perfetly.
Here is code:
import tkinter as tk
SW, SH = 800, 600
win = tk.Tk()
win.geometry(f"{SW}x{SH}")
frm_appname = tk.Frame(
master = win,
bg = 'red'
)
frm_appname.place(
anchor = tk.N,
width = 800,
relheight = (1/6)
)
Here is output:
Can anyone explain me what happened here?
The anchor of "n" (tk.N) means that the top center portion of the frame is at the given coordinate. Since you didn't provide an x and y coordinate for place it defaults to 0,0. So, the top/middle (400,0) of the frame is at coordinate 0,0.
If you set the anchor to "n" or tk.NW, the top-left corner of your frame will be in the top-left corner of the window.
On an unrelated note, experience has taught me that layouts are nearly always easier to create with pack and/or grid. In my coupe of decades of using tk and tkinter, I've never used place for more than one or two special-case widgets.

How to arrange things (coordinate) in Tkinter?

I want to create a Tkinter window where on the right there is an image as below.
In the left of the image, there will be an Entry box with written answers.
Users can check whether the information is correct (if the answer question 30 is 1 the Entry box should be 1, in case if it is not 1, users could fix it)
My so far code:
b,g,r = cv2.split(image)
img = cv2.merge((r,g,b))
root = tk.Tk()
im = Image.fromarray(img)
imgtk= ImageTk.PhotoImage(image=im)
image = tk.Label(root, image=imgtk).pack()
entry1 = tk.Entry(root)
entry1.insert(tk.END, 'the answer').pack()
canvas1.create_window(50,50, window = entry1)
canvas1.create_window(50,100, window = image) #I thought this would make it stood next to each other
root.mainloop()
Sorry, first time working with Tkinter, so my code may stupid
P/s: I know the shape of the image and the Y axis of every question to place the Entry box right next to it.

Why are the Tkinter canvas lines jagged?

The lines drawn on a Tkinter.Canvas are not smooth. How can they be made smooth?
Here's what I tried:
from Tkinter import *
root = Tk()
cv = Canvas(root,bg = 'white')
rt1 = cv.create_rectangle(10,10,110,110,width = 8,tags = ('r1','r2','r3'))
def printRect(event):
print 'rectangle'
def printLine(event):
print 'line'
cv.tag_bind('r1','<Button-1>',printRect)
cv.tag_bind('r1','<Button-3>',printLine)
cv.create_line(10,20,200,200,width = 5,tags = 'r1')
cv.pack()
root.mainloop()
Here's what it looks like:
Tkinter graphics are not anti-aliased which is why the diagonal line appears jagged. There may be a platform specific work-around like this one I found titled Drawing Anti-Aliased Graphics Under Tkinter/Windows to provide the functionality you desire.
You might try to do some antialiasing of the poor, by drawing a clearer colored one pixel larger second line before (under) the first one.
draw an antialias image (using Image PIL ,numpy,opencv etc)
show the image on the canvas.

Draw and move a point over an image in python

Hi all I have to do a little script in Python.
In this script I have a variable (that represents a coordinate) that is continuously updated to a new value.
So I have to draw a red point over a image and update the point position every time the variable that contains the coordinate is updated.
I tried to explain what I need doing something like this but obviously it doesn't works:
import Tkinter, Image, ImageDraw, ImageTk
i=0
root = Tkinter.Tk()
im = Image.open("img.jpg")
root.geometry("%dx%d" % (im.size[0], im.size[1]))
while True:
draw = ImageDraw.Draw(im)
draw.ellipse((i, 0, 10, 10), fill=(255, 0, 0))
pi = ImageTk.PhotoImage(im)
label = Tkinter.Label(root, image=pi)
label.place(x=0, y=0, width=im.size[0], height=im.size[1])
i+=1
del draw
someone may help me please? thanks very much!
Your on the right track using a PhotoImage in a Label but instead of creating a new Label each loop, just create the label once and update its position in the loop.

Categories

Resources