Eugene Kartashev
Eugene Kartashev

Reputation: 173

How to update an image on button in Tkinter?

When I click on 'apply_but' button I want the picture on this button to be updated. But it doesn't work. Another problem is that when I open this window - "Noise insertion" also method "curves" works and opens another window with label "What are u looking for?". But this method should work only after clicking on "img_but" button.

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()

Upvotes: 1

Views: 104

Answers (1)

pitamer
pitamer

Reputation: 1064

Commands shouldn't call functions directly - instead, they should name the function to be called when activated.

Try replacing this: command=self.update_pic(pic)

With this: command = lambda pic=pic: self.update_pic(pic)

And this: command = self.curves()

With this: command = self.curves

Upvotes: 1

Related Questions