Tkinter destroy方法崩溃了程序

时间:2016-06-01 11:16:55

标签: python tkinter

我正在尝试使用tkinter创建一种GUI,这是代码。但是,当我运行它时,单击设置,然后返回,python崩溃。你知道为什么会发生这种情况以及如何解决这个问题吗?它发生在我运行destroy函数时,或者当我在另一个Tk窗口中执行某些操作时,

代码:(有些可能没有必要,但我会包含它)

import tkinter as tk

class moveFrame():
    def move(self, event):
        widget = event.widget
        widget.place(x = widget.winfo_x() + event.x - widget.startX, y = widget.winfo_y() + event.y - widget.startY)

def resize(self, event):
    widget = event.widget
    wlength = widget["length"]
    wwidth = widget["width"]
    widget.config(length = wlength + event.x - widget.startX, width = wwidth + event.y - widget.startY)

def getPos(self, event):
    widget = event.widget
    widget.lift()
    widget.startX, widget.startY = event.x, event.y

def __init__(self, master, frameInfo, xPos, yPos):
    self.frame = tk.Frame(master, cnf = frameInfo)
    self.frame.bind("<Button-1>", self.getPos)
    self.frame.bind("<B1-Motion>", self.move)
    self.frame.bind("<B3-Motion>", self.getPos)
    self.frame.bind("<Button-3>", self.resize)
    self.frame.place(x = xPos, y = yPos)


def homescreen():
    screen = tk.Tk()
    #arrangeButton = tk.Canvas(screen, width = 120, height = 40, bg = "purple")
    #arrangeButton.create_text(60, 20, text = "Arrange Homescreen")
    #arrangeButton.place(x = 0, y = 0)
    #arrangeButton.bind("<Button-1>", lambda event: arrange(arrangeButton))
    settingsButton = tk.Canvas(screen, width = 60, height = 60)
    settingsButton.place(x = 20, y = 20)
    settingsButton.create_oval(5, 5, 58, 58, fill = "#a6a6a6", tags = "click")
    settingsButton.create_oval(15, 10, 58, 53, fill = "#000", tags = "click")
    settingsButton.create_oval(27, 22, 46, 41, fill = "#00f", tags = "click")
    settingsButton.tag_bind("click", "<Button-1>", lambda event: settings())

def settings():
    sscreen = tk.Tk()
    #Get previous settings
    try:
        file = open("settings.txt", "r")
    except IOError:
        file = open("settings.txt", "x")
    finally:
        file = open("settings.txt", "r+")
    curSet = [x.strip("\n") for x in file.readlines()]
    #Widgets
    back = tk.Canvas(sscreen, width = 60, height = 48)
    back.place(x = 20, y = 20)
    back.create_rectangle(25, 22, 60, 26, fill = "#000", tags = "sclick")
    back.create_polygon(10, 24, 25, 10, 25, 38, fill = "#000", tags = "sclick")
    back.create_text(42, 32, text = "Back", tags = "sclick")
    back.tag_bind("sclick", "<Button-1>", lambda event: sscreen.destroy())

def arrange(*widgets):
    arrangeScreen = tk.Tk()
    arrangeButton = widgets[0]
    aBaF = moveFrame(arrangeScreen, {"bd" : 4, "bg" : "#a6a6a6"}, arrangeButton.winfo_x() - 4, arrangeButton.winfo_y() - 4)
    aBa = tk.Canvas(aBaF, width = 120, height = 40, bg = "purple")


def load(goTo, ms):
    load = tk.Tk()
    loadImage = tk.Canvas(load, height = 300, width = 300, bg = "black")
    loadImage.pack()
    loadImage.create_oval(125, 130, 175, 180, fill = "white")
    loadImage.create_oval(130, 140, 170, 150, fill = "black")
    loadImage.create_oval(130, 140, 171, 151, fill = "white")
    loadImage.create_oval(152, 140, 148, 150, fill = "black")
    load.after(ms, lambda: [goTo(), load.destroy()])

load(homescreen, 1)

1 个答案:

答案 0 :(得分:1)

如果您的程序代表一个应用程序,则只生成一个Tk实例。

Tk对象表示整个应用程序的根目录,通过删除它并创建另一个应用程序,您实际上是在创建一个单独的应用程序,并且对先前应用程序的任何引用都会导致未定义的行为,例如崩溃python。

不使用Tk作为单独的窗口,而是使用Toplevel,因为这是它们的目的,单独的窗口。

tk.Tk()中所有出现的tk.Toplevel(abs_root)替换为abs_root,然后将tk.Tk()定义为abs_root = tk.Tk() abs_root.withdraw() #hides the window while your program runs. load(homescreen, 1) abs_root.mainloop() ,如下所示:

abs_root

会使你的程序工作,但是它确实意味着它会一直运行直到abs_root.destroy()被破坏,这不能通过点击关闭按钮发生,因为它没有显示为窗口,你要么必须使用某些条件您明确调用abs_root或选择一个将成为第一个创建的窗口并最后关闭的窗口,并将其用作cursor.getInt(i);。 (不能用你的程序编写)