然而,当我确定错误部分时,该程序仍然可以正常工作,而错误部分只应出现在circles.dat
(其中包含int列表)的情况下,但是,失败的第二个窗口(故意用于测试错误)对象)对象仍然显示为一个小窗口。
from tkinter import *
class Error():
def __init__(self):
self.root = Tk()#Tk
self.root.title("ERROR-PAGE")#name on bar
self.root.resizable(0,0)#doesn't let it be resized
self.gamefield = Canvas(self.root, width=320, height=480, bg="black") #canvas
self.top = Frame(self.root)
self.gamefield.create_text(20, 60, anchor=W, font=("Comic Sans MS", 25),text="We have",fill="white")
self.gamefield.create_text(20, 100, anchor=W, font=("Comic Sans MS", 25),text="had",fill="white")
self.gamefield.create_text(20, 140, anchor=W, font=("Comic Sans MS", 25),text="a",fill="white")
self.gamefield.create_text(55, 140, anchor=W, font=("Comic Sans MS", 25,"bold"),text="BAD TIME",fill="cyan")
self.gamefield.create_text(20, 180, anchor=W, font=("Comic Sans MS", 25),text="finding 'circles.dat'",fill="white")
self.gamefield.create_text(20, 220, anchor=W, font=("Comic Sans MS", 25),text="Please place the",fill="white")
self.gamefield.create_text(20, 260, anchor=W, font=("Comic Sans MS", 25),text="file in the",fill="white")
self.gamefield.create_text(20, 300, anchor=W, font=("Comic Sans MS", 25),text="same folder as",fill="white")
self.gamefield.create_text(20, 340, anchor=W, font=("Comic Sans MS", 25),text="the file",fill="white")
self.gamefield.create_text(20, 390, anchor=W, font=("Comic Sans MS", 25),text="'my_circle.py'",fill="white")
self.gamefield.pack(side="top")
self.root.mainloop()
class Mycircle:
def __init__(self):
self.root = Tk()#Tk
self.root.title("My Circle")#name on bar
self.root.resizable(0,0)#doesn't let it be resized
self.gamefield = Canvas(self.root, width=640, height=480, bg="white") #canvas
self.top = Frame(self.root)
self.count=0#count
try:
with open("circles.dat", "r") as f:
self.text = [int(a) for l in f for a in l.split()]
except:
Error()
while self.count < len(self.text):
try:
self.gamefield.create_oval((self.text[0+self.count]),(self.text[1+self.count]),(self.text[2+self.count]),(self.text[3+self.count]), outline="gray",fill="red", width=2)
self.count+=4
self.gamefield.pack(fill=BOTH, expand=1)
except:
print("there wasn't enough int to finish another circle")
self.count+=4
self.gamefield.pack(side="top")
self.root.mainloop()#loop
def Main():
Mycircle()
Main()