Reputation: 33
I know it's easy to call a function with Tkinter buttons by using command
, but it doesn't work the same way with images. My question is pretty simple I believe, how can I call a function by clicking on an image?
Here's the code. I want to click on picture
and that will call the make_newwindow
function.
from tkinter import *
import tkinter as tk
def make_newwindow():
global newwindow
root.withdraw()
newwindow = tk.Toplevel()
newwindow.title('Nível da grama região 2')
newwindow.geometry('580x520')
root = tk.Tk()
root.title('Nível da grama região 1')
root.geometry("580x520")
picture = PhotoImage(file="picture.png")
label0 = Label(root, image=picture, borderwidth=0, highlightthickness=0)
label0.place(x=62, y=205)
root.mainloop()
Upvotes: 1
Views: 2584
Reputation: 123423
In my opinion the simplest way to do it would be to attach the image to a Button
instead of a Label
widget, because then all you would need to do is specify a command=
argument referencing the function you want to be called when it's clicked.
Here's what I mean:
import tkinter as tk
def make_newwindow():
global newwindow
raiz.withdraw()
newwindow = tk.Toplevel()
newwindow.title('Nível da grama região 2')
newwindow.geometry('580x520')
raiz = tk.Tk()
raiz.title('Nível da grama região 1')
raiz.geometry("580x520")
picture = tk.PhotoImage(file="picure.png")
btn0 = tk.Button(raiz, image=picture, borderwidth=0, highlightthickness=0,
command=make_newwindow)
btn0.place(x=62, y=205)
raiz.mainloop()
If you really want to use a Label
for some reason, you can call the universal bind()
widget method to attach your function to mouse button-1 click events.
To do that, change the code above so it creates a Label
(like your code does), but also calls bind()
as illustrated. Note how a callback function is being created dynamically via a lambda
expression. This is needed because your make_newwindow()
doesn't accept any arguments. However tkinter
event-handler callback functions are all passed an event
argument (see Events and Bindings). Since it's not needed here, the argument is simply ignored and has been given the name _
(the Python convention for such things).
...
picture = tk.PhotoImage(file="picure.png")
label0 = tk.Label(raiz, image=picture, borderwidth=0, highlightthickness=0)
label0.place(x=62, y=205)
label0.bind('<Button-1>', lambda *_: make_newwindow()) # Create and bind callback func.
raiz.mainloop()
Upvotes: 1
Reputation: 1917
how about this? Here's the complete code that works
import tkinter as tk
from tkinter import *
def make_newwindow(data):
global newwindow
root.withdraw()
newwindow = tk.Toplevel()
newwindow.title('Nível da grama região 2')
newwindow.geometry('580x520')
root = tk.Tk()
root.title('Nível da grama região 1')
root.geometry("580x520")
picture = PhotoImage(file="Capture001.png")
label0 = Label(root, image=picture, borderwidth=0, highlightthickness=0)
label0.place(x=62, y=205)
label0.pack()
label0.bind('<Button-1>', func=make_newwindow)
root.mainloop()
the data
is the information about the even that occured
Upvotes: 0