使用tkinter.entry使用exec()执行tkinter.entry时的syntaxerror。
我正在尝试创建一个程序,其中包含各种下拉菜单和可写的领域。为了简化添加新的菜单和可写的字段,我想循环浏览包含创建动态变量名称和菜单所需信息的元组列表。
我遇到的问题是运行以下代码,其中“名称”是字符串“百分比”。应该注意的是,如果我删除“ validate”和“ validatecommand”,则代码有效,尽管显然没有完成验证。
def number_validate(char):
return char.isdigit()
self.percentage_validification = self.root.register(number_validate)
exec("self." + name + "_box" + " = " + "tkinter.Entry(self.root, validate = 'key', self.validatecommand = (self.validation, '%S'))")
以上代码给出以下错误:
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
以下代码给出了正确的输出,但不允许动态变量命名和创建,这表明命令本身不是问题:
self.Percentage_box = tkinter.Entry(self.root, validate = "key", validatecommand = (self.percentage_validification, '%S'))
我假设我的问题与处理字符串的不当处理有关Exec功能,但我根本不知道应该如何完成。有人有答案吗?
I am trying to create a program containing various drop down menus and writable fields. To simplify adding new menus and writable fields i wanted to loop over a list of tuples containing the information required to create dynamic variable names and the menus.
The problem i am running into is when running the below code where 'name' is the string "Percentage". It should be noted that the code works if i remove 'validate' and 'validatecommand', though obviously the validation isn't done.
def number_validate(char):
return char.isdigit()
self.percentage_validification = self.root.register(number_validate)
exec("self." + name + "_box" + " = " + "tkinter.Entry(self.root, validate = 'key', self.validatecommand = (self.validation, '%S'))")
The above code gives the following error:
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
The following code gives the correct output but does not allow dynamic variable naming and creation, which indicates that it is not a problem with the command itself:
self.Percentage_box = tkinter.Entry(self.root, validate = "key", validatecommand = (self.percentage_validification, '%S'))
I assume my problem relates to improper handling of strings in the exec function, but i simply can't figure out how is should be done. Does anyone have an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论