python - Tkinter中resize操作不正确
问题描述
class SomeClass(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root, bg="#ffffff")
self.label_image = tk.Label(self, bg ="#ffffff")
self.label_image.pack(anchor=tk.CENTER, fill=tk.X, padx=20, pady=10)
self.label_image.bind('<Configure>', self._resize_image)
def _resize_image(self, event):
print("resize_image")
f_w, f_h = self.winfo_width(), self.winfo_height() - 50
i_w, i_h = self.img_copy.size
width = 1
height = 1
if f_w/f_h > i_w/i_h:
height = f_h
width = int(i_w * f_h/i_h)
else:
width = f_w
height = int(i_h * f_w/i_w)
self.image = self.img_copy.resize((width, height), PIL.Image.ANTIALIAS)
loading_image = ImageTk.PhotoImage(self.image)
self.label_image.configure(image=loading_image)
self.label_image.image = loading_image
macOS。当你拉伸屏幕时,代码可以正常工作,但是如果你全屏制作应用程序并打开图片,它会慢慢按比例增加。在 Ubuntu 上,图片仍然很小,根本没有增加。
在终端会看到: resize_image resize_image resize_image resize_image resize_image...
解决方案
推荐阅读
- bash - 有没有办法使用 bash 将目录中所有文件的所有选项卡更改为空格?
- regex - 我需要这个正则表达式来忽略 HTML 标签
- python - 使用 pytesseract 时,我的 Raspberry Pi 出现问题
- ios - 如何防止应用程序因两个重叠的 AVAudioPlayer 冻结?
- acumatica - 取消保留标题时如何验证未保存的网格记录?
- c++ - 将返回正数总和的内置函数。?
- powershell - 直接获取经理的CN
- directx - 尝试在 directx11 中渲染网格时,三角形似乎引用了不正确的顶点
- tensorflow-federated - 如何控制联合框架的验证数据
- c# - 将枚举添加到 MVC 视图会破坏返回的模型