내 프로젝트에서 나는 이미지로 배경 gif와 함께 Tkinter 버튼을 사용했습니다. 이제 base64 문자열로 지정된 "아이콘"을 추가해야합니다. 은 Tkinter Button
아이콘 또는 두 번째 이미지를 추가하는 옵션을 제공하지 않습니다. 그래서 canvas
. 아래 코드 :
from Tkconstants import DISABLED
from 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 the 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 and 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 is not None:
self.command()
tk = Tk()
but = 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()
첫 번째 버튼 (클래스)은 내가 사용했던 Tkinter 버튼입니다. 이제 나는 문제가 있습니다. 내 캔버스 사용자 정의 버튼을 일반 Tkinter Button
. 회색으로 표시되고 마우스 이벤트를 무시해야합니다. create_image 또는 캔버스 개체 의 상태를 사용하는 Tkinter 8.5 참조 에 따르면 DISABLED
내 사용자 지정 Button은 이전 Button처럼 동작해야합니다. Python 2.7을 사용하고 있습니다.
여기에 또 다른 해결 방법 rectangle
이 있습니다. 다른 항목 위에 a 를 그려서 흰색으로 채우고 내 예제 stipple
에서는 a Bitmap
로 채우십시오 gray50
.self.create_rectangle(-1,-1,70,70, stipple="gray50", fill="white")
코드는 다음과 같습니다.
from Tkconstants import DISABLED, NORMAL
from 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 the 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
)
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(Tkinter.Canvas):
def __init__(self, parent, buttonname=None, icon=None, command=None, **options):
Tkinter.Canvas.__init__(self, parent, borderwidth=0, highlightthickness=0,**options)
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 and self._icon:
self.create_text(35, 63, anchor="s",
text=self.text,
font=("arial", 8, "bold"),
fill=_FONTCOLOR)
elif not self._icon:
self.create_text(35, 45, anchor="s",
text=self.text,
font=("arial", 8, "bold"),
fill=_FONTCOLOR)
self.configure(width=70, height=70)
self._activation()
def disable(self):
self.config(state=DISABLED)
self._activation()
def enable(self):
self.config(state=NORMAL)
self._activation()
def _activation(self):
if self.cget("state") == "disabled":
self.create_rectangle(-1,-1,70,70, stipple="gray50", fill="white")
self.unbind("<ButtonPress-1>")
self.unbind("<ButtonRelease-1>")
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 is not None:
self.command()
tk = Tk()
but = DesktopBtn(tk, "test")
but.config(state="disabled")
but.pack()
butt_blank = CustomButton(tk)
butt_text = CustomButton(tk, buttonname="test", state=DISABLED)
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.disable()
butt_icon_text.pack()
tk.mainloop()
결과는 거의 동일하지만이 "위젯"을 비활성화하고 활성화하는 두 가지 방법을 구현해야합니다. 그래서 나는 여전히 내 질문에 대한 해결책을 기다리고 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다