在 for 循环创建标签中将元组转换为文本

Convert Tuple to Text in the for cycle creating labels

我是编程新手,我想学习 Python。 我有以下任务: 从元组创建或列出一个矩阵(window 与 tkinter),元组中每个对象的标签与对象的相同 "name"。

import tkinter as tk

cards = ("AA", "AKs", "AQs", "AJs", "ATs", "A9s", "A8s", "A7s", "A6s", "A5s", "A4s", "A3s", "A2s", "AKo", "KK", "KQs")

root = tk.Tk()

for i in carte:
    label = tk.Label(root, **text = cards()** , bg="black", fg="white")


root.mainloop()

感谢任何建议

您的代码中存在一些错误:

  1. 卡片是 Tuple,你用 cards(),这是不正确的。
  2. for i in carte 有拼写错误。
  3. 在您创建变量 label 之后,您还没有使用 pack()place()grid() 将其放入您的应用程序。

现在代码应该是:

import tkinter as tk

cards = ("AA", "AKs", "AQs", "AJs", "ATs", "A9s", "A8s", "A7s", "A6s", "A5s", "A4s", "A3s", "A2s", "AKo", "KK", "KQs")

root = tk.Tk()

for i in cards:
    label = tk.Label(root, text = i , bg="black", fg="white")
    label.grid()

root.mainloop()