首页 > 解决方案 > 我正在努力让我的 tkinter 窗口在一段时间循环中关闭

问题描述

import tkinter
import random
scoresFile = open("Pythagorus quiz scores.txt","a")

again = True

def hypotenuse():
    adj = int(input("Enter the length of the adjacent (in cm): "))
    opp = int(input("Enter the length of the opposite (in cm): "))
    a = adj**21
    b = opp**2
    c = a+b
    hyp = c**0.5
    print("The hypotenuse is",hyp,"cm long")

def adjacent():
    hyp = int(input("Enter the length of the hypotenuse (in cm): "))
    opp = int(input("Enter the length of the opposite (in cm): "))
    c = hyp**2
    b = opp**2
    a = c -b
    adj = a**0.5
    print("The adjacent is",adj,"cm long")

def opposite():
    hyp = int(input("Enter the length of the hypotenuse (in cm): "))
    adj = int(input("Enter the length of the adjacent (in cm): "))
    c = hyp**2
    a = adj**2
    b = c -a
    opp = b**0.5
    print("The opposite is",opp,"cm long")

def pythagorus_test():
    print("Welcome to the pythagorus quiz")
    score = 0
    name= input("What is your name? ")
    for counter in range(10):
        num1 = random.randint(1,12)
        num2 = random.randint(1,12)
        answer = 1
        if answer == 1:
            print("The adj is",num1,"cm. The opp is",num2,"cm")
            answer = num1**2 + num2**2
            answer = answer **0.5
            userinput = int(input("Your answer:  "))
            answer = round(answer,1)
        if userinput == answer:
            print("Correct")
            score = score + 1
        else:
            print("Incorrect")
            print("The correct answer is",answer)
    print(name,"got "+str(score)+"/10")
    scoresFile.write("\t")
    scoresFile.write(name)

    with open('Pythagorus quiz scores.txt', 'w') as f:
      f.write('%d' % score)
    scoresFile.write("\n")

def exitt(root):
    again = False
    print("Goodbye!")
    root.quit()
    root.destroy()
    return again


while again == True:
    root = tkinter.Tk()
    canvas = tkinter.Canvas(root, height=600, width=1000)
    canvas = canvas.pack()
    frame = tkinter.Frame(root, bg="black")
    frame = frame.place(relwidth=1, relheight =1)
    button = tkinter.Button(root, text="Work out the length of the hypotenuse",bg ="white", fg ="red", command = hypotenuse)
    button = button.place(relx = 0.35, rely = 0.35, relwidth=0.3, relheight =0.1)
    button2 = tkinter.Button(root, text="Work out the length of the adjacent",bg ="white", fg ="red", command = adjacent)
    button2 = button2.place(relx = 0.35, rely = 0.5, relwidth=0.3, relheight =0.1)
    button3 = tkinter.Button(root, text="Work out the length of the opposite",bg ="white", fg ="red", command = opposite)
    button3 = button3.place(relx = 0.35, rely = 0.65, relwidth=0.3, relheight =0.1)
    button4 = tkinter.Button(root, text="Pythagorus tester",bg ="white", fg ="red", command = pythagorus_test)
    button4 = button4.place(relx = 0.4, rely = 0.2, relwidth=0.2, relheight =0.1)
    button5 = tkinter.Button(root, text="Exit",bg ="white", fg ="red", command = exitt)
    button5 = button5.place(relx = 0.4, rely = 0.1, relwidth=0.2, relheight =0.1)
    root.mainloop()

我遇到的问题(您可能会发现)是:我无法使用退出按钮或角落中的传统 X 关闭窗口。

标签: pythonuser-interfacetkinterwhile-loop

解决方案


除了添加global againexitt()函数之外,您还需要更改Exit按钮 ( button5) 调用它的方式。

这是您的代码,其中的更改由# ALL CAPS COMMENTS.

import tkinter
import random
scoresFile = open("Pythagorus quiz scores.txt","a")

again = True

def hypotenuse():
    adj = int(input("Enter the length of the adjacent (in cm): "))
    opp = int(input("Enter the length of the opposite (in cm): "))
    a = adj**21
    b = opp**2
    c = a+b
    hyp = c**0.5
    print("The hypotenuse is",hyp,"cm long")

def adjacent():
    hyp = int(input("Enter the length of the hypotenuse (in cm): "))
    opp = int(input("Enter the length of the opposite (in cm): "))
    c = hyp**2
    b = opp**2
    a = c -b
    adj = a**0.5
    print("The adjacent is",adj,"cm long")

def opposite():
    hyp = int(input("Enter the length of the hypotenuse (in cm): "))
    adj = int(input("Enter the length of the adjacent (in cm): "))
    c = hyp**2
    a = adj**2
    b = c -a
    opp = b**0.5
    print("The opposite is",opp,"cm long")

def pythagorus_test():
    print("Welcome to the pythagorus quiz")
    score = 0
    name= input("What is your name? ")
    for counter in range(10):
        num1 = random.randint(1,12)
        num2 = random.randint(1,12)
        answer = 1
        if answer == 1:
            print("The adj is",num1,"cm. The opp is",num2,"cm")
            answer = num1**2 + num2**2
            answer = answer **0.5
            userinput = int(input("Your answer:  "))
            answer = round(answer,1)
        if userinput == answer:
            print("Correct")
            score = score + 1
        else:
            print("Incorrect")
            print("The correct answer is",answer)
    print(name,"got "+str(score)+"/10")
    scoresFile.write("\t")
    scoresFile.write(name)

    with open('Pythagorus quiz scores.txt', 'w') as f:
      f.write('%d' % score)
    scoresFile.write("\n")

def exitt(root):
    global again  # ADDED
    again = False
    print("Goodbye!")
    root.quit()
    root.destroy()
#    return again  # NOT NEEDED


while again == True:
    root = tkinter.Tk()
    canvas = tkinter.Canvas(root, height=600, width=1000)
    canvas = canvas.pack()
    frame = tkinter.Frame(root, bg="black")
    frame = frame.place(relwidth=1, relheight=1)
    button = tkinter.Button(root, text="Work out the length of the hypotenuse",bg="white", fg="red", command=hypotenuse)
    button = button.place(relx = 0.35, rely = 0.35, relwidth=0.3, relheight =0.1)
    button2 = tkinter.Button(root, text="Work out the length of the adjacent",bg ="white", fg="red", command=adjacent)
    button2 = button2.place(relx = 0.35, rely = 0.5, relwidth=0.3, relheight =0.1)
    button3 = tkinter.Button(root, text="Work out the length of the opposite",bg ="white", fg="red", command=opposite)
    button3 = button3.place(relx = 0.35, rely = 0.65, relwidth=0.3, relheight =0.1)
    button4 = tkinter.Button(root, text="Pythagorus tester",bg ="white", fg ="red", command=pythagorus_test)
    button4 = button4.place(relx = 0.4, rely = 0.2, relwidth=0.2, relheight =0.1)
    button5 = tkinter.Button(root, text="Exit",bg ="white", fg ="red", command=lambda r=root: exitt(r))  # CHANGED.
    button5 = button5.place(relx = 0.4, rely = 0.1, relwidth=0.2, relheight=0.1)
    root.mainloop()

推荐阅读