Daniel Huckson
Daniel Huckson

Reputation: 1217

Tkinter how to change the checkbutton check image

I would like to change the check image for a tkinter menu checkbutton widget.

I look online at https://www.tcl.tk/man/tcl8.4/TkCmd/menu.htm#M39 and found the option below "selectimage" and tried that but it didn't work.

self.image1 and self.image2 are tk.PhotoImages

I get no errors. Just nothing shows up.

-selectimage value

Available only for checkbutton and radiobutton entries. Specifies an image to display in the entry (in place of the -image option) when it is selected. Value is the name of an image, which must have been created by some previous invocation of image create. This option is ignored unless the -image option has been specified.

    parent.entryconfig(
        self.label,
        image=self.image1,
        selectimage=self.image2,
        variable=self.var,
        command=None if not self.command else lambda: self.command(self.uri, self)
    )

Upvotes: 0

Views: 1378

Answers (1)

figbeam
figbeam

Reputation: 7176

Your code does not run. I won't guess how you have intended this to work. I can, however, provide an example of working code. Have a look and see if this is what you are after:

from tkinter import *

root = Tk()
root.geometry('200x50')

img1 = PhotoImage(file='unselected.png')
img2 = PhotoImage(file='selected.png')
cb = Checkbutton(root, text='Spam', image=img1, compound='left',
                 selectimage=img2)
cb.pack(pady=20)

root.mainloop()

Upvotes: 2

Related Questions