0

I'm trying to make a GUI in python using tkinter for a messaging. When I want to delete a specific person from the friend list, a new window will show up to ask me to confirm my decision. the problem is, after I select an answer, it's supposed to close that window, but it closes everything else too.

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Use `command=lambda: close_window(new)` and then `def close_window(window): window.quit()` – TheLizzard Jun 26 '21 at 17:46
  • You don't have to do `from tkinter import ttk` as the previous line i.e `from tkinter import *` imports everything from the tkinter module to the file including `ttk` – Tanish Sarmah Jun 26 '21 at 17:57
  • @TheLizzard it still closes both windows, not just the confirm window – Halil Simionca Jun 26 '21 at 18:05
  • 1
    Use `window.destroy()`, instead of `window.quit()`. – Delrius Euphoria Jun 26 '21 at 18:13
  • 1
    I want you to think about what you're doing, and not just monkey type an answer. "window" is the global variable that represents you main window. It should not be a surprise to you that when you call `window.quit()`, your whole app closes. That's why @TheLizzard's answer was correct. It passed the TARGET window as a parameter, so that you are closing the one window you want, not the main window. – Tim Roberts Jun 26 '21 at 18:16
  • @CoolCloud Do you want to write an answer? I have to go so I can't. – TheLizzard Jun 26 '21 at 18:19
  • @TheLizzard I'll see if someone else will write, else I will write in a while :D – Delrius Euphoria Jun 26 '21 at 18:20
  • @CoolCloud Yes, there were two problems in the original code. (1) global window, (2) quit instead of destroy. I want the poster to understand the global issue. – Tim Roberts Jun 26 '21 at 18:22
  • @TimRoberts Actually your comment made sense, I just read it wrong :/ – Delrius Euphoria Jun 26 '21 at 18:22
  • In my close.window(new) i had to type new.destroy(), problem solved – Halil Simionca Jun 26 '21 at 18:30

1 Answers1

2

Let's summarize the two immediate mistakes you made. You had your button handlers call close_window, which called window.quit(). window here is a global that refers to your main window, so closing that window kills the app. You need to close the calling dialog window. Second, quit on the main window quits the app. You need destroy to simply close the window.

But there are bigger issues we cannot solve. Your dialog functions do nothing. Both buttons simply close the dialog window, without saving the selection that the user made, and without returning any result. You still have a lot of code to write, and we haven't written it here.

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)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30