【问题标题】:How to update an image on button in Tkinter?如何在 Tkinter 中更新按钮上的图像?
【发布时间】:2020-08-27 07:44:45
【问题描述】:

当我点击“apply_but”按钮时,我希望更新此按钮上的图片。但它不起作用。 另一个问题是,当我打开这个窗口时——“噪声插入”也可以使用“曲线”方法并打开另一个带有标签“你在找什么?”的窗口。但是这种方法只有在点击“img_but”按钮后才有效。

class Noise(Toplevel):
    def __init__(self, pic):
        super().__init__()

        w = self.winfo_screenwidth() // 2 - 300  # ширина экрана
        h = self.winfo_screenheight() // 2 - 300  # высота экрана

        self.geometry('600x600+{}+{}'.format(w, h))

        self.title('Noise insertion')
        self.resizable(width=False, height=False)
        self['bg'] = '#7be8cf'

        Label(self, text='Noise insertion', font=('Comic Sans MS', 20),
                       fg='#3d3d42', bg='#7be8cf').place(x=200, y=0)

        var = IntVar()
        var.set(0)
        var2 = IntVar()
        var2.set(0)

        self.yes_but = Radiobutton(self, text="Yes", variable=var, value=0,
                               font=('Comic Sans MS', 20),
                               width='10', height='1', activebackground='#e6f547',
                               bg='#7be8cf')
        self.no_but = Radiobutton(self, text="No", variable=var, value=1,
                              font=('Comic Sans MS', 20),
                              width='10', height='1', activebackground='#e6f547',
                              bg='#7be8cf')
        self.salt_but = Radiobutton(self, text="salt and peper", variable=var2, value=0,
                                font=('Comic Sans MS', 20),
                                width='10', height='1', activebackground='#e6f547',
                                bg='#7be8cf')
        self.gauss_but = Radiobutton(self, text="gauss noise", variable=var2, value=1,
                                 font=('Comic Sans MS', 20),
                                 width='10', height='1', activebackground='#e6f547',
                                 bg='#7be8cf')
        self.scale = Scale(self, orient=VERTICAL, from_=0, to=100, bg='#7be8cf',
                       activebackground='#e6f547', font='10',
                       highlightbackground='#7be8cf', label='%', fg='#f53be2')
        self.apply_but = Button(self, text='Apply', font=('Comic Sans MS', 10),
                            width='15', bg='grey', fg='white', bd=20, command=self.update_pic(pic))

        self.img_but = Button(self, image=pic, command = self.curves())

        # Pack()
        self.yes_but.place(x=0, y=100)
        self.no_but.place(x=0, y=50)
        self.salt_but.place(x=220, y=50)
        self.gauss_but.place(x=220, y=100)
        self.scale.place(x=500, y=50)
        self.apply_but.place(x=220, y=200)
        self.img_but.place(x=150, y=300)

    def update_pic(self, pic):
        var = self.scale.get()
        scale_res = self.scale.get()
        self.img = ImageTk.PhotoImage(get_pic('color_dragon.jpg', size= 
                              (300,200)).filter(ImageFilter.GaussianBlur))
        #self.img_but[image] = self.img ???

        pass

    def curves(self):
        Curves()
        pass

class Curves(Toplevel):
    def __init__(self):
        super().__init__()
        w = self.winfo_screenwidth() // 2 - 300  # ширина экрана
        h = self.winfo_screenheight() // 2 - 300  # высота экрана

        self.geometry('600x600+{}+{}'.format(w, h))

        self.title('Curves')
        self.resizable(width=False, height=False)
        self['bg'] = '#7be8cf'

        Label(self, text='What are u looking for?', font=('Comic Sans MS', 20),
                       fg='#3d3d42', bg='#7be8cf').pack()

【问题讨论】:

  • command = self.curves() 应该是command = self.curves 也可以在update_pic() self.img_but.image = self.img 中说
  • @CoolCloud 如果我说self.img_but.image = self.img,那么我会收到以下错误:AttributeError: 'Noise' object has no attribute 'img_but'. 并且使用command = self.curves 没有帮助。在单击按钮之前,我仍然会打开此窗口。
  • 我实际上要求从方法中删除()self.img_but[image] = self.img 有什么问题?
  • 我不经常使用 OOP,所以我不知道属性有什么问题,但尝试使用 img_but.image = self.img 而不是 self.img_but.image = self.img,它也应该是 command=lambda: self.update_pic(pic)
  • @CoolCloud 首先我得到Unresolved reference 'image' and then the same mistake AttributeError: 'Noise' object has no attribute 'img_but'`

标签: python user-interface tkinter


【解决方案1】:

命令不应直接调用函数 - 相反,它们应命名激活时要调用的函数。

尝试替换这个:command=self.update_pic(pic)

有了这个:command = lambda pic=pic: self.update_pic(pic)

还有这个:command = self.curves()

有了这个:command = self.curves

【讨论】:

  • 是的,这行得通。现在另一个窗口在时间之前没有打开。但是图像呢?如何更新?
  • 好吧,如果我能看到完整的代码,这部分对我来说会更容易测试......但也许可以尝试在 update_pic 函数的末尾添加 self.img_but = Button(self, image=self.img, command=self.curves),然后使用tkinter.Tk() 对象的.update_idletasks.update 方法。
  • .update.update_idletasks 不起作用。但是将一个按钮放在另一个按钮之上)))self.img_but = Button(self, image=self.img, command=self.curves).place(x=150, y=300)update_pic的末尾添加这个
  • 太棒了!如果您选择我的答案,我将不胜感激。您甚至可以对其进行编辑以包含您找到的解决方案,以造福其他人:) @EugeneKartashev
猜你喜欢
  • 2021-03-13
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
相关资源
最近更新 更多