Python - 如何防止按钮像洪水一样被按下?暂时停用按钮操作以停止暴力破解

标签 python linux user-interface gtk pygtk

我有三个按钮。单击时的每个按钮都会向端口 58888 发送一条命令,该端口有一些工作,一旦工作完成,基本上该特定按钮应该在 5 分钟后可以再次按下。

知道如何阻止按钮吗?以至于有人暂时无法在那个按钮上产生泛洪??

  button = gtk.Button("a")
  button.connect("clicked" , self.on_the_fly("a") , None)
  bbox.add(button)

  button = gtk.Button("v")
  button.connect("clicked" , self.on_the_fly("v") , None)
  bbox.add(button)

  button = gtk.Button("b")
  button.connect("clicked" , self.on_the_fly("b") , None)
  bbox.add(button)

按钮使用的方法:

  def on_the_fly(self, input):

    logging.debug(  'Clicked at x={0}, y={0}' )

    try:

      if self.myname.startswith("enduser"):
        logging.debug("reconnect")

      else:

        if input.startswith("a"):

          command = "r0x01"
          client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          client_socket.connect(("localhost", 58888))
          client_socket.send(command)
          client_socket.close()

        elif input.startswith("v"):

          command = "r0x02"
          client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          client_socket.connect(("localhost", 58888))
          client_socket.send(command)
          client_socket.close()

        elif input.startswith("both"):

          command = "r0x03"
          client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          client_socket.connect(("localhost", 58888))
          client_socket.send(command)
          client_socket.close()

        else:

          print "nothing"

        subprocess.Popen(["/var/tmp/runme.sh"], stdout=subprocess.PIPE)

    except Exception, err:
      logging.debug(  err )
      pass

最佳答案

您需要使按钮“不敏感”(在 Gtk 中表示“停用”、“禁用”或“变灰”),然后设置一个计时器,然后在 5 分钟后重新使其敏感。

这意味着您要将按钮本身(而不仅仅是字母)传递给命令函数。

向您展示如何执行此操作比应该做的要难一些,因为您的命令函数已经出错了。您在每个 connect 调用中调用 self.on_the_fly,并将按钮连接到返回值(None ).因此,您的程序在启动时调用 on_the_fly 三次,然后记录有关 None 的异常,无论何时单击按钮都无法调用。你想要的是以下之一:

button.connect("clicked", self.on_the_fly, "a")

button.connect("clicked", lambda: self.on_the_fly("a"), None)

因此,您传递按钮小部件的方式也可以是:

button.connect("clicked", self.on_the_fly, ("a", button))

button.connect("clicked", lambda: self.on_the_fly("a", button), None)

然后,像这样更改 on_the_fly 方法:

def on_the_fly(self, input, widget):
    logging.debug(  'Clicked at x={0}, y={0}' )
    widget.set_sensitive(False)
    gtk.timeout_add_seconds(5*60, widget.set_sensitive, True)
    # rest of function

关于Python - 如何防止按钮像洪水一样被按下?暂时停用按钮操作以停止暴力破解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284004/

相关文章:

python - 获取条件下唯一值的计数

python - 列表推导和生成器表达式中的 yield

linux - $(find -X) 等价于 linux

python - python的WPF替代品

python - 在wxpython中保存组合框的选定值

java - Swing:重置 ObjectOutputStream 时出现 EDT NullPointerException

python - 在 TensorFlow 中对 5-D 张量进行上采样

python - 使用 qcut pandas 进行多个有值(value)的分类

linux - path/tmp 不对应于常规文件

linux - 是给linux终端中的普通文本着色吗?