Tkinter程序创建无限循环

时间:2019-05-06 01:34:13

标签: python python-2.7 tkinter

我和一个朋友分配了一份编写爱情兼容性测试的代码。我试图让问题出现在屏幕上,并在文本框中输入答案,但是当我运行代码时,它在后台创建了一个无限循环,没有任何反应。

代码:

from Tkinter import *
import random

gender1 = "m"
gender2 = "f"
age1 = 0
age2 = 0
believe = "no"
name1 = ""
name2 = ""
height1 = 0
height2 = 0
weight1 = 0
weight2 = 0
sign1 = ""
sign2 = ""
total = 0
compat = [
        ["aries", "aquarius", "sagittarius", "leo", "gemini"],
        ["taurus", "virgo", "pisces"],
        ["gemini", "gemini", "aquarius", "libra"],
        ["cancer", "scorpio", "pisces"],
        ["leo", "sagittarius", "libra", "gemini", "aries"],
        ["virgo", "tarus", "capricorn"],
        ["libra", "libra", "leo", "sagittarius"],
        ["scorpio", "scorpio", "pisces"],
        ["sagittarius", "leo", "aries", "aquarius", "libra"],
        ["capricorn", "pisces", "scorpio", "virgo", "taurus"],
        ["aquarius", "gemini", "libra"],
        ["pisces", "scorpio", "cancer"]
    ]

def retrieve_input():
    value = e.get()
    return value

def rerun():
    line3 = "Run again?(y/n): "
    rerun = retrieve_input()
    rerun = rerun.lower()
    if rerun == "yes" or rerun == "y":
        run()
    else:
        close_escape()

def close_escape(event=None):
    root.destroy()

def pp():
    line1 = "    T.T"
    final = ("You are " + str(total) + "% compatable")
    line2 = final

def p():
    line1 = "I'm sorry but..."
    final = ("You are " + str(total) + "% compatable")
    line2 = final

def mp():
    line1 = "It probably won't work out"
    final = ("You are " + str(total) + "% compatable")
    line2 = final

def mf():
    line1 = "It's worth pursuing!"
    final = ("You are " + str(total) + "% compatable")
    line2 = final

def f():
    line1 = "Go get 'em tiger!"
    final = ("You are " + str(total) + "% compatable")
    line2 = final

def ff():
    line1 = "It's sure to work out!"
    final = ("You are " + str(total) + "% compatable")
    line2 = final

def invalid():
    print "Please enter a correct value"

def gender1():
    global gender1
    line4 = "Are you male or female?: "
    gender1 = retrieve_input()
    gender1 = gender1.lower()
    if gender1 == "male" or gender1 == "m":
        gender1 = "m"
    elif gender1 == "female"  or gender1 == "f":
        gender1 = "f"
    else:
        invalid()
        gender1()

def gender2():
    global gender2
    line5 = "Is person two male or female?: "
    gender2 = retrieve_input()
    gender2 = gender2.lower()
    if gender2 == "male" or gender2 == "m":
        gender2 = "m"
    elif gender2 == "female"  or gender2 == "f":
        gender2 = "f"
    else:
        invalid()
        gender2()

def age1():
    global age1
    line6 = "How old are you?: "
    age1 = retrieve_input()

def age2():
    global age2
    line7 = "How old is person two?: "
    age2 = retrieve_input()

def belief():
    global believe
    line1 = "Do you believe in astrological signs?(y/n): "
    believe = retrieve_input()
    believe = believe.lower()
    if believe == "yes" or believe == "y":
        believe = "yes"
    elif believe == "no" or believe == "n":
        believe = "no"
    else:
        invalid()
        belief()

def sign1():
    global sign1
    line5 = "What is your zodiac sign?: "
    sign1 = retrieve_input()
    sign1 = sign1.lower()

def sign2():
    global sign2
    line6 = "What is person two\'s zodiac sign?: "
    sign2 = retrieve_input()
    sign2 = sign2.lower()

def name1():
    global name1
    line2 = "What is your name?: "
    name1 = retrieve_input()
    name1 = name1.lower()

def name2():
    global name2
    line3 = "What is person two's name?: "
    name2 = retrieve_input()
    name2 = name2.lower()

def height1():
    global height1
    line1 = "Enter your height(feet then inches):"
    line2 = ""
    line3 = ""
    line4 = ""
    line5 = ""
    line6 = ""
    line7 = ""
    feet = retrieve_input()
    inches = retrieve_input()
    height1 = feet * 12 + inches

def height2():
    global height2
    line2 = "Enter person two\'s height(feet then inches):"
    feet = retrieve_input()
    inches = retrieve_input()
    height2 = feet * 12 + inches

def weight1():
    global weight1
    line3 = "How much do you weigh?: "
    weight1 = retrieve_input()
    weight1 = weight1.lower()

def weight2():
    global weight2
    line4 = "How much does person two weigh?: "
    weight2 = retrieve_input()
    weight2 = weight2.lower()

def bmi():
    global bmi1, bmi2
    bmi1 = int(weight1)*703/int(height1)**2
    bmi2 = int(weight2)*703/int(height2)**2

def results():
    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""
    line5 = ""
    line6 = ""
    line7 = ""
    if total <= 20:
        pp()
    elif total <= 40:
        p()
    elif total <= 60:
        mp()
    elif total <= 80:
        mf()
    elif total < 90:
        f()
    elif total >= 90:
        ff()

def calculate():
    global total
    total = random.randint(0,90)
    if bmi1 > 27 or bmi1 < 20:
        total -= 10
    else:
        total += 10
    if bmi2 > 27 or bmi2 < 20:
        total -=5
    else:
        total += 10
    if believe == "yes":
        for list in compat:
            if sign1 == list[0]:
                if sign2 in list[1:]:
                    total += 15
                else:
                    total -= 5
    else:
        total += 25
    if len(name1) >= len(name2):
        for letter in name2:
            if letter in name1:
                total += 5
    else:
        for letter in name1:
            if letter in name2:
                total += 5
    if len(name2) == range(len(name1) - 1, len(name1) + 1):
        total += 10
    elif len(name1) == range(len(name1) - 2, len(name1) + 2):
        total += 5
    if gender1 == "m":
        if gender2 == "m":
            total -= 10
        elif gender2 == "f":
            total += 10
    elif gender1 == "f":
        if gender2 == "m":
            total += 10
        elif gender2 == "f":
            total += 15
    if gender1 == "m":
        if gender2 == "f":
            if height2 > height1:
                total -= 10
            if weight2 > weight1:
                total -= 18
    if gender2 == "m":
        if gender1 == "f":
            if height1 > height2:
                total -= 10
            if weight1 > weight2:
                total -= 18
    age_dif = age1 - age2
    if 0 > age_dif:
        age_dif = age_dif * -1
    if age1 >= 18:
        if age2 >= 18:
            if age_dif in range(0, 4):
                total += 10
            elif age_dif in range(5, 8):
                total += 8
            elif age_dif in range(9, 11):
                total += 4
            elif age_dif in range(12, 15):
                total -= 8
            elif age_dif > 15:
                total -= 15
        else:
            total -= 30
    elif age1 < 18:
        if age2 < 18:
            if age_dif in range(0, 4):
                total += 10
            elif age_dif in range(5, 8):
                total += 8
            elif age_dif in range(9, 11):
                total += 4
            elif age_dif in range(12, 15):
                total -= 8
            elif age_dif > 15:
                total -= 15
        else:
            if age_dif > 1:
                total -= 30
    if total > 100:
        total = 100
    elif total < 0:
        total = 0
    else:
        total += 0

def run(event=None):
    print ""
    belief()
    name1()
    name2()
    gender1()
    gender2()
    age1()
    age2()
    height1()
    height2()
    weight1()
    weight2()
    bmi()
    if believe == "yes":
        sign1()
        sign2()
    calculate()
    results()
    rerun()

root = Tk()
root.title("Love compatibility test")
root.geometry("1280x1024")
filename = PhotoImage(file = "/home/pi/Desktop/rewrittenprogram/1.png")
background_label = Label(root, image= filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

label1 = Label(root, bg = "#ffadc8", text = "Love compatibility test", font = ("Times", 24, "bold italic"))
label1.pack()
label2 = Label(root, bg = "#ffadc8", text = " ", font = ("Times", 24, "bold italic"))
label2.pack()
main_canvas = Canvas(root, bg = "white", width = 750, height = 400)
main_canvas.pack()
label3 = Label(root, bg = "#ffadc8", text = " ", font = ("Times", 14, "bold italic"))
label3.pack()

line1 = ""
line2 = ""
line3 = ""
line4 = ""
line5 = ""
line6 = ""
line7 = ""
canvas1 = Label(root, bg = "white", text = line1, font = ("Helvetica", 14))
main_canvas.create_window(50, 50, window = canvas1)
canvas2 = Label(root, bg = "white", text = line2, font = ("Helvetica", 14))
main_canvas.create_window(50, 80, window = canvas2)
canvas3 = Label(root, bg = "white", text = line3, font = ("Helvetica", 14))
main_canvas.create_window(50, 110, window = canvas3)
canvas4 = Label(root, bg = "white", text = line4, font = ("Helvetica", 14))
main_canvas.create_window(50, 140, window = canvas4)
canvas5 = Label(root, bg = "white", text = line5, font = ("Helvetica", 14))
main_canvas.create_window(50, 170, window = canvas5)
canvas6 = Label(root, bg = "white", text = line6, font = ("Helvetica", 14))
main_canvas.create_window(50, 200, window = canvas6)
canvas7 = Label(root, bg = "white", text = line7, font = ("Helvetica", 14))
main_canvas.create_window(50, 230, window = canvas7)

main_canvas.create_line(10, 10, 740, 10, fill="black", width = 3)
main_canvas.create_line(10, 10, 10, 390, fill="black", width = 3)
main_canvas.create_line(10, 390, 740, 390, fill="black", width = 3)
main_canvas.create_line(740, 390, 740, 10, fill="black", width = 3)
main_canvas.create_line(0, 0, 750, 0, fill="black", width = 12)
main_canvas.create_line(0, 0, 0, 400, fill="black", width = 12)
main_canvas.create_line(0, 400, 750, 400, fill="black", width = 10)
main_canvas.create_line(750, 400, 750, 0, fill="black", width = 10)

e = Entry(root)
e.pack()
e.focus_set()

root.bind("<Return>", retrieve_input)
root.bind("<space>", run)
root.bind("<Escape>", close_escape)

root.mainloop()

应该发生的是应该调用每个函数,并用其问题更新画布上的标签。然后,您在文本框中输入一个答案,然后回答该问题,然后转到下一个。

实际上发生的是无效函数一遍又一遍地调用,冻结了tkinter屏幕

0 个答案:

没有答案