2014-08-31 40 views
0

晚上好一切:)枕頭,Tkinter的,Python中,隱形圖像按鈕

所以,我一直努力我的一個小項目,香港專業教育學院想出這個代碼(由Sapphire64一點幫助:)) 我可以顯示一個圖像作爲一個按鈕,使其可點擊,但我有問題,你可以看到它是一個按鈕,你可以看到圖像周圍的矩形按鈕框。 我希望圖像可以像按鈕一樣點擊,但基本上不可見。 我又試圖使它成爲這幾乎以同樣的一個checkbutton但同樣我擁有的不僅僅是形象,checkbutton其他一些明顯證據。我可以讓checkbutton隱形嗎?

所以我的問題,我怎樣才能使一個可點擊的圖片,沒有它是明顯的一個按鈕/ checkbutton。

謝謝!

香港專業教育學院把我的整個代碼在這裏,你可以看到圖像被顯示爲兩個按鈕,一個被註釋掉了。

from tkinter import * 
from PIL import Image, ImageTk 

SWH = Tk() #Create Window 

SWH.geometry("1024x950+130+0") 
SWH.title("ServiceWhiz.") 

img = None #Var for future image named img. Currently giving it "None" as value. 

def printimage(): #When print image is pressed do this: 
    global img  #Make reference to predefined var. 
    load = Image.open("ServiceWhiz.png") #Load Image from file. 
    render = ImageTk.PhotoImage(load) #Load the image. 

################################ 
    img = Checkbutton(SWH,state = ACTIVE,height = 45,width = 289,offvalue=0, image=render,command=imgpress) #Display the image as a button and allow it to go to imgpress. 
    #img = Button(SWH,image=render,command=imgpress) 
################################ 
    img.image = render 
    img.place(x=0,y=0) 
    return; 

def imgpress(): 
    global img 
    img.destroy() 
    Label1 = Label(SWH, text="Image has been clicked",fg="#0094FF",font=('Arial',20)).pack() 
    return; 

SWTitle = Label(SWH, text="ServiceWhiz.",fg="#0094FF",font=('Arial',20)).pack() 
MyButtonTest = Button(SWH, text="Click Me.",fg="White",bg="#0094FF",command=printimage).pack() 
+0

你可以做的就是讓一個按鈕的X和Y座標,點擊看:HTTP://stackoverflow.com/questions/5083720/how-do-i-get-the-x-coords-to-show-上之後,鼠標點擊的Python/13440521#13440521,那麼你可以進行測試,看看他們是否有一定的邊界內,如果它確實是這樣的東西,這將有效地給一個看不見的按鈕。 – W1ll1amvl 2014-08-31 20:28:01

回答

2

如果您想要點擊圖片,只需添加一個綁定到<Button-1>。例如:

l = Label(..., image=render, ...) 
... 
def imgpress(event): 
    ... 
l.bind("<Button-1>", imgpress) 
... 

通過將圖像添加到畫布並綁定到畫布上,可以獲得相同的效果。

+0

啊!這很有用,現在它工作得很好,非常感謝:) – 2014-09-01 11:19:22