如何使用 tkinter Canvas 小部件制作按钮? [英] How to make a Button using the tkinter Canvas widget?

查看:34
本文介绍了如何使用 tkinter Canvas 小部件制作按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Canvas 中获取一个按钮.我尝试在按钮小部件中pack 画布,但这没有用.谷歌搜索了一下,我发现(这里:How您是否在 tkinter Canvas 上创建了一个按钮?)Canvas 方法 create_window 可能会有所帮助.但是我使用它的方式应该有问题.

I want to obtain a button out of a Canvas. I've tried to pack the canvas in the button widget, but that didn't work. Googling a bit, I've found (here: How do you create a Button on a tkinter Canvas?) that the Canvas method create_window might help. But there should be something wrong in the way I'm using it.

import Tkinter

DIM = 100

root = Tkinter.Tk()
frame = Tkinter.Frame(root)

button = Tkinter.Button(None, width=DIM, height=DIM, command=root.quit)

circle = Tkinter.Canvas(frame, width=DIM, height=DIM)
circle.create_oval(5, 5, DIM-5, DIM-5, fill="red")
circle.create_window(0, 0, window=button)

frame.grid()
circle.grid(row=1, column=1)

root.mainloop()

如果我删除 create_window 行,我可以看到我的画,但我不能(显然)点击它.但是这样,按钮小部件覆盖了我的圈子并显示了一个悲伤的空按钮.

If I erase the create_window line, I can se my painting but I can't (obviously) click on it. But in this way, the button widget cover my circle and shows a sad empty button.

基本上,我想创建一个内部涂有红色圆圈的按钮.

Basically, I want to create a button with a red circle painted inside.

推荐答案

Tkinter 不允许您直接在画布以外的小部件上绘图,并且画布绘图将始终位于嵌入的小部件下方.

Tkinter doesn't allow you to directly draw on widgets other than the canvas, and canvas drawings will always be below embedded widgets.

简单的解决方案是仅使用画布创建按钮效果.这样做并没有什么特别之处:只需创建一个画布,然后为 ButtonPress 和 ButtonRelease 添加绑定以模拟被按下的按钮.

The simple solution is to create the effect of a button using just the canvas. There's really nothing special about doing this: just create a canvas, then add bindings for ButtonPress and ButtonRelease to simulate a button being pressed.

这是一个粗略的想法:

class CustomButton(tk.Canvas):
    def __init__(self, parent, width, height, color, command=None):
        tk.Canvas.__init__(self, parent, borderwidth=1, 
            relief="raised", highlightthickness=0)
        self.command = command

        padding = 4
        id = self.create_oval((padding,padding,
            width+padding, height+padding), outline=color, fill=color)
        (x0,y0,x1,y1)  = self.bbox("all")
        width = (x1-x0) + padding
        height = (y1-y0) + padding
        self.configure(width=width, height=height)
        self.bind("<ButtonPress-1>", self._on_press)
        self.bind("<ButtonRelease-1>", self._on_release)

    def _on_press(self, event):
        self.configure(relief="sunken")

    def _on_release(self, event):
        self.configure(relief="raised")
        if self.command is not None:
            self.command()

要完成幻觉,您需要在 上设置绑定(以模拟活动状态),并且还使确保光标在按钮释放上的按钮上 - 请注意,如果在释放之前将鼠标移开,实际按钮将不会执行任何操作.

To complete the illusion you'll want to set a binding on <Enter> and <Leave> (to simulate the active state), and also make sure that the cursor is over the button on a button release -- notice how real buttons don't do anything if you move the mouse away before releasing.

这篇关于如何使用 tkinter Canvas 小部件制作按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆