简体   繁体   English

Python在破坏Tkinter窗口时崩溃

[英]Python crashes upon destroying Tkinter window

I'm a student in software development and I'm developing an application (my first application!) in Python for learning purposes. 我是软件开发的学生,并且正在出于学习目的使用Python开发一个应用程序(我的第一个应用程序!)。 It works fairly well, and I got almost all of the features working that I want to work. 它运行良好,并且我可以使用几乎所有功能。

A short explanation of the program: It's an administrative program to log the members of a sports club. 该程序的简要说明:这是一个管理运动俱乐部会员的管理程序。 So it has basic functions like adding members, listing members, etc. 因此它具有基本功能,例如添加成员,列出成员等。

For it to be complete, it also needs a function to delete a member. 为了使其完整,还需要删除成员的功能。 This has always worked fine, but I rarely used the function. 这一直很好,但是我很少使用该功能。

Now after I modified a ttk.Treeview() object so it now sorts by last name, I added a new member to see if it sorts like it should. 现在,在我修改了ttk.Treeview()对象以使其按姓氏排序之后,我添加了一个新成员以查看其排序是否应该。 It did! 它做了!

Now I wanted to delete that member, but for some reason, it no longer works. 现在我想删除该成员,但是由于某种原因,它不再起作用。

It crashes Python. 它使Python崩溃。 It doesn't just crash the application, no error, just plain "Pythonw.exe has stopped working". 它不仅崩溃了应用程序,没有错误,只是普通的“ Pythonw.exe已停止工作”。

Here's all the code that belongs to that function: 这是该函数的所有代码:

def dellid(zeid, addwindow):
    winsound.PlaySound("SystemHand", winsound.SND_ASYNC)
    usure = tkinter.Tk()
    print('usure window created')
    usure.title('Lid verwijderen')
    usure.geometry('{}x{}'.format('300', '150'))
    usure.iconbitmap(default='programdata\\icon.ico')
    usure.attributes("-topmost", True)
    addwindow.grab_set()
    #logo2 = PhotoImage(file="warning.png")
    #logolabel = tkinter.Label(usure, image = logo2)
    #logolabel.grid(row01, columnspan = 2)

    usure.columnconfigure(0, weight = 1)
    usure.columnconfigure(1, weight = 2)

    label01 = tkinter.Label(usure, text = '''
Weet U zeker dat U dit lid wilt verwijderen?
Deze actie kan niet ongedaan worden gemaakt!''').grid(row = 1, columnspan = 2)
    emptyrow = tkinter.Label(usure, text = ' ').grid(row = 2, columnspan = 2)
    jaknop = ttk.Button(usure, text = 'Ja', command = lambda: delforsure(zeid, usure, addwindow)).grid(row = 3, column = 0, sticky = 'E')
    neeknop = ttk.Button(usure, text = 'Nee', command = lambda: nodell(addwindow, usure)).grid(row = 3, column = 1, sticky = 'S') 





def nodell(addwindow, usure):
    addwindow.grab_release()
    usure.destroy()



def delforsure(zeid, usure, addwindow):
    #addwindow.grab_release()
    addwindow.destroy()
    print('addwindow destroyed')
    usure.destroy()
    print('usure destroyed')
    conn = sqlite3.connect('test.db')
    c = conn.cursor()

    opendb = []
    opendb.append(zeid)

    print(zeid)
    print(opendb)

    c.execute("DELETE FROM leden WHERE ids = ?",opendb)
    print('c.execute')
    conn.commit()
    print('c.commit')
    #usure.destroy()

    done = tkinter.Tk()
    done.title('Lid verwijdert')
    done.geometry('{}x{}'.format('300', '150'))
    done.iconbitmap(default='programdata\\icon.ico')

    label01 = tkinter.Label(done, text = '''


Lid verwijdert

''')
    label01.pack()

    done.update()
    time.sleep(2)

    on_return(done)

The on_return function closes the said window and reopens the main menu. on_return函数关闭所述窗口并重新打开主菜单。

Now in delforsure(zeid, usure, addwindow) it locks up on the line usure.destroy() 现在在delforsure(zeid, usure, addwindow)它锁定在usure.destroy()

It doesn't print the line 'usure destroyed' anymore, that I put there to see where it locks up. 它不再打印“可用销毁”行,而是放在那里查看其锁定位置。

It doesn't give any error, Python itself just crashes. 它没有给出任何错误,Python本身只是崩溃了。

If I move 如果我移动

usure.destroy()
print('usure destroyed')

under on_return(done) , all the way at the bottom, it does close the window and return to the main menu, but the main menu will be drawn with graphical distortions and soon still crashes afterwards. on_return(done) ,一直到底部,它确实关闭了窗口并返回到主菜单,但是将以图形变形绘制主菜单,此后不久仍然崩溃。

I really don't understand what is going on. 我真的不明白发生了什么。

Can anybody help me trace the issue please? 有人可以帮我追踪这个问题吗?

It looks like you are making a yes / no modal window. 看来您正在制作是/否模态窗口。 Is there a reason you don't want to use the version that is built into tkinter? 您是否有不想使用tkinter内置版本的原因?

from tkinter import messagebox

def dellid(zeid, addwindow):
    answer = messagebox.askyesno(
        'Lid verwijderen',
        'Weet U zeker dat U dit lid wilt verwijderen?\n'
        'Deze actie kan niet ongedaan worden gemaakt!')
    if answer:
        addwindow.destroy()
        print('addwindow destroyed')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM