【发布时间】: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