我目前正在学习 tkinter 基础知识,并且正在构建一个小型、超级简单的程序来测试我对一些最基本的小部件的了解。
我在验证和条目方面遇到问题,可能是因为我对此事缺乏了解......这提出了三个问题:
1 - 如何做这里所做的事情:https ://stackoverflow.com/a/4140988/2828287没有类部分。只需在脚本运行时执行此操作。
2 - 那些自我是什么。和 .self 在那里做什么?哪些是因为那是一个类,哪些是因为验证方法本身?
3 - 我的代码有什么问题?基于这个解释>> http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry-validation.html
from tkinter import *
from tkinter import ttk
# function that should take the '%d' replacer and only validate if the user didn't delete
def isOkay(self, why):
if why == 0:
return False
else:
return True
okay = entry.register(isOkay) # didn't understand why I had to do this, but did it anyway...
entry = ttk.Entry(mainframe, validate="key", validatecommand=(okay, '%d'))
# the mainframe above is a ttk.Frame that contains all the widgets, and is the only child of the usual root [ = Tk()]
entry.grid(column=1,row=10) # already have lots of stuff on upper rows
我得到的错误是这样的:“NameError:name 'entry' is not defined”我试图改变事情的顺序,但总是有这些错误之一..它指向我做的行.register() 东西
--EDITED CODE-- 这不会给我一个错误,但仍然允许我删除......
def isOkay(why):
if (why == 0):
return False
else:
return True
okay = (**root**.register(isOkay), "%d")
entry = ttk.Entry(mainframe, validate="key", validatecommand=okay)
entry.grid(column=1,row=10)
(“根”部分写在** **之间,它必须是根吗?或者它可以是要使用它的小部件的任何父级?或者它必须是它的直接父级?例如,我有:root >> mainframe >> entry。它必须是 root、大型机还是两者都可以?)