【问题标题】:Can't properly close a GUI window in tkinter [duplicate]无法在 tkinter 中正确关闭 GUI 窗口 [重复]
【发布时间】:2021-06-26 17:44:32
【问题描述】:

我正在尝试使用tkinter 在python 中创建一个GUI 用于消息传递。当我想从朋友列表中删除特定的人时,会出现一个新窗口,要求我确认我的决定。问题是,在我选择答案后,它应该关闭那个窗口,但它也会关闭其他所有内容。

from tkinter import *
from tkinter import ttk

def open_win():
    new= Tk()
    new.geometry("750x250")
    new.title("New Window")
   #Create a Label in New window
    Label(new, text="No friends to be seen, moving on", font=('Helvetica 17 bold')).pack(pady=30)

def close_window():
    window.quit()
   # exit()

def open_win_delete():
    new= Tk()
    new.geometry("750x250")
    new.title("New Window")
   #Create a Label in New window
    Label(new, text="Are you sure you want to delete this friend?", font=('Helvetica 17 bold')).grid(row=0,column=2,sticky=NE)
    Button(new, text="Yes",width=6, font="none 12 bold", command=close_window) .grid(row=1,column=2,sticky=NE)    
    Button(new, text="No",width=6, font="none 12 bold", command=close_window) .grid(row=1,column=3,sticky=NE)


def open_win_block():
    new= Tk()
    new.geometry("750x250")
    new.title("New Window")
   #Create a Label in New window
    Label(new, text="Are you sure you want to block this friend?", font=('Helvetica 17 bold')).grid(row=0,column=2,sticky=NE)
    Button(new, text="Yes",width=6, font="none 12 bold", command=close_window) .grid(row=1,column=2,sticky=NE)
    Button(new, text="No",width=6, font="none 12 bold", command=close_window) .grid(row=1,column=3,sticky=NE)


def boxchat():
    window = Tk()

    window.title("Messenger")

    window.configure(background="RoyalBlue4")
    window.geometry("410x500")

    Label(window,text="User:",bg="RoyalBlue4",fg="white",font="none 12 bold") .grid(row=0, column=0,sticky=SW)

    output= Text(window, width=50, height=20,wrap=WORD,background="white")
    output.grid(row=1,column=0,columnspan=3, sticky=N)

    #photo = PhotoImage(file = "D:/SS/Screenshot_1999.png")

    #photoimage = photo.subsample(6, 6)
    Button(window, text="Send",width=4, font="none 12 bold") .grid(row=6,column=1,sticky=E)

    Label(window, text="Input text: ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=4,column=0,sticky=W)
    Button(window, text="Friends",width=7, font="none 12 bold", command=open_win) .grid(row=0,column=1,sticky=NE) 

    t = Text(window, height=4, width=39)
    t.grid(row=5,column=1,sticky=SW)

    window.mainloop()





window = Tk()
window.title("Friend List")
window.configure(background="RoyalBlue4")
window.geometry("410x250")

#1user
Label(window, text="Bide ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=0,column=0,sticky=W)
Label(window, text="      ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=0,column=1,sticky=W)
Button(window, text="Chat",width=4, font="none 12 bold", command=boxchat) .grid(row=0,column=2,sticky=NE)
Button(window, text="Delete",width=6, font="none 12 bold", command=open_win_delete) .grid(row=0,column=3,sticky=NE)
Button(window, text="Block",width=5, font="none 12 bold", command=open_win_block) .grid(row=0,column=4,sticky=NE)

#2user

Label(window, text="Robi ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=1,column=0,sticky=W)
Label(window, text="      ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=1,column=1,sticky=W)
Button(window, text="Chat",width=4, font="none 12 bold", command=boxchat) .grid(row=1,column=2,sticky=NE)
Button(window, text="Delete",width=6, font="none 12 bold", command=open_win_delete) .grid(row=1,column=3,sticky=NE)
Button(window, text="Block",width=5, font="none 12 bold", command=open_win_block) .grid(row=1,column=4,sticky=NE)

#3user

Label(window, text="Ibo ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=2,column=0,sticky=W)
Label(window, text="      ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=2,column=1,sticky=W)
Button(window, text="Chat",width=4, font="none 12 bold", command=boxchat) .grid(row=2,column=2,sticky=NE)
Button(window, text="Delete",width=6, font="none 12 bold", command=open_win_delete) .grid(row=2,column=3,sticky=NE)
Button(window, text="Block",width=5, font="none 12 bold", command=open_win_block) .grid(row=2,column=4,sticky=NE)
Label(window, text="                         ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=5,column=5,sticky=W)
Label(window, text="                         ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=6,column=5,sticky=W)
Label(window, text="                         ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=7,column=5,sticky=W)
Label(window, text="                         ", bg="RoyalBlue4", fg="white",font="none 12 bold") .grid(row=8,column=5,sticky=W)
Button(window, text="Exit",width=4, font="none 12 bold", command=close_window) .grid(row=9,column=6,sticky=SE)

window.mainloop()

【问题讨论】:

  • 使用command=lambda: close_window(new),然后使用def close_window(window): window.quit()
  • 您不必像上一行那样执行from tkinter import ttk,即from tkinter import * 将所有内容从tkinter 模块导入到文件中,包括ttk
  • @TheLizzard 它仍然关闭两个窗口,而不仅仅是确认窗口
  • 使用window.destroy(),而不是window.quit()
  • 我希望你想想你在做什么,而不仅仅是猴子输入一个答案。 “window”是代表主窗口的全局变量。当您拨打window.quit() 时,您的整个应用程序都会关闭,这对您来说应该不足为奇。这就是为什么@TheLizzard 的回答是正确的。它将 TARGET 窗口作为参数传递,因此您正在关闭您想要的一个窗口,而不是主窗口。

标签: python user-interface tkinter


【解决方案1】:

让我们总结一下您犯的两个直接错误。你的按钮处理程序调用close_window,它调用window.quit()window 这里是一个全局变量,它指向您的主窗口,因此关闭该窗口会杀死应用程序。您需要关闭调用对话窗口。其次,主窗口上的quit 退出应用程序。您需要destroy 才能简单地关闭窗口。

但是我们无法解决更大的问题。你的对话函数什么都不做。这两个按钮只是关闭对话框窗口,而不保存用户所做的选择,也不返回任何结果。你还有很多代码要写,我们这里没有写。

def close_window(dlg):
    dlg.destroy()

def open_win_delete():
    new= Tk()
    new.geometry("750x250")
    new.title("New Window")
   #Create a Label in New window
    Label(new, text="Are you sure you want to delete this friend?", font=('Helvetica 17 bold')).grid(row=0,column=2,sticky=NE)
    Button(new, text="Yes",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=2,sticky=NE)    
    Button(new, text="No",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=3,sticky=NE)


def open_win_block():
    new= Tk()
    new.geometry("750x250")
    new.title("New Window")
   #Create a Label in New window
    Label(new, text="Are you sure you want to block this friend?", font=('Helvetica 17 bold')).grid(row=0,column=2,sticky=NE)
    Button(new, text="Yes",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=2,sticky=NE)
    Button(new, text="No",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=3,sticky=NE)

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2019-03-27
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多