like the image, see how it lacks an X button, i am trying to figure out how to make a window
not have an X button.
CodePudding user response:
I don't think you can make such a window using Tkinter, however, you can delete the title bar and make it again, without the close button:
Here is some code:
from tkinter import *
root = Tk()
root.overrideredirect(True)
root.geometry('400x100 200 200')
title_bar = Frame(root, bg='white', relief='raised', bd=2)
window = Canvas(root, bg='black')
title_bar.pack(expand=1, fill=X)
window.pack(expand=1, fill=BOTH)
root.mainloop()
In case of Mac computers:
from tkinter import *
root = Tk()
root.overrideredirect(1)
root.overrideredirect(0)
root.geometry('400x100 200 200')
title_bar = Frame(root, bg='white', relief='raised', bd=2)
window = Canvas(root, bg='black')
title_bar.pack(expand=1, fill=X)
window.pack(expand=1, fill=BOTH)
root.mainloop()
Note: This code is taken from here.
If you run this program, you will find a custom title bar that doesn't have the close button.
CodePudding user response:
If you want to edit the title bar, the way you want with Tkinter, you will have to turn off the title bar and rebuild all those methods from scratch.
But for this case of disabling "Close Button", you have 3 options: using a callback function, or using root.overriderdirect(True)
, or using root.attributes('-disabled', True)
Refer to this question
Simplest method I would suggest to disable "X" button is this:
from tkinter import *
from tkinter import ttk
win= Tk()
win.geometry("750x250")
def close_win():
win.destroy()
def disable_event():
pass
btn = ttk.Button(win, text ="Click here to Close",command=close_win)
btn.pack()
win.protocol("WM_DELETE_WINDOW", disable_event)
win.mainloop()