tkinter中背景图像的大小

问题描述 投票:1回答:1

我需要根据背景图像的宽度和高度调整窗口大小(tkinter)。我的代码

from tkinter import *
from PIL import ImageTk
import cv2

image=cv2.imread("New_refImg.png")
width_1, height_1,channels = image.shape   

canvas = Canvas(width = width_1, height = height_1, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)

img = ImageTk.PhotoImage(file = "New_refImg.png")
canvas.create_image(10, 10, image = img, anchor = NW)

mainloop()

我正在使用一个简单的方法,我调用相同的图像两次:image=cv2.imread("New_refImg.png")img = ImageTk.PhotoImage(file = "New_refImg.png"),但有没有办法改变这条线img = ImageTk.PhotoImage(file = "New_refImg.png")img = ImageTk.PhotoImage(image)(图像已经在代码的第3行调用)谢谢

python-3.x image tkinter
1个回答
0
投票

我不知道PIL,但我可以告诉你如何在tkinter中做到这一点:

from tkinter import *

root = Tk() # Create Tk before you can create an image

img = PhotoImage(file='pilner.png')
w, h = img.width(), img.height()   

canvas = Canvas(root, width=w, height=h, bg='blue', highlightthickness=0)
canvas.pack(expand = YES, fill = BOTH)
canvas.create_image(0, 0, image=img, anchor=NW)

root.mainloop()

highlightthickness=0移动画布上的高亮边框。我将它定位在0,0,以便不显示bg。

© www.soinside.com 2019 - 2024. All rights reserved.