【问题标题】:Check if a Tkinter button remains clicked检查 Tkinter 按钮是否保持单击状态
【发布时间】:2018-11-30 12:14:13
【问题描述】:

我想制作 2 个可以改变音量的按钮(+ 和 -)。但是如果我想进一步增加音量,我想可以按住按钮。任何想法如何检查用户是否按下按钮?

def volumeUp():
    currentVolume = m.getvolume()[0]
    volumeSlider.set(currentVolume + 5)

def volumeDown():
    currentVolume = m.getvolume()[0]
    volumeSlider.set(currentVolume - 5)

volumeDownButton = Button(win, text = "-", font = myFont, command =  volumeDown, height = 1, width = 1)
volumeDownButton.pack(side = BOTTOM)

volumeUpButton = Button(win, text = "+", font = myFont, command = volumeUp,   height = 1, width = 1)
volumeUpButton.pack(side = BOTTOM)

【问题讨论】:

    标签: python button tkinter


    【解决方案1】:

    您可以做的是让 Button press fire 成为一个改变音量的功能,然后安排自己在一定时间(例如 100 毫秒)后再次运行。然后当松开按钮时,您可以取消改变音量的功能的预定重复以打破循环。

    我对您的代码稍作修改以作为示例:

    from tkinter import *
    
    win = Tk()
    
    def volumeUpPress(e=None):
        global up_after
        currentVolume = volumeSlider.get()
        volumeSlider.set(currentVolume + 2)
        up_after = win.after(100, volumeUpPress)
    
    def volumeUpRelease(e=None):
        global up_after
        win.after_cancel(up_after)
    
    
    def volumeDownPress(e=None):
        global down_after
        currentVolume = volumeSlider.get()
        volumeSlider.set(currentVolume - 2)
        down_after = win.after(100, volumeDownPress)
    
    def volumeDownRelease(e=None):
        global down_after
        win.after_cancel(down_after)
    
    
    volumeSlider = Scale(win, from_=0, to=100, orient=HORIZONTAL)
    volumeSlider.pack()
    
    volumeDownButton = Button(win, text = "-", height = 1, width = 1)
    volumeDownButton.pack(side = BOTTOM)
    volumeDownButton.bind("<Button-1>", volumeDownPress)
    volumeDownButton.bind("<ButtonRelease-1>", volumeDownRelease)
    
    volumeUpButton = Button(win, text = "+", height = 1, width = 1)
    volumeUpButton.pack(side = BOTTOM)
    volumeUpButton.bind("<Button-1>", volumeUpPress)
    volumeUpButton.bind("<ButtonRelease-1>", volumeUpRelease)
    
    win.mainloop()
    

    注意事项:

    • 我没有使用 Button 的 command,因为它不能让您灵活地知道何时释放 Button。相反,我做了两个绑定,一个用于按下按钮时,一个用于释放按钮时。
    • 我使用after 方法在设定的毫秒数后安排对同一函数的新调用,我将对该预定函数的引用保存在全局变量中,以便能够在发布函数中再次使用它用after_cancel 取消它
    • .bind 调用带有事件对象的函数,而 after 调用不带参数的函数,因为两者都调用同一个函数,所以我这样做是为了使函数可以带参数和不带参数调用 (e=None)

    【讨论】:

      【解决方案2】:

      也使用afterfhdrsdg's answer 的替代方法是测量Buttonstate 值并检测它当前是否为active,为此我们将一个函数绑定到@987654326 @ 检查state,如果stateactive,则增加一个值,然后使用after 在短暂延迟后再次调用该函数:

      from tkinter import *
      
      class App():
          def __init__(self, root):
              self.root = root
              self.button = Button(self.root, text="Increment")
      
              self.value = 0
      
              self.button.pack()
              self.button.bind("<Button-1>", lambda x: self.root.after(10, self.increment))
      
          def increment(self, *args):
              if self.button["state"] == "active":
                  self.value += 1
                  print(self.value)
                  self.root.after(10, self.increment)
      
      root = Tk()
      App(root)
      root.mainloop()
      

      【讨论】:

      • 这很漂亮!
      • @fhdrsdg 谢谢,您的解决方案给了我灵感。
      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 2019-10-04
      • 2021-05-29
      • 1970-01-01
      • 2015-10-02
      相关资源
      最近更新 更多