python - 我正在努力让我的 tkinter 窗口在 while 循环中关闭

标签 python user-interface tkinter while-loop

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 关闭窗口。

最佳答案

除了再次将 global 添加到 exitt() 函数之外,您还需要更改通过 Exit 按钮调用它的方式 (按钮5)。

这是您的代码,其中包含 # 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()

关于python - 我正在努力让我的 tkinter 窗口在 while 循环中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202208/

相关文章:

Python FTP上传栏

java - 使用 GUI (Java) 启动线程

android - 从android中的另一个类访问ui

python - 更改 pandastable 中显示的数据框(tkinter 小部件)

python - 如何获取 tkinter 小部件的宽度?

python - 在 python 中组合列表中的第一个元素和同一列表的其他元素

python - 删除具有冲突标签的行

python - BeautifulSoup : get contents of search result tag

c - 用 C 语言设计一个简单的 GUI 框架

Python 全局文本区域