栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用tkinter画布小部件制作按钮?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用tkinter画布小部件制作按钮?

Tkinter不允许您直接在画布之外的其他窗口小部件上绘图,并且画布图形将始终位于嵌入式窗口小部件的下方。

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

这是一个大概的想法:

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

要完成这种错觉,您需要在

<Enter>
和上设置绑定
<Leave>
(以模拟活动状态),并确保光标位于按钮释放按钮的上方-
注意,如果您使用真正的按钮,则什么也不做释放鼠标之前将其移开。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646458.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号