How do I display a message before closing the tkinter window? - python-3.x

A message is required to be shown just before closing the tkinter window on the click of a close button added to the window.
lab = Label(window,text = 'thank you')
lab.grid()
window.destroy()
I used the above code to do so, but the window gets closed before the message is being displayed
Can I have the solution for this?

You can either use this:
from tkinter import Tk
import tkinter.messagebox as msgbox
def display_msg():
msgbox.showinfo(title='', message='Thank You')
root.destroy()
root = Tk()
root.protocol('WM_DELETE_WINDOW', display_msg)
root.mainloop()
which will show You a messagebox before closing,
or if You want to display a widget use this:
from tkinter import Tk, Label
def display_msg():
msg = Label(root, text='Thank You!')
msg.pack()
root.after(3000, root.quit)
root = Tk()
root.protocol('WM_DELETE_WINDOW', display_msg)
root.mainloop()
When You protocol root with 'WM_DELETE_WINDOM' You can make it execute a function when You try to close the window.
In the first example it will just show an infobox.
In the second example it will wait for 3 seconds (3000 miliseconds) and then destroy the root

Related

multiple commands for a tkinter button

I have been trying to execute 2 commands in 1 button. I have read that using lambda can solve the problem. But my situation is a little different. On button press, one command is to destroy the existing GUI, and the second command, I want is to open another SERVER GUI. Below is my existing button functionality.
exit_button = Button(topFrame, text='Quit', command=window.destroy)
How can I open another GUI using same button?
Thank you for any help.
Edit:-
I have now created the below function:
def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()
GUI_Interface is a function that I need to call after closing the existing .py file. If I put GUI_Interface as the first command for close_func(), then it really does not go back to the 2nd step and never closes the existing.py file.
And if I place GUI_Interface in the end, it just closes the existing one and nevr opens the function of the new .py file
Three options at least:
using or (with lambda if arguments):
from tkinter import Tk, Button
root = Tk()
Button(root, text='Press', command=lambda: print('hello') or root.destroy() or print('hi')).pack()
root.mainloop()
important to use or because and didn't work (don't know why or how this works at all)
or use function definitions:
from tkinter import Tk, Button
def func():
print('hello')
root.destroy()
print('hi')
root = Tk()
Button(root, text='Press', command=func).pack()
root.mainloop()
or lists with lambda:
from tkinter import Tk, Button
def func():
print('hello')
root.destroy()
print('hi')
root = Tk()
Button(root, text='Press', command=lambda: [print('hello'), root.destroy()]).pack()
root.mainloop()

why does tkinter widgets doesnt appear in vscode?

from tkinter import *
from tkinter import ttk
root = Tk()
root.mainloop()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
And when i try to run the program only the window show up but without the button.
it gives me this error:
Exception has occurred: TclError
NULL main window
File "C:\Users\Elad\Desktop\coding\tkintertut.py", line 6, in <module>
button1 = ttk.Label(root, text = 'lol')
The root.mainloop() has to be at the end of the code for the window to be shown, not right after declaration of root.
from tkinter import *
from tkinter import ttk
root = Tk()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
root.mainloop()
Explanation:
Only the lines of code between root and root.mainloop() gets executed as long as the window is open, if you close the window, the Labels and all other widget declared after root.mainloop() become active but, the window is now closed and root is destroyed, hence the error. But here in my answer, the code between root and root.mainloop() has everything I need to be in the window and hence it shows the complete window. Just keep in mind to always say root.mainloop() at the end of the code only
Hope it cleared your errors. Do let me know if any more errors or doubts.
Cheers
Your root.mainloop() should be the last code!
from tkinter import *
from tkinter import ttk
root = Tk()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
root.mainloop()

hiding tkinter window using withdraw vs wm_withdraw

What is the difference between withdraw and wm_withdraw?
import time
import tkinter as tk
def hide():
root.withdraw()
time.sleep(2)
root.deiconify()
root = tk.Tk()
tk.Button(root, text = 'hide', command = hide).pack()
root.mainloop()
When the 'hide' button is clicked, the window is hidden. It disappears from the panel (taskbar), and is not visible in the task view (simultaneous view of all open windows) for 2 seconds.
import time
import tkinter as tk
def hide():
root.wm_withdraw()
time.sleep(2)
root.deiconify()
root = tk.Tk()
tk.Button(root, text = 'hide', command = hide).pack()
root.mainloop()
Same code, but wm_withdraw instead of withdraw. Again, clicking the 'hide' button makes the both the taskbar entry and the window itself invisible for 2 seconds.
Is there any difference at all between these two? Which one should I use? Further, should I use deiconify or wm_deiconify? All four combinations (withdraw, deiconify; wm_withdraw, deiconify; withdraw, wm_deiconify; wm_withdraw, wm_deiconify) seem to do the exact same thing. Is there any application where they will do different things?
There's no difference between them - they both (withdraw and deiconify) just shortucts for wm_ counterparts.
The same applies to all functions, that interact with Window manager under Wm class.
There is no difference between withdraw and wm_withdraw. I can not specify why this was done, but here is the source of tkinter in which we have line withdraw = wm_withdraw (which makes it clear that both calls end up at the same method):
def wm_withdraw(self):
"""Withdraw this widget from the screen such that it is unmapped
and forgotten by the window manager. Re-draw it with wm_deiconify."""
return self.tk.call('wm', 'withdraw', self._w)
withdraw = wm_withdraw

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...

Closing my message box and root window in python 3.6

import statements
import from tkinter import *
import tkinter.simpledialog
import tkinter.messagebox
from tkinter import ttk
from time import strftime
now = strftime( "%x %Z %X")
Setting window
root=Tk()
root.title("ON TOP OF THE WORLD")
canvas= Canvas(root, width=350, height=250)
canvas.pack()
photo=PhotoImage(file='/Users/m/Desktop/TOTW.gif')
canvas.create_image(0, 0, anchor = NW, image=photo)
Welcome User
tkinter.messagebox.showinfo("Hello,today is", now)
ask user
name=tkinter.simpledialog.askstring("name","What is your name?" )
input process
output = "Hello, %s! May the only place you find yourself today is on top of the world !" %name
show output
tkinter.messagebox.showinfo(now, output)
CLOSE
def on_closing():
if tkinter.messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol(on_closing)
root.mainloop()
I need help in closing or ending this dialogue box
You could take two other approaches to this, one being quit() and simply ending the code all together, or using frames in tkinter and calling a different frame whether it says close or comes up blank. It would be able to cover everything already there

Resources