python tkinter:仅显示图像的一部分

发布于 2024-12-03 03:21:33 字数 709 浏览 1 评论 0原文

这仅显示我的图像的右下角。我做错了什么?

from Tkinter import *
from PIL import Image, ImageTk

class Application(Frame):
    def __init__(self, titl, master=None):
        Frame.__init__(self, master)
        self.grid()

        self.create_widgets()
        self.master.title(titl)

    def create_widgets(self):

        image_file = 'sample.jpg'
        image1 = ImageTk.PhotoImage(Image.open(image_file))
        w = image1.width()
        h = image1.height() 
        self.canvas = Canvas(self, width=w+5, height=h+5)
        self.canvas.grid(row=0, column=0)
        self.canvas.create_image(0,0, image=image1)
        self.canvas.image = image1

app = Application('Image')

app.mainloop()

This only displays the bottom right corner of my image. What am I doing wrong?

from Tkinter import *
from PIL import Image, ImageTk

class Application(Frame):
    def __init__(self, titl, master=None):
        Frame.__init__(self, master)
        self.grid()

        self.create_widgets()
        self.master.title(titl)

    def create_widgets(self):

        image_file = 'sample.jpg'
        image1 = ImageTk.PhotoImage(Image.open(image_file))
        w = image1.width()
        h = image1.height() 
        self.canvas = Canvas(self, width=w+5, height=h+5)
        self.canvas.grid(row=0, column=0)
        self.canvas.create_image(0,0, image=image1)
        self.canvas.image = image1

app = Application('Image')

app.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-12-10 03:21:33

您必须将锚点设置为NW(西北),因为默认情况下其值为CENTER,顾名思义,它使图像在给定坐标上居中:

self.canvas.create_image(0,0, image=image1, anchor=NW)

或者您可以更改稍后如果您保留图像 ID:

self.idImage = self.canvas.create_image(0,0, image=image1)
...
self.canvas.itemconfig(self.idImage, anchor=NW)

http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_image-method

You have to set the anchor to NW (NorthWest) because its value is CENTER by default, which as the name suggests centers the image on the given coordinates:

self.canvas.create_image(0,0, image=image1, anchor=NW)

Or you can change that later if you keep the image id:

self.idImage = self.canvas.create_image(0,0, image=image1)
...
self.canvas.itemconfig(self.idImage, anchor=NW)

http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_image-method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文