首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在长印刷机上用tkinter添加数字?

问如何在长印刷机上用tkinter添加数字?
EN

Stack Overflow用户
提问于 2020-09-23 10:23:03
回答 1查看 23关注 0票数 0

在tkinter GUI中,我有一些数字,当按下长键时,它应该继续与前一个数字相加。按键松开后停止。我已经尝试了greenbutton.bind('<ButtonPress-1>', start_motor)不同的事件,我无法解决。

EN

回答 1

Stack Overflow用户

发布于 2020-09-23 10:38:26

没有内置的方法可以做到这一点,你需要手动监控按钮的按下和释放。这将是一个使用Button子类的好地方。如下所示:

代码语言:javascript
复制
import tkinter as tk

class LongPressButton(tk.Button):
    def __init__(self, master=None, command=None, repeat_time=250, **kwargs):
        super().__init__(master, **kwargs)
        self.command = command
        self.repeat_time = repeat_time
        self.command_trigger = ' '
        self.bind('<ButtonPress-1>', self.on_press)
        self.bind('<ButtonRelease-1>', self.on_release)

    def on_press(self, event=None):
        if self.command is not None:
            self.command()
        self.command_trigger = self.after(self.repeat_time, self.on_press)

    def on_release(self, event=None):
        self.after_cancel(self.command_trigger)

### TEST / DEMO:
def tick_func():
    print("tick")

root = tk.Tk()
root.geometry('200x200')
greenbutton = LongPressButton(root, text='click and hold me!!', bg='green', command=tick_func)
greenbutton.pack()
bluebutton = LongPressButton(root, text="I'm faster!!", bg='light blue', command=tick_func, repeat_time=80)
bluebutton.pack()
root.mainloop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64020125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档