首页 > 解决方案 > 用另一个函数替换一个函数

问题描述

我正在尝试用下面的 compare() 函数替换 Tkinter 块中的 test() 函数。我已经尝试了很长时间,但我做不到。总结上下文我有我的程序(我的比较函数),我复制了某人的 Tkinter 部分。这个已经有比较功能,我只是想插入我的基本比较功能而不是 Tkinter 部分中的比较功能,但我做不到。提前致谢

from tkinter import *

root = Tk()
root.geometry('372x362')
root.configure(bg="pink")
Label(root,text='Homme',bg="pink",fg="black").grid(row=0,column=1)
Label(root,text='Femme',bg="pink",fg="black").grid(row=0,column=3)
Male = Entry(root)
Male.grid(row=1,column=1)
Label(root,text='&',bg="pink",fg="black").grid(row=1,column=2)
Female = Entry(root)
Female.grid(row=1,column=3)

class Candidat: #Subject class (characteristics)
    def __init__(self,genre,humour,physique,intelligence,projet):
        self.genre=genre
        self.humour=humour
        self.physique=physique
        self.intelligence=intelligence
        self.projet=projet

def test():

    MaleEntry = Male.get()
    FemaleEntry = Female.get()
    if MaleEntry == FemaleEntry:
        Label(root,text='0% compatibility',bg="red",fg="white").grid(row=3,column=2)

    else:
        Label(root,text='100% compatibility',bg="green",fg="white").grid(row=3,column=2)

Button(root,text='Test de Compatibilité',command=test,bg="purple",fg="white",bd=4).grid(row=2,column=2)

root.mainloop()


#-----------------------------------------------------------------------------------------------------#
    #Second part (I know that several conditions are missing but it is to keep the code as small as possible.)

def compare(Test1,Test2):

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Match Parfait 100%',bg="green",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 0%',bg="red",fg="white").grid(row=4,column=2)
#Match à 75% :

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 75%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 1',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 75%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 2',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 75%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 3',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 75%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 4',bg="red",fg="white").grid(row=4,column=2)

#Match à 50% :

    if Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 50%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 01',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 50%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 02',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 50%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 03',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 50%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 04',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 50%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 05',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 50%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 06',bg="red",fg="white").grid(row=4,column=2)

#Match à 25%

    if Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        Label(root,text='Mauvais Match 25%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 001',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 25%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 002',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 25%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 003',bg="red",fg="white").grid(row=4,column=2)

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 25%',bg="red",fg="white").grid(row=4,column=2)
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        Label(root,text='Mauvais Match 004',bg="red",fg="white").grid(row=4,column=2)
#characteristics
hommes = {}
femmes = {}

Bryan=Candidat("homme",4,5,3,2)
Adrien=Candidat("homme",4,3,2,3)
Marin=Candidat("homme",5,2,4,3)
Alcaraz=Candidat("homme",4,3,2,1)
Allan=Candidat("homme",4,3,2,1)
Seby =Candidat("homme",4,3,2,1)
hommes['Bryan'] = Bryan
hommes['Adrien'] = Adrien
hommes['Marin'] = Marin
hommes['Alcaraz'] = Alcaraz
hommes['Allan'] = Allan
hommes['Seby'] = Seby
#--------------------------------#
Anissa=Candidat("femme",0,5,2,0)
Melanie=Candidat("femme",5,3,4,3)
Dita =Candidat("femme",4,4,3,2)
LeaMary=Candidat("femme",4,3,2,1)
Maisanne=Candidat("femme",4,3,2,1)
Kellyn=Candidat("femme",4,3,2,1)
femmes['Anissa'] = Anissa
femmes['Melanie'] = Melanie
femmes['Dita'] = Dita
femmes['LeaMary'] = LeaMary
femmes['Maisanne'] = Maisanne
femmes['Kellyn'] = Kellyn


#I don't know if these 3 lines are still useful
'''
#I don't know if these 3 lines are still useful
homme = input("Choisir le prénom d'un homme : ")#input Man
femme = input("Choisir le prénom d'une femme : ")#input Woman
compare(hommes[homme],femmes[femme])'''

标签: pythonuser-interfacetkinterreplaceinverse

解决方案


推荐阅读