python - 如何在tkinter中将 'text'按钮彼此进行比较?

标签 python python-3.x tkinter error-handling tk

我一直在tic-tac-toe项目中工作,而我正在面对这个问题,那就是。如何通过循环获取tkinter Button['text']文本值?

是否可以(如果没有其他方法)?

我会尝试进行比较,但是它不起作用,我也查看了一些文档,但会得到任何帮助。

my code on github

,我尝试:

for i in range(1, 10):
     print(f'self.btn{i}[\'text\']')

返回:
self.btn1['text'] <<---- problem this result.
self.btn2['text'] 
self.btn3['text'] 
self.btn4['text'] 
self.btn5['text'] 
self.btn6['text'] 
self.btn7['text'] 
self.btn8['text'] 
self.btn9['text'] 

# Insted this I want Button values of tkinter

如何更好地获得这些值(value)?

最佳答案

要检查按钮,您不能使用string和print()-它们只能创建字符串并在屏幕上显示。

您必须像这样使用for循环

result = False

for btn in all_buttons:
    if btn['text'] == ' ':
        result = True
        break # there is no need to check rest

print("There are empty buttons:", result)

要么
result = False

for btn in all_buttons:
    result = result or (btn['text'] == ' '):

print("There are empty buttons:", result)

(有时可能需要or而不是and-所有取决于您尝试检查的内容-anyall)

或者您可以使用any()all()
result = any(btn['text'] == ' ' for btn in all_buttons)
print("There are empty buttons:", result)

您也可以将其与sum()一起使用以进行计数
result = sum(btn['text'] == 'X' for btn in all_buttons)
print("Number of buttons with X:", result)
import tkinter as tk
import random
root = tk.Tk()

all_buttons = []

for row in range(3):
    for col in range(3):
         #btn = tk.Button(root, text=str(row*3+col))
         btn = tk.Button(root, text=random.choice(['X', 'O', ' ']))
         all_buttons.append(btn)
         btn.grid(row=row, column=col, sticky='news')

for btn in all_buttons:
    print('>', btn['text'], '<')

#---

result = sum(btn['text'] == 'X' for btn in all_buttons)
print("Number of buttons with X:", result)

result = sum(btn['text'] == 'O' for btn in all_buttons)
print("Number of buttons with O:", result)

result = sum(btn['text'] == ' ' for btn in all_buttons)
print("Number of empty buttons:", result)

print('---')

result = False
for btn in all_buttons:
    if btn['text'] == ' ':
        result = True
        break
print("There are empty buttons:", result)
print('---')

# the same using `any()` or `all()`    
result = any(btn['text'] == ' ' for btn in all_buttons)
print("Any button is empty:", result)
print("All buttons are used:", not result)
print('---')

result = all(btn['text'] != ' ' for btn in all_buttons)
print("Any button is empty:", not result)
print("All buttons are used:", result)
print('---')

result = all(btn['text'] == 'X' for btn in all_buttons)
print("Any button doesn't have X:", not result)
print("All buttons have X:", result)
print('---')

result = all(btn['text'] in ('O', 'X') for btn in all_buttons)
print("Any button doesn't have O or X:", not result)
print("All buttons have O or X:", result)
print('---')

rows = [
    all_buttons[0:3],
    all_buttons[3:6],
    all_buttons[6:9],
]    

for number, row in enumerate(rows, 1):
    result = all(btn['text'] == 'X' for btn in row)
    print("Row", number, "has three X (X wins):", result)
print('---')

columns = [
    [all_buttons[0], all_buttons[3], all_buttons[6]],
    [all_buttons[1], all_buttons[4], all_buttons[7]],
    [all_buttons[2], all_buttons[5], all_buttons[8]],    
]

for number, col in enumerate(columns, 1):
    result = all(btn['text'] == 'X' for btn in col)
    print("Column", number, "has three X (X wins):", result)

tk.mainloop()    

关于python - 如何在tkinter中将 'text'按钮彼此进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57942559/

相关文章:

django - DRF - 序列化多个模型

python - 在完成绘制之前关闭 python turtle 时获取 tkinter.tclerror

python - PyAudio 不会安装

python - 对字典进行排序但出现 "List Indices must be int, not tuple"错误

python - Django runserver 报错,全新安装,Django 初次使用者

python - 正则表达式拆分连续的换行符

Python - 显示 json 数据

python - 无法将日期转换为所需格式

python - 如何 bundle 含依赖项的 Python 应用程序?

python - 所有对象默认 Sticky=W (Tkinter)