使用tkinter.entry使用exec()执行tkinter.entry时的syntaxerror。

发布于 2025-02-10 22:47:40 字数 831 浏览 0 评论 0原文

我正在尝试创建一个程序,其中包含各种下拉菜单和可写的领域。为了简化添加新的菜单和可写的字段,我想循环浏览包含创建动态变量名称和菜单所需信息的元组列表。

我遇到的问题是运行以下代码,其中“名称”是字符串“百分比”。应该注意的是,如果我删除“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文