提问者:小点点

有可能将方法转换为int吗? 如果是,如何处理?


我正在尝试将一个方法转换为int(我需要将方法值除以2)

width = int(post.width) / 2

height = int(post.height) / 2

但它给了我一个错误:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'method'

有什么方法可以做吗?

编辑:对于那些试图帮助我的人,我想导入一个图像(与Pil,tkinter),但与他的一半大小。

post1 = Image.open(directory)

width = int(post.width) / 2

height = int(post.height) / 2

canvas=Canvas(root,width=width,height=height)
canvas.create_image(0,0,anchor=NW,image=post)
canvas.pack(padx=20, pady=20)

另外,如果您需要,我将提供完整的脚本:

from minio import Minio
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
import random
import os

root = Tk()
root.title("Ripetra")

width_value = root.winfo_screenwidth()
height_value = root.winfo_screenheight()

minio = Minio('myip',
                access_key='key',
                secret_key='key',
              )

immagini = minio.list_objects('ripetra', recursive=True)
names = [img2.object_name for img2 in immagini]
image = random.choice(names)
imagecanvas = minio.fget_object("ripetra", image, "cont/ft/" + image)

dir = os.path.join("cont/ft/", image)

post1 = Image.open(dir)

resized = post1.resize((width_value, height_value), Image.ANTIALIAS)

post = ImageTk.PhotoImage(resized)

width = int(post.width()) / 2

height = int(post.height()) / 2

canvas=Canvas(root,width=width,height=height)
canvas.create_image(0,0,anchor=NW,image=post)
canvas.pack(padx=20, pady=20)

root.mainloop()

共2个答案

匿名用户

您可能忘了。width和。height后面的括号:

width = int(post.width()) / 2
height = int(post.height()) / 2

匿名用户

您应该在每个方法名之后放入(),因为您要做的是调用该方法。 您的代码当前试图做的是将一段python代码转换为一个整数,这是没有意义的。

请尝试执行以下操作:

width = int(post.width()) / 2

height = int(post.height()) / 2

虽然我觉得你可能需要这个:

width = int(post.width() / 2)

height = int(post.height() / 2)

也就是说,如果返回的值中有一个是奇数,则需要在除法后将该值转换为整数。