【问题标题】:Python refresh variable to new value tkinterPython将变量刷新为新值tkinter
【发布时间】:2019-03-04 12:12:22
【问题描述】:

我已经设法将 var 的值更改为新图像,但它没有显示在 GUI(tkinter) 上。 想知道是否有办法刷新画布或变量以使新图像出现?

我对python很陌生,所以请放轻松。

 def clothesImage(self):
    n = 0
    def nextimage(event):
        nonlocal n
        while os.path.splitext(imagechange(n))[0] == os.path.splitext(getimage)[0]:
            n += 1
        print(imagechange(n))


    # output_dir = 'resized'
    def imagechange(n):
        dir = os.getcwd()
        ordereddir = sorted(os.listdir(dir))
        getimage = ordereddir[n]
        extention = os.path.splitext(getimage)[1]
        while str(extention) != ".jpg":
            n += 1
            getimage = ordereddir[n]
            extention = os.path.splitext(getimage)[1]
        return getimage

    getimage = imagechange(n)

    width = 400
    height = 533
    canvas = Canvas(self, width=width, height=height)
    canvas.place(x=0, y=0)
    image = Image.open(getimage)
    canvas.photo = ImageTk.PhotoImage(image)
    image_id = canvas.create_image(width/2, height/2, anchor="center", image=canvas.photo)
    canvas.bind("<Return>", nextimage)
    return image_id, canvas

【问题讨论】:

    标签: python variables tkinter refresh


    【解决方案1】:

    您正在搜索的方法是 canvas.update() :它将(很明显)应用所有修改并激活已触发的绑定函数。

    【讨论】:

    • 嘿,我厌倦了,它似乎没有用,有什么方法可以让它在这种情况下工作吗?
    • 您有错误消息吗?当我尝试执行 imagechange(n) 时,它不起作用,因为如果您在 Windows 上,隐藏的“.DS_Store”文件不可分成两部分,这可能是一个问题,如果您不是我不知道如果有一些
    • 我们能看到整个程序吗? @JaysonAriate 我也无法运行函数的最后一部分:“'Image' has no attribute 'open'”
    • 你能通过 ja2graal@gmail.com 给我发电子邮件吗?我会把项目转让给你。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多