【问题标题】:"SyntaxError: non-keyword arg after keyword arg" when trying to print values of variables in a Label尝试在标签中打印变量值时出现“语法错误:关键字 arg 后的非关键字 arg”
【发布时间】:2016-08-22 02:26:59
【问题描述】:

我只是想在Label 声明中打印变量的值,如下所示

c = Label(root, text="Enter The Number Of Fruits In Basket%d Of Type%d\n"%j,i)

但我收到以下错误

SyntaxError: 关键字 arg 后的非关键字 arg

我是否遗漏了什么,或者错误地声明了任何 arg?

【问题讨论】:

  • @ParvizKarimli OP提供的代码绰绰有余(至少在这种情况下)。
  • 你的字符串插值错了,你忘了用括号,c = Label(root, text="Enter The Number Of Fruits In Basket%d Of Type%d\n" % (j, i) )
  • 哦.. GOsh.. 愚蠢.. 非常感谢大家!!

标签: python python-2.7 tkinter


【解决方案1】:

因为您没有在j, i 周围使用括号作为格式字符串,Python 认为i 是一个变量,它作为第三个参数传递给Label() 函数,而不是格式字符串。既然你已经把text=(作为一个命名参数),那么所有后续的参数也必须命名。

j, i周围加上括号就可以了:

c = Label(root, text="Enter The Number Of Fruits In Basket%d Of Type%d\n" % (j, i))

【讨论】:

  • 这是一个愚蠢的错误。非常感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多