如何使用Tkinter的画布组件一个按钮? [英] How to make a Button using the tkinter Canvas widget?

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

问题描述

我想获得一个按钮出了画布。我试图在按钮插件的画布上,但没有奏效。谷歌搜索了一下,我发现(此处为:<一href=\"http://stackoverflow.com/questions/11980812/how-do-you-create-a-button-on-a-tkinter-canvas\">How你对一个Tkinter的画布?)的画布方法 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.

的简单的解决方案是创建只使用帆布按钮的作用。没什么好说的特别之处这样做:只需创建一个画布,然后加为按钮preSS和ButtonRelease绑定来模拟一个按钮是pressed

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.

下面是一个粗略的想法:

Here's a rough idea:

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

要完成你要设置℃的结合的错觉;进入&GT; &LT;假&GT; (模拟激活状态),同时确保光标在按钮上松开按钮 - 注意按钮多么真实,如果你在释放之前移动鼠标离开不做任何事情。

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的画布组件一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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