【问题标题】:clearing an entry widget box清除条目小部件框
【发布时间】:2012-09-28 10:55:49
【问题描述】:

我想使用条目小部件来获取 1 到 9 之间的数字。如果按下任何其他键,我想将其从显示中移除。

    def onKeyPress(event):
        if event.char in ['1', '2', '3', '4', '5', '6', '7', '8', '9']
            ...do something
            return

        # HERE I TRY AND REMOVE AN INVALID CHARACTER FROM THE SCREEN
        # at this point the character is:
        #   1) visible on the screen
        #   2) held in the event
        #   3) NOT YET in the entry widgets string
        # as the following code shows...
        print ">>>>", event.char, ">>>>", self._entry.get()
        # it appeARS that the entry widget string buffer is always 1 character behind the event handler

        # so the following code WILL NOT remove it from the screen...
        self._entry.delete(0, END)
        self._entry.insert(0, "   ")

    # here i bind the event handler    
    self._entry.bind('<Key>',  onKeyPress)

好的,那我怎样才能清屏呢?

【问题讨论】:

    标签: python tkinter widget tkinter-entry


    【解决方案1】:

    您进行输入验证的方式是错误的。你所问的不能用你发布的代码来完成。一方面,正如您所发现的,当您在&lt;&lt;Key&gt;&gt; 上绑定时,默认情况下该绑定会在 字符出现在小部件中之前触发。

    我可以为您提供解决方法,但正确的答案是使用内置工具进行输入验证。请参阅条目小部件的 validatecommandvalidate 属性。 This answer 到问题Interactively validating Entry widget content in tkinter 将告诉你如何做。该答案显示了如何针对上/下进行验证,但是很容易将其更改为与一组有效字符进行比较。

    【讨论】:

      【解决方案2】:
      import Tkinter as tk
      
      class MyApp():
          def __init__(self):
              self.root = tk.Tk()
              vcmd = (self.root.register(self.OnValidate), 
                      '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
              self.entry = tk.Entry(self.root, validate="key", 
                                    validatecommand=vcmd)
              self.entry.pack()
              self.root.mainloop()
      
          def OnValidate(self, d, i, P, s, S, v, V, W):
              # only allow integers 1-9
              if P == "":
                  return True
              try:
                  newvalue = int(P)
              except ValueError:
                  return False
              else:
                  if newvalue > 0 and newvalue < 10:
                      return True
                  else:
                      return False
      
      app=MyApp()
      

      取自 this 答案,修改后的验证只允许整数 1-9。

      (我确信有更好的方法来编写该验证,但据我所知,它可以完成这项工作。)

      【讨论】:

      • 在这种特殊情况下,我建议您删除所有这些额外参数,因为您不使用它们。出于说明目的,我将它们放在其他答案中,但在这里您没有使用它们,因此它们只会使解决方案变得比需要的更复杂。
      • 感谢 Bryan 和 Jdog,我会试试这个验证码。如果可能,我想保留 OnKeyPress 事件处理程序 - 如果输入有效,我实际上使用它来移动到另一个条目小部件;所以一个后续问题 - 你可以一起使用验证和事件处理程序吗?如果可以的话,哪个是第一个验证或事件处理程序?
      【解决方案3】:

      清除条目小部件的简单方法是:

      from tkinter import *
      
      tk = Tk()
      
      
      # create entry widget
      
      evalue = StringVar()
      e = Entry(tk,width=20,textvariable=evalue)
      e.pack()
      
      
      def clear(evt):
          evalue.set("")   # the line that removes text from Entry widget
      
      tk.bind_all('<KeyPress-Return>',clear)  #clears when Enter is pressed
      tk.mainloop()
      

      你可以在任何你想要的环境中使用它,这只是一个例子

      【讨论】:

        猜你喜欢
        • 2014-10-06
        • 1970-01-01
        • 1970-01-01
        • 2015-03-19
        • 1970-01-01
        • 2012-02-08
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        相关资源
        最近更新 更多