如何调整从Tkinter中的optionmenu显示的图像的大小?

2024-04-27 22:14:48 发布

您现在位置:Python中文网/ 问答频道 /正文

如标题所述,如何动态调整图片大小? 功能:单击“其他”按钮时,将显示一个打开的菜单。optionmenu显示我在代码中指定的文件夹中的图像。所有的图像都有不同的大小。如何在调整长宽比的同时保持长宽比

...
def otherdrops():
    other_opt = wother
    other_opt.config(width=50, font=('Helvetica', 12))
    other_opt.pack()   
def other_images(wother):
    print(wother)  # selected option
    other_label.config(image=other[wother])
other_label = tk.Label(otherframe)
other_label.pack(side = 'bottom', pady=padylength)
other = {}
for other_name in tradinglists.tradingotherimages:
    other[other_name] = ImageTk.PhotoImage(Image.open("/Images/{}.png".format(other_name)))
othervariable = tk.StringVar(tab2)
othervariable.set(tradinglists.tradingotherimages[0])
wother = tk.OptionMenu(otherframe, othervariable, *tradinglists.tradingotherimages, command=other_images)
def refreshother():
    otherframe.pack_forget() if otherframe.winfo_manager() else otherframe.pack(anchor='center')
other_k = tk.Button(wavebtnframe, bg = "red", text="Other", width = artbtn_width, height = btnsize_height, command=lambda:[otherdrops(), refreshother()])
other_k.pack(side = 'left', padx=wavebtnspadx, pady=padylength)

V2:

def importImageWithResize(filename):
    img = Image.open(filename)
    width, height = img.size
    ratio = width / height
    new_height = 20
    new_width = new_height * ratio
    return img.resize((width, height))

def otherdrops():
    other_opt = wother
    other_opt.config(width=50, font=('Helvetica', 12))
    other_opt.pack()   
def other_images(wother):
    print(wother)  # selected option
    other_label.config(image=other[wother])
other_label = tk.Label(otherframe)
other_label.pack(side = 'bottom', pady=padylength)
other = {}
for other_name in tradinglists.tradingotherimages:
    other[other_name] = ImageTk.PhotoImage(importImageWithResize("./Images/{}.png".format(other_name)))
othervariable = tk.StringVar(tab2)
othervariable.set(tradinglists.tradingotherimages[0])
wother = tk.OptionMenu(otherframe, othervariable, *tradinglists.tradingotherimages, command=other_images)
def refreshother():
    otherframe.pack_forget() if otherframe.winfo_manager() else otherframe.pack(anchor='center')
other_k = tk.Button(wavebtnframe, bg = "red", text="Other", width = artbtn_width, height = btnsize_height, command=lambda:[otherdrops(), refreshother()])
other_k.pack(side = 'left', padx=wavebtnspadx, pady=padylength)

Tags: namedefwidthlabelpacktkotheropt
1条回答
网友
1楼 · 发布于 2024-04-27 22:14:48

您可以使用image_name.resize((width, height))调整图像大小 我会做一个这样的方法:

def importImageWithResize(filename):
    img = Image.open(filename)
    width, height = img.size
    ratio = width / height
    new_height = preset_height
    new_width = int(new_height * ratio)
    return img.resize((new_width, new_height ))

然后将导入行更改为匹配:

other[other_name] = ImageTk.PhotoImage(importImageWithResize("/Images/{}.png".format(other_name)))

我假设你有一个设定的高度,你希望他们都匹配,但你可以改变它有一个预设的宽度或任何你想要的。如果你对你想要的尺寸有具体的规定并且需要帮助,请随意询问例子

如果您有更多问题等,请告诉我们

相关问题 更多 >