I'm following Is it possible to have a vertical-oriented button in tkinter? and How do you create a Button on a tkinter Canvas? as the guideline to create some actions for Button on a Tkinter Canvas?. The actions worked, but the effect of Button
is frozen.
The code:
import tkinter as tk
import tkinter.font as tkfont
main = tk.Tk()
font = tkfont.nametofont("TkDefaultFont")
label = "Click Me"
height = font.measure(label) 4
width = font.metrics()['linespace'] 4
canvas = tk.Canvas(main, height=height, width=width, background="SystemButtonFace", borderwidth=2, relief="raised")
canvas.create_text((4, 4), angle="90", anchor="ne", text=label, fill="SystemButtonText", font=font)
canvas.bind("<ButtonPress-1>", lambda ev: ev.widget.configure(relief="sunken"))
canvas.bind("<ButtonRelease-1>", lambda ev: ev.widget.configure(relief="raised"))
canvas.bind("<ButtonPress-1>", lambda ev: submit())
def submit():
print("1")
canvas.place(x=5, y=height 10)
main.mainloop()
If I remove canvas.bind("<ButtonPress-1>", lambda ev: submit())
, then the effect of Button
worked, but there is Submit
action.
Otherwise, If leave the code like above which contains the line canvas.bind("<ButtonPress-1>", lambda ev: submit())
, then the effect of Button
is frozen, but the Submit
action works.
I'm looking for a way to have either the effect of Button
and Submit
action work together. Please help me. Thank you.
CodePudding user response:
You need to set add=True
on the second bind of <ButtonPress-1>
event, so that the previous binding is not override:
canvas.bind("<ButtonPress-1>", lambda ev: ev.widget.configure(relief="sunken"))
canvas.bind("<ButtonRelease-1>", lambda ev: ev.widget.configure(relief="raised"))
canvas.bind("<ButtonPress-1>", lambda ev: submit(), add=True) # added add=True