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

如何限制小部件生成的事件数

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

如何限制小部件生成的事件数

我为两种类型的旋转框使用了类似的解决方案,但是实现方式略有不同,因为它们不使用相同的事件。想法是创建一个Spinbox类,其

_increment_lock
属性设置为
True
Spinbox递增时和延迟设置为时的
False
。然后,使旋转框增加的事件绑定到
_increment_lock
在实际执行增加之前检查的方法。减量原理相同。

对于

tk.Spinbox
,我使用了
<Up>
和的绑定来
<Down>
实现上述解决方案,而我使用了
<<Increment>>
和的绑定
<<Decrement>>

这是代码:

import tkinter as tkimport tkinter.ttk as ttkclass MySpinbox(tk.Spinbox):    def __init__(self, master=None, delay=500, **kwargs):        kwargs.setdefault('repeatdelay', delay)        kwargs.setdefault('repeatinterval', delay)        tk.Spinbox.__init__(self, master, **kwargs)        self.delay = delay  # repeatdelay in ms        self.bind('<Up>', self._on_increment)        self.bind('<Down>', self._on_decrement)        self._increment_lock = False        self._decrement_lock = False    def _unlock_increment(self):        self._increment_lock = False    def _on_increment(self, event):        if self._increment_lock: return "break"  # stop the increment        else: self._increment_lock = True self.after(self.delay, self._unlock_increment)    def _unlock_decrement(self):        self._decrement_lock = False    def _on_decrement(self, event):        if self._decrement_lock: return "break"  # stop the increment        else: self._decrement_lock = True self.after(self.delay, self._unlock_decrement)class MyTtkSpinbox(ttk.Spinbox):    def __init__(self, master=None, delay=500, **kwargs):        ttk.Spinbox.__init__(self, master, **kwargs)        self.delay = delay  # repeatdelay in ms        self.bind('<<Increment>>', self._on_increment)        self.bind('<<Decrement>>', self._on_decrement)        self._increment_lock = False        self._decrement_lock = False    def _unlock_increment(self):        self._increment_lock = False    def _on_increment(self, event):        if self._increment_lock: return "break"  # stop the increment        else: # generate a virtual event corresponding to when the spinbox # is actually incremented self.event_generate('<<ActualIncrement>>') self._increment_lock = True self.after(self.delay, self._unlock_increment)    def _unlock_decrement(self):        self._decrement_lock = False    def _on_decrement(self, event):        if self._decrement_lock: return "break"  # stop the increment        else: # generate a virtual event corresponding to when the spinbox # is actually decremented self.event_generate('<<ActualDecrement>>') self._decrement_lock = True self.after(self.delay, self._unlock_decrement)class App(tk.Tk):    def __init__(self):        super().__init__()        self.rowconfigure(990, weight=1)        self.columnconfigure(0, weight=1)        self.title('Timed Events Demo')        self.geometry('420x200+20+20')        tk_spn1 = MySpinbox(self, value=0, values=list(range(0, 1000)))        tk_spn1.grid(row=0, pady=5)        tk_spn2 = MyTtkSpinbox(self, from_=0, to=1000)        tk_spn2.grid(row=1, pady=5)        def test(e): print(e)        tk_spn2.bind('<<ActualIncrement>>', test)if __name__ == '__main__':    app = App()    app.mainloop()


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

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

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