可以将多个变量绑定到tkinter消息框

时间:2016-06-01 09:34:59

标签: python user-interface python-3.x tkinter

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

    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))

它会返回此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__
    return self.func(*args)
  File "/Users/---/Documents/test.py", line 16, in compoundInterest
    lines = ['Principal Amount: %s', 'Rate: %s', 'Time: %s years.', 'Compounded: %s times a year.'] % (principal, rate, time, compound)
TypeError: unsupported operand type(s) for %: 'list' and 'tuple'

不幸的是,通过研究,我无法找到任何显示如何正确地将变量绑定到消息框的内容,因此我不知道它仅仅是可能性。如果有另一种方式我愿意听到它!

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

1 个答案:

答案 0 :(得分:3)

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

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

这有效:

tkinter.messagebox.showinfo('Compound Interest Result:', "\n".join(["test1","test2","test3"]))

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

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)]