【问题标题】:Pillow, Tkinter, Python, Invisible image buttonPillow, Tkinter, Python, Invisible image button
【发布时间】:2014-08-31 20:10:22
【问题描述】:

大家晚上好:)

所以,我一直在做我的一个小项目,我想出了这段代码(在 Sapphire64 的帮助下:)) 我可以将图像显示为按钮以使其可点击,但我遇到的问题是您可以看到它是一个按钮,您可以看到图像周围的矩形按钮框。 我希望图像像按钮一样可点击,但本质上是不可见的。 然后我试着把它做成一个检查按钮,它的工作原理几乎相同,但我再次看到了一些可见的证据,而不仅仅是图像,检查按钮。我可以让检查按钮不可见吗?

所以我的问题是,我怎样才能制作一个可点击的图像而不让它明显是一个按钮/检查按钮。

谢谢!

我把我的整个代码放在这里,你可以看到图像显示为两个按钮,一个被注释掉了。

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

【问题讨论】:

  • 您可以做的是获取按钮单击的 x 和 y 坐标请参见:stackoverflow.com/questions/5083720/… 然后您可以测试它们以查看它们是否在特定边界内,如果是这样,它会执行某些操作,这将有效地提供一个不可见的按钮。

标签: python image button tkinter pillow


【解决方案1】:

如果您希望能够单击图像,只需将绑定添加到<Button-1>。例如:

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

您可以通过将图像添加到画布并在画布上绑定来获得相同的效果。

【讨论】:

  • 啊!这很有用,现在很好用,非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多