简体   繁体   English

为什么tkinter-buttons全部禁用?

[英]Why tkinter-buttons do all disable?

I am following a beginner's Python guide book and I am stuck with one particular task with tkinter. 我正在关注初学者的Python指南,而tkinter却使我无法完成一项特定任务。 I've followed the code from the book but it still does not seem to work properly: 我遵循了本书中的代码,但是它似乎仍然无法正常工作:

The purpose is to make a guessing game where buttons are pressed two times. 目的是制作一个猜测游戏,其中两次按下按钮。 If the user finds two same symbols after guessing two times in a row, those buttons/symbols are disabled and stay visible. 如果用户连续猜测两次后发现两个相同的符号,则这些按钮/符号将被禁用并保持可见状态。 In other case, they are hidden and guessing starts again. 在其他情况下,它们将被隐藏,并且再次开始猜测。 Problem is: After pressing the buttons they all stay visible. 问题是:按下按钮后,它们都保持可见状态。 Please see attachment. 请查看附件。 Screenshot of outcome 结果截图

For doing this code I am using Jupyter Notebook 5.5.0 which has worked well with other exercises in the book. 为了执行此代码,我使用Jupyter Notebook 5.5.0,它与本书中的其他练习效果很好。 I was wondering if it is about the notebook (also the graphics look different here than in the book) I am using or just a bug in the code? 我想知道这是否与我正在使用的笔记本有关(此处的图形与本书中的图形有所不同)还是代码中的错误?

Thanks in advance! 提前致谢!

import random
import time
from tkinter import Tk, Button, DISABLED 

def show_symbol(x, y): 

    global first 
    global previousX, previousY
    buttons[x, y]["text"] = button_symbols[x, y] 
    buttons[x, y].update_idletasks() 

    if first: 

        previousX = x 
        previousY = y
        first = False 

    elif previousX != x or previousY != y: 

        if buttons[previousX, previousY]["text"] != buttons[previousX, previousY]["text"]: 

            time.sleep(0.5) 
            buttons[previousX, previousY]["text"] = ""
            buttons[x, y]["text"] = ""

        else: 

            buttons[previousX, previousY]["command"] = DISABLED
            buttons[x, y]["command"] = DISABLED 

        first = True 

root = Tk()
root.title("Find a pair")
root.geometry("500x500") 
root.resizable(width=False, height=False) 

buttons = {}
first = True
previousX = 0 
previousY = 0 

button_symbols = {}
symbols = [u"\u2702", u"\u2702", u"\u2705", u"\u2705", u"\u2708", u"\u2708", u"\u2709", u"\u2709", u"\u270A", u"\u270A",
          u"\u270B", u"\u270B", u"\u270C", u"\u270C", u"\u270F", u"\u270F", u"\u2712", u"\u2712", u"\u2714", u"\u2714",
          u"\u2716", u"\u2716", u"\u2728", u"\u2728"]

random.shuffle(symbols)

for x in range(6): 

    for y in range(4):

        button = Button(command=lambda x=x, y=y: show_symbol(x, y), width=3, height=3)
        button.grid(column=x, row=y) 
        buttons[x, y] = button 
        button_symbols[x, y] = symbols.pop() 

root.mainloop()

The following if statement will always return false because it is checking if itself is not equal to itself . 以下if语句将始终返回false,因为它正在检查自身是否不等于其自身 A simple oversight ;) 一个简单的监督;)

if buttons[previousX, previousY]["text"] != buttons[previousX, previousY]["text"]: 

Just change it to the following: 只需将其更改为以下内容:

if buttons[previousX, previousY]["text"] != buttons[x, y]["text"]: 

I tested your code and it works with that change 我测试了您的代码,并且该更改可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM