As mentioned in the title, how do I resize the pictures dynamically? Functionality: When I click "Other" button, an open menu is displayed. The optionmenu displays images from a folder that I have specified in the code. All the images are in different sizes. How can I maintain the aspect ratio while resizing them?
...
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)