gpt4 book ai didi

Python tkInter入门乐趣

转载 作者:行者123 更新时间:2023-11-28 21:30:54 24 4
gpt4 key购买 nike

玩弄 Python - tkInter - Entry 小部件 - 当我使用 validatecommand(如下)时,第一次检查字符串 > Max,但是当我继续输入文本时检查步骤 - 在第一次?有什么建议吗? (除了不通过 python 构建桌面应用程序之外)


#!/usr/bin/env python
from Tkinter import *

class MyEntry(Entry):

def __init__(self, master, maxchars):
Entry.__init__(self, master, validate = "key", validatecommand=self.validatecommand)
self.MAX = maxchars

def validatecommand(self, *args):
if len(self.get()) >= self.MAX:
self.delete(0,3)
self.insert(0, "no")
return True

if __name__ == '__main__':
tkmain = Tk()
e = MyEntry(tkmain, 5)
e.grid()
tkmain.mainloop()

最佳答案

From the Tk man :

The validate option will also set itself to none when you edit the entry widget from within either the validateCommand or the invalidCommand. Such editions will override the one that was being validated. If you wish to edit the entry widget (for example set it to {}) during validation and still have the validate option set, you should include the command

after idle {%W config -validate %v}

不知道如何将其转换为 python。

关于Python tkInter入门乐趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1019782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com