可以将多个变量绑定到tkinter messagebox

2024-05-29 00:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望将多个变量绑定到tkinter messagebox中。这可能吗?我确实有一些我放在一起的示例代码,加上它的错误代码。在

    lines = ['Principal Amount: %s', 'Rate: %s', 'Time: %s years.', 'Compounded: %s times a year.'] % (principal, rate, time, compound)
    tkinter.messagebox.showinfo('Compound Interest Result:', "\n".join(lines))

它返回以下错误:

^{2}$

不幸的是,通过研究,我还没有找到任何能说明如何正确地将变量绑定到MessageBox的东西,所以我不知道这仅仅是一种可能性。如果还有别的方法我愿意听!在

(请注意,我是Python和tkinter的新手)


Tags: 代码principal示例ratetimetkinteramountyear
1条回答
网友
1楼 · 发布于 2024-05-29 00:27:10

问题不在于messagebox,而在于错误消息中提到的行的定义:

TypeError: unsupported operand type(s) for %: 'list' and 'tuple'

这是有效的:

^{pr2}$

例如,要创建,可以执行以下操作:

sList=['Principal Amount: {}', 'Rate: {}', 'Time: {} years.', 'Compounded: {} times a year.']
valueList=['test1', 'test2','test3','test4']
lines = [s.format(value) for s,value in zip(sList,valueList)]

相关问题 更多 >

    热门问题