python - 我正在努力让我的 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 关闭窗口。
解决方案
除了添加global again
到exitt()
函数之外,您还需要更改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()
推荐阅读
- delphi - 如何在设计时将格式丰富的文本分配给 Delphi TRichEdit 或 TcxRichEdit(可能使用 Lines 属性)?
- firebase - 即使屏幕被锁定,我如何让 Flutter 应用程序在收到后台 firebase 消息时启动我希望我的应用程序启动
- react-native - 放置
标记下 - c# - 如何使用谷歌图像到文本 API 获取文本的 XY 坐标?
- mongodb - 获取带/不带锁定的整个 Mongo 集合?
- powershell - Powershell:Invoke-WebRequest:找不到接受参数{URI}的位置参数
- go - 如何向 Prometheus 查询所需的指标
- r - 从数据集中提取特定行
- javascript - 为带有子菜单的移动菜单添加关闭功能
- c - 在 C 中创建一个函数来检查二维数组中是否存在相邻的相等元素