gpt4 book ai didi

python - PhotoImage 没有响应 tkinter Scale 小部件

转载 作者:太空宇宙 更新时间:2023-11-03 21:46:23 25 4
gpt4 key购买 nike

我有一个 python 脚本,它允许用户在 tkinter Canvas 小部件中打开图像。程序正在打印新图像应有的尺寸,但只显示原始图像。如何使用新尺寸刷新 Canvas 。

这是Python代码:

from Tkinter import * 
import tkFileDialog
from PIL import ImageTk, Image
factor = 1.0
width_org = 500
height_org = 500
mGui = Tk()
mGui.geometry("600x600")
mGui.configure(background = 'yellow')
frame = Frame(mGui, width = 400, height = 400)
frame.pack(anchor = 'nw')
frame.configure(background = 'red')
canvasframe = Canvas(frame, width = 400, height = 400, scrollregion = (-500,-500,500,500))
hbar = Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill = X)
hbar.configure(command= canvasframe.xview)
vbar = Scrollbar(frame,orient = VERTICAL )
vbar.pack(side=RIGHT, fill = Y)
vbar.configure(command= canvasframe.yview)
canvasframe.configure(xscrollcommand= hbar.set, yscrollcommand= vbar.set)
canvasframe.pack( expand = True, fill = BOTH)
pil_img = Image.open("rose-flower-500x500.jpg")
img = ImageTk.PhotoImage(pil_img)
def openit():
in_path = tkFileDialog.askopenfilename()
try:
pil_img = Image.open(in_path)
except IOError:
pass
width_org, height_org = pil_img.size
try:
img = ImageTk.PhotoImage(pil_img)
except IOError:
pass

canvasframe.create_image(20,20, image = img)
canvasframe.img = img
valueList = [25, 50, 100, 150, 200]
def valuecheck(value):
newval = min(valueList, key=lambda x:abs(x-float(value)))
scalepic.set(newval)
factor = newval/100.0
w = int(width_org*factor)
h = int(height_org*factor)
print (w, h)
pil_img = pil_img.resize((w,h),Image.ANTIALIAS)
img = ImageTk.PhotoImage(pil_img)
canvasframe.create_image(20,20, image =img)
openpic = Button(mGui, text = "Open", command = openit).pack()
scalelabel = Label(mGui, text = "Resize Image").pack()
scalepic = Scale(mGui, from_=min(valueList), to=max(valueList), command=valuecheck, orient = HORIZONTAL)
scalepic.set(100)
scalepic.pack()

mGui.mainloop()

另外,如何打开新图像?我正在考虑 for 循环和某种 dispose 方法,但我不确定语法。

编辑类中的部分代码

from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image


factor = 1.0
width_org = 500
height_org = 500
class MainApp(Frame):
def createControls(self):
frame = Frame(self, width = 600, height = 500)
frame.configure(background = 'red')
frame.pack(anchor = 'nw')
canvasframe = Canvas(frame, width = 600, height = 500, scrollregion = (-600,-500,600,500))
canvasframe.configure(xscrollcommand= hbar.set, yscrollcommand= vbar.set)
canvasframe.pack(expand = True, fill = BOTH)
hbar = Scrollbar(frame, orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill=X)
hbar.configure(command= canvasframe.xview)
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.pack(side=RIGHT, fill = Y)
vbar.configure(command= canvasframe.yview)


def __init__(self, parent):
Frame.__init__(self, parent, width = 800, height = 600, background = 'yellow')
self.pack()
self.createControls()


root = Tk()
app = MainApp(parent =root)

app.mainloop()

最佳答案

您忘记在 canvasframe.img 中保存对新 PhotoImage 的引用,因此当 valuecheck 返回时它会被垃圾收集。

此外,您只需通过 itemconfig 更新现有图像,而不是使用 create_image 为重新缩放的图像创建新的 Canvas 图像。为此,您需要保存 create_image 返回的项目 ID 号。例如,在 openit 中,执行

canvasframe.imgid = canvasframe.create_image(20,20, image = img)

然后在 valuecheck 中,创建新的 PhotoImage 后执行

canvasframe.img = img
canvasframe.itemconfig(canvasframe.imgid, image=img)

您不必将 imgimgid 存储为 canvasframe 的属性,但在这种情况下很方便。但如果 Canvas 上有很多需要跟踪的东西,就会变得困惑。但对于较大的 Tkinter 程序,最好将所有内容放入一个或多个类中。它使事情更有条理,更容易访问。

关于python - PhotoImage 没有响应 tkinter Scale 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52460584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com