为画布按钮创建具有 Tkinter 效果的操作
我正在关注 是否可以有一个tkinter 中的垂直按钮? 和 如何在 tkinter 画布上创建按钮? 作为在 Tkinter 画布上为按钮创建一些操作的指南?。这些操作有效,但 Button
的效果被冻结。
代码:
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()
如果我删除 canvas.bind("
,那么 Button
的效果就起作用了,但是有是提交
操作。
否则,如果保留上面包含行 canvas.bind("
的代码,则 Button
的效果code> 已冻结,但 Submit
操作有效。
我正在寻找一种方法来使 Button
和 Submit
操作的效果一起工作。请帮我。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
事件的第二个绑定上设置add=True
,以便不会覆盖之前的绑定:You need to set
add=True
on the second bind of<ButtonPress-1>
event, so that the previous binding is not override: