python - How to disable a Tkinter canvas object -


in project using tkinter buttons background gif image. have requirement add "icon", given base64 string. tkinter button doesn't provide option add icon, or second image. that's why have created custom button using canvas. code below:

from tkconstants import disabled tkinter import tk, canvas import tkinter import base64  import imagetk   _fontcolor = "#ffffff" _bgcolor = "#787878" _icondata = '''ivborw0kggoaaaansuheugaaabgaaaaycayaaadgdz34aaaagxrfwhrtb2z0d2fyzqbbz  g9izsbjbwfnzvjlywr5ccllpaaaaujjrefuenrsvc1qhdaqhq20fnrp7kf68ib9d+99dp/j5+jdr9bz8vwu  pchbqbdo00yyllusrw4x9nkbjy+rl5nmz34mxhjodbm0h/yexoy3dcynrtxx3bmbv8qtxxdvvat7jgywiv2  opbe1gywh9rwdh83xqeicuphjg6mmnixa+t5jkrelmum4fimk4nvjf4bhm6cjlkqwu1vp69p8q9u2mi7j/p  oo5hv+yn9weveuqri8yazomuc5ne0dwzbbmaznupn95o276kqryxi8z4mgcmd3fajrwqn79tnoug5c1xvtz  lrq04om8h3bbi7jqn/3oo2m/u0too9vvqlrklh/uwj2y/hhsg0b97vyrcjyv+mss5n6ewmaqhic5zzdswas  suzqhfzw8l5sd5clnqgkbxer9ttu/3vw/yb/eyjvaqya4v5708p9noaaaaaasuvork5cyii='''   class desktopbtn(tkinter.button):      def __init__(self, parent, buttonname, connector=none, **options):         '''         @param buttonname: name of button          '''         tkinter.button.__init__(self, parent, **options)         self._imagepath = 'button.gif'         self._btnpresspath = 'buttonp.gif'         self._btnpressimage = tkinter.photoimage(file=self._btnpresspath)         self._image = tkinter.photoimage(file=self._imagepath)         self.bind('<buttonpress-1>', self._on_pressed)         self.bind('<buttonrelease-1>', self._on_release)         self._parent = parent         self._btnname = buttonname         self._connector = connector         self.config(width=70,                     height=65,                     borderwidth=0,                     compound=tkinter.center,                     font=("arial", 9, "bold"),                     foreground=_fontcolor,                     activebackground=_bgcolor,                     text=buttonname,                     wraplength=64,                     image=self._image,                     command=self._onclickswitch,                     state="disabled")      def _on_pressed(self, event):         if self.cget("state") != "disabled":             self.config(relief="flat")             self.config(image=self._btnpressimage)      def _on_release(self, event):         if self.cget("state") != "disabled":             self.config(image=self._image)      def _onclickswitch(self):         self.config(relief="flat")         if self._connector:             self._connector.switchdesktop(self._btnname,                                           "test")      def getbuttonname(self):         return self._btnname      def setconnector(self, connector):         self._connector = connector   class custombutton(canvas):     def __init__(self, parent, buttonname=none, icon=none, command=none):         canvas.__init__(self, parent, borderwidth=0, highlightthickness=0)         self.command = command         self._imagepath = 'button.gif'         self._btnpresspath = 'buttonp.gif'         self._icon = icon         self._btnpressimage = tkinter.photoimage(file=self._btnpresspath)         self._image = tkinter.photoimage(file=self._imagepath)         self.bgimage = self.create_image(35, 35, image=self._image)         self.text = buttonname         if self._icon:             self._icondata = base64.b64decode(self._icon)             self._iconimage = imagetk.photoimage(data=self._icondata)             self.create_image(35, 35, image=self._iconimage)         if self.text , self._icon:             self.create_text(35, 63, anchor="s",                              state=disabled,                              text=self.text,                              font=("arial", 9, "bold"),                              fill=_fontcolor)         elif not self._icon:             self.create_text(35, 45, anchor="s",                              state=disabled,                              text=self.text,                              font=("arial", 9, "bold"),                              fill=_fontcolor)         self.configure(width=70, height=70, state=disabled) #         if self.cget("state") == "disabled": #             pass #         else:         self.bind("<buttonpress-1>", self._on_press)         self.bind("<buttonrelease-1>", self._on_release)      def _on_press(self, event):         self.itemconfig(self.bgimage,image=self._btnpressimage)         print "pressed"      def _on_release(self, event):         self.itemconfig(self.bgimage,image=self._image)         if self.command not none:             self.command()  tk = tk() = desktopbtn(tk, "test") but.pack() butt_blank = custombutton(tk) butt_text = custombutton(tk, buttonname="test") butt_icon = custombutton(tk, icon=_icondata) butt_icon_text = custombutton(tk, icon=_icondata, buttonname="test") butt_blank.pack() butt_text.pack() butt_icon.pack() butt_icon_text.pack() tk.mainloop() 

the first button(class) tkinter button used use. have problem. how disable canvas custom button the normal tkinter button. should grayed , ignore mouse events. according tkinter 8.5 reference using disabled state on create_image or canvas object, custom button should behave old button. i'm using python 2.7.

here button images used (button.gif , buttonp.gif): button.gif buttonp.gif

as mentioned in comments 1 workaround make composite image.
simple example of is:

#!python3  import tkinter tk pil import image, imagetk  root = tk.tk() bgim = image.open("bg.gif") bgphoto = imagetk.photoimage(bgim)  button1 = tk.button(root, image=bgphoto) button1.pack()  newim = image.open("bg.gif").convert('rgba') # ensure both images in mode supports transparency iconim = image.open("icon.png").convert('rgba') newim.paste(iconim, (-30,-40), iconim) # paste second image first image newphoto = imagetk.photoimage(newim) button2 = tk.button(root, image=newphoto) button2.pack()  root.mainloop() 

the bg.gif 1 of images origional post, other simple png icon transparency.

for more info on merging images see post


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -