防止在“之后”暂停期间单击make按钮?

时间:2019-02-17 12:23:12

标签: python tkinter

我正在用tkinter编写著名的“查找对”游戏的Python代码。一切正常,除了我无法删除的故障。用户单击了两个不同的按钮后,该程序应该:

  • 得分+ = 1,如果找到的数字相同,则将2个按钮设置为绿色背景。
  • 让2个按钮显示1.5秒钟,然后将两个按钮隐藏在一起(如果它们不相同)。

一切正常,但是如果用户在“ after”生成的暂停期间按下第3,第4,...按钮,则程序会转动第3,第4,...按钮并且不会将其隐藏

您会找到我的文件和标准功能文件here

Fonctionsplus.py

import os, shutil, math, numpy, functools, time
from tkinter import * 
from functools import partial

def posMat(x, test): #renvoie la position de l'élément "x" dans la matrice "test"
    a = -1
    b = temp = 0
    while a < (len(test)-1):
        if temp == 1:
            break
        b = 0
        a += 1
        while b < len(test):
            if str(test[a][b]) == str(x):
                tempa = a + 1
                tempb = b + 1
                matrice = [a + 1, b + 1]
                matrice = numpy.array(matrice)
                return matrice
                temp = 1
                break
            b += 1

def matrice(a, b, c):           #crée une matrice de taille "a x b" et y met des "c"
    matrice = []
    for i in range(a):
        matrice.append([c] * b)
    return numpy.array(matrice)

def moveaf(source, dest1):  #déplace tout le contenu d'un dossier SANS le dossier source
    files = os.listdir(source)
    for f in files:
        shutil.move(source+f, dest1)

def checkint(nom):          #demande de taper un entier, pas de texte
    temp = 0
    while temp == 0:
        x = input("\nVeuillez introduire un chiffre (" + nom + ") : ")
        try:
            x = int(x)
        except:
            print("\nOn vous demande un chiffre entier (" + nom + ")!!!\n")
            input("Appuyer sur ENTER...")
            temp = 0
        else:
            temp = 1
            return x

Trouve les Paires.pyw

from Fonctionsplus.Fonctionsplus import *

# Opérations
lignes = colonnes = 1
while (lignes % 2 == 1 and colonnes % 2 == 1) or lignes == 0 or colonnes == 0:
    lignes = int(input("Nombre de lignes : "))
    colonnes = int(input("Nombre de colonnes : "))
    if lignes == 0 or colonnes == 0:
        print("\nAucun des 2 ne peut valoir 0.\n")
    if lignes % 2 == 1 and colonnes % 2 == 1:
        print ("\nPour obtenir un nombre pair d'éléments, il faut que soit #lignes soit #colonnes soit les 2 soient pairs.\n")

mat = matrice(lignes, colonnes, "XXXXXXXXXX")

# crée la matrice mattemp avec les lignes*colonnes éléments disposés en random
mattemp = []
mattemp = numpy.arange(int(lignes*colonnes/2))
mattemp = numpy.append(mattemp, mattemp, 0)
numpy.random.shuffle(mattemp)
mattemp = mattemp.reshape((lignes, colonnes))
reponse = "reponse"
while reponse != "exit":
    reponse = input("\nVoulez-vous la solution? (o/n) : ")
    if reponse == "o":
        print("\n" + str(mattemp))
        reponse = "exit"
    elif reponse == "n":
        print("\nBonne chanche!")
        reponse = "exit"
    else:
        print("Veuillez taper 'o' pour oui ou 'n' pour non svp.")

testvar = tempa = tempaa = tempaaa = tempb = tempbb = tempbbb = score = 0

def raz():
    global tempa, tempb, tempaa, tempbb, tempaaa, tempbbb, score
    if tempaaa != tempbbb:
        Button(fenetre,text=str(tempa) + ", " + str(tempaa), command=partial(retourner,tempa - 1, tempaa - 1), height = 1, width = 3).grid(row=tempa, column=tempaa)
        Button(fenetre,text=str(tempb) + ", " + str(tempbb), command=partial(retourner,tempb - 1, tempbb - 1), height = 1, width = 3).grid(row=tempb, column=tempbb)    
    else:
        score += 1
        Button(fenetre,text=" " + str(tempaaa) + " ", bg = "green", height = 1, width = 3).grid(row=tempa, column=tempaa)
        Button(fenetre,text=" " + str(tempbbb) + " ", bg = "green", height = 1, width = 3).grid(row=tempb, column=tempbb)
        txt1=Label(fenetre, text="Score : " + str(score) + " / " + str(int(lignes * colonnes / 2))).grid(row=1, column=len(mat[0])+1)
        if score == lignes * colonnes / 2:
            txt1=Label(fenetre, text="Gagné!").grid(row=3, column=len(mat[0])+1)
    return tempa, tempaa, tempaaa, tempb, tempbb, tempbbb, score

essais = 0
def retourner(y, z):
    global testvar, tempa, tempb, tempaa, tempbb, tempaaa, tempbbb, essais
    essais += 0.5
    testvar += 1
    txt1=Label(fenetre, text="Essais : " + str(essais)).grid(row=2, column=len(mat[0])+1)
    if testvar % 2 == 1:
        Button(fenetre,text=" " + str(mattemp[y, z]) + " ", bg = "orange", height = 1, width = 3).grid(row=y + 1, column=z + 1)
        tempa = y + 1
        tempaa = z + 1
        tempaaa = mattemp[y, z]
        return testvar, tempa, tempaa, tempaaa
    else:
        Button(fenetre,text=" " + str(mattemp[y, z]) + " ", bg = "orange", height = 1, width = 3).grid(row=y + 1, column=z + 1)
        tempb = y + 1
        tempbb = z + 1
        tempbbb = mattemp[y, z]
        if tempaaa != tempbbb:
            fenetre.after(1500, raz)
        else:
            raz()
        return testvar, tempa, tempaa, tempaaa, tempb, tempbb, tempbbb, essais

# Visuel
fenetre = Tk() 
fenetre.title("Trouve les paires")
b = 0
for i in range(lignes): #nombre de lignes
    b += 1
    c = 1
    for j in range(colonnes): #nombre de colonnes
        Button(fenetre,text=str(i + 1) + ", " + str(j + 1), command=partial(retourner,i ,j), height = 1, width = 3).grid(row=b, column=c)
        mat[i][j] = "(" + str(i + 1) + ", " + str(j + 1) +")"
        c += 1

txt1=Label(fenetre, text="Score : 0/" + str(int(lignes * colonnes / 2))).grid(row=1, column=len(mat[0])+1)

我希望程序阻止在“之后”暂停结束之前单击两个以上按钮的尝试。

请帮助...

0 个答案:

没有答案