如何使用 lambda 访问函数给出的标签

问题描述 投票:0回答:1

我两周前开始编码,所以我是一个初学者,可能有一个简单的解决方案可以解决我的问题,但我无法解决它。目前我正在尝试使用 tkinter 在 Python 中构建 Tic Tac Toe。现在,要检查某人是否获胜,我需要能够访问单击一个按钮时出现的标签中的文本。

我的代码:

import tkinter as tk
count = 2


def spielfeld_generieren():
    Startknopf.destroy()

    ueberschrift = tk.Label(Display, text="X Beginnt!", font=("Arial", 10))
    ueberschrift.grid(row=0, column=0, columnspan=3)

    first = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(first, 1, 0))
    first.grid(row=1, column=0, )

    second = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(second, 1, 1))
    second.grid(row=1, column=1)

    third = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(third, 1, 2))
    third.grid(row=1, column=2)

    fourth = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(fourth, 2, 0))
    fourth.grid(row=2, column=0)

    fifth = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(fifth, 2, 1))
    fifth.grid(row=2, column=1)

    six = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(six, 2, 2))
    six.grid(row=2, column=2)

    seven = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(seven, 3, 0))
    seven.grid(row=3, column=0)

    eight = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(eight, 3, 1))
    eight.grid(row=3, column=1)

    nine = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(nine, 3, 2))
    nine.grid(row=3, column=2)


def nutzereingabe(position, a, b):
    position.destroy()
    global count
    if count % 2 == 0:
        ausgabe = "X"
        color = "red"
    else:
        ausgabe = "O"
        color = "black"
    name = tk.Label(Display, text=ausgabe, fg=color, font=("Arial", 100), height=1, width=2)
    name.grid(row=a, column=b)
    count += 1


Display = tk.Tk()
Display.title("Tic Tac Toe")

Startknopf = tk.Button(Display, text="Start the game", command=spielfeld_generieren)
Startknopf.pack()


Display.mainloop()

最初我认为标签将具有变量参数“位置”的名称(如“第一”,“第二”,“第三”...)。但事实并非如此,因为然后我收到此名称未定义的错误。我还尝试重命名标签,但为此我还需要它们的名称。

python tkinter
1个回答
0
投票
I need to be able to access the text in the labels which appear when one button is clicked.

问题可以解决。

  • Display = tk.Tk()
    移至顶部
    spielfeld_generieren()
    之前 函数'
  • ueberschrift
    小部件移到功能之外。
  • ueberschrift.configure
    功能中添加
    spielfeld_generieren()
  • 同时添加
    ueberschrift.configure(text="O turn!")
    ueberschrift.configure(fg='black')
    位于
    nutzereingabe
    函数内。
  • 同时添加
    ueberschrift.configure(text="X turn!")
    ueberschrift.configure(fg='red')
    位于
    nutzereingabe
    函数内。
  • pack()
    更改为 Startknopf.grid()`

重新修改片段:

import tkinter as tk
count = 2

Display = tk.Tk()

def spielfeld_generieren():
    Startknopf.destroy()
    ueberschrift.configure(text="X Beginnt!")

    first = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(first, 1, 0))
    first.grid(row=1, column=0, )

    second = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(second, 1, 1))
    second.grid(row=1, column=1)

    third = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(third, 1, 2))
    third.grid(row=1, column=2)

    fourth = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(fourth, 2, 0))
    fourth.grid(row=2, column=0)

    fifth = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(fifth, 2, 1))
    fifth.grid(row=2, column=1)

    six = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(six, 2, 2))
    six.grid(row=2, column=2)

    seven = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(seven, 3, 0))
    seven.grid(row=3, column=0)

    eight = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(eight, 3, 1))
    eight.grid(row=3, column=1)

    nine = tk.Button(Display, height=10, width=20, command=lambda: nutzereingabe(nine, 3, 2))
    nine.grid(row=3, column=2)


def nutzereingabe(position, a, b):
    position.destroy()
    global count
    if count % 2 == 0:
        ausgabe = "X"
        color = "red"
        ueberschrift.configure(text="O turn!")
        ueberschrift.configure(fg='black')
    else:
        ausgabe = "O"
        color = "black"
        ueberschrift.configure(text="X Turn")
        ueberschrift.configure(fg='red')
    name = tk.Label(Display, text=ausgabe, fg=color, font=("Arial", 100), height=1, width=2)
    name.grid(row=a, column=b)
    count += 1

ueberschrift = tk.Label(Display, font=("Arial", 10))
ueberschrift.grid(row=0, column=0, columnspan=3)

 
Display.title("Tic Tac Toe")

Startknopf = tk.Button(Display, text="Start the game", command=spielfeld_generieren)
Startknopf.grid()


Display.mainloop()

截图:

© www.soinside.com 2019 - 2024. All rights reserved.