Python 我不能使用标签传递变量

Python 我不能使用标签传递变量,python,python-3.x,tkinter,label,Python,Python 3.x,Tkinter,Label,我开始编写一个tkinter程序时,在代码中偶然发现了这个问题: elif (xtimes=="" or xtimes=="Optional") and (t!="" or t!="Optional"): amount years=0 while years<t: principle=principle+((interest/100)*(principle))

我开始编写一个
tkinter
程序时,在代码中偶然发现了这个问题:

 elif (xtimes=="" or xtimes=="Optional") and (t!="" or t!="Optional"):
    amount
    years=0
    while years<t:
        principle=principle+((interest/100)*(principle))
        years+=1

    amount=principle
    finallabel=Label(root,text="Your Final Amount will be",amount,"at the end of",years,"years")
    finallabel.grid(row=13,column=0)
elif(xtimes==“或xtimes==“可选”)和(t!=“或t!=“可选”):
数量
年=0

而年份唯一需要传递的位置参数是
Label(root)
。 因此,如果您添加标签
(text='my text',root)
,则会出现此错误

这项工作:

import tkinter as tk
root = tk.Tk()

lab = tk.Label(root, text='hi')
lab.pack()
root.mainloop()
这不是:

import tkinter as tk
root = tk.Tk()

lab = tk.Label(text='hi',root)
lab.pack()
root.mainloop()
更新后。。让我们看看这一行代码:

finallabel=Label(root,text="Your Final Amount will be",amount,"at the end of",years,"years")
在这里,您所做的是通过Label类的接口解析参数,并使用给定参数的配置生成其实例

tkinter标签类知道可以找到的参数

因此,将标签与可用参数进行比较,您会注意到,
金额
年份
不在其中。tkinter的Label类需要的唯一Posistional参数是
主参数
,后跟关键字参数
**选项

您试图做的是一个带有变量的字符串,有几种方法可以实现这一点。我个人最喜欢的是。 使用f'字符串,您的代码将如下所示:

finallabel=Label(root,text=f'Your Final Amount will be {amount},at the end of {years} years')

如果有不清楚的地方,请告诉我。

欢迎来到StackOverflow。请将您的代码以电子邮件的形式发布。如果没有代码,很难看出哪里出了问题。请添加一些代码示例此语句毫无意义:“您需要传递的唯一关键字参数是Label(root)”-
root
不是关键字参数。你的意思是写“唯一的位置参数…”吗?我已经发布了代码…我需要用标签显示答案,但我得到了错误。请帮忙,非常感谢@Atlas435