1

我正在用 Python 制作一个简单的 tkinter GUI,我需要放置大约 10 个或更多标签。我使用网格方法放置标签,但在一定数量后出现错误,提示“TypeError:Label object is not callable”。这是标签和网格放置的代码:

UnitPointLabel = ttk.Label(root, text = unit_points)
UnitLabel = ttk.Label(root, text = unit)
HQ1 = ttk.Label(root, text = "HQ:")
HQ2 = ttk.Label(root, text = "HQ:")
Elite1 = ttk.Label(root, text = "Elite:")
Elite2 = ttk.Label(root, text = "Elite:")
Elite3 = ttk.Label(root, text = "Elite:")
Troop1 = ttk.Label(root, text = "Troop:")
Troop2 = ttk.Label(root, text = "Troop:")
Troop3 = ttk.Label(root, text = "Troop:")
Troop4 = ttk.Label(root, text = "Troop:")
Troop5 = ttk.Label(root, text = "Troop:")
Troop6 = ttk.Label(root, text = "Troop:")
Fast_Attack1 = ttk.Label(root, text = "Fast Attack:")
Fast_Attack2 = ttk.Label(root, text = "Fast Attack:")
Heavy_Support1 = ttk.Label(root, text = "Heavy Support:")
Heavy_Support2 = ttk.Label(root, text = "Heavy Support:")
Heavy_Support3 = ttk.Label(root, text = "Heavy Support:")



UnitPointLabel.grid(row = 3, column = 7)
HQ1.grid(row = 3, column = 5)
HQ2.grid(row = 4, column = 5)
Troop1.grid(row = 5, column = 5)
Troop2.grid(row = 6, column = 5)
Troop3.grid(row = 7, column = 5)
Troop4.grid(row = 8, column = 5)
Troop5.grid(row = 9, column = 5)
Troop6.grid(row = 10, column = 5)
Fast_Attack1.grid(row = 11, column = 5)
Fast_Attack2.grid(row = 12, column = 5)

Stops working here: Heavy_Support1(row = 3, column = 6)
Heavy_Support2(row = 2, column = 6)
Heavy_Support3(row = 3, column = 6)

如您所见,我尝试移动行和列,但仍然无法正常工作。有谁知道为什么会这样?

4

1 回答 1

4

调试的第一条规则:假设错误信息是真实的。它说什么?它说“标签对象不可调用”。这意味着您有一些对象是 Label 对象的实例,并且您正在尝试调用它。例如,“foo=Label(...); ...; foo()”。

那么,看看你的代码,你想调用什么标签对象?看看这三行:

Heavy_Support1(row = 3, column = 6)
Heavy_Support2(row = 2, column = 6)
Heavy_Support3(row = 3, column = 6)

回顾您的代码,我发现 Heavy_Support1 (等)都是 Label 的实例,您确实在尝试调用它。

长话短说,您似乎忘记添加.grid这些行了。他们应该是:

Heavy_Support1.grid(row = 3, column = 6)
Heavy_Support2.grid(row = 2, column = 6)
Heavy_Support3.grid(row = 3, column = 6)
于 2012-05-28T21:20:45.240 回答