简体   繁体   中英

Change words on tkinter Messagebox buttons

I'm using tkinter's "askokcancel" message box to warn the user, with a pop-up, of an irreversible action.

from tkinter import Tk
Tk().withdraw()
from tkinter.messagebox import askokcancel
askokcancel("Warning", "This will delete stuff")

I'd like to change the text of the 'OK' button (from 'OK') to something like 'Delete', to make it less benign-looking.

Is this possible?

If not, what is another way to achieve it? Preferably without introducing any dependancies...

Why not open a child window thus creating your own box with your own button like this:

from tkinter import *
def messageWindow():
    win = Toplevel()
    win.title('warning')
    message = "This will delete stuff"
    Label(win, text=message).pack()
    Button(win, text='Delete', command=win.destroy).pack()
root = Tk()
Button(root, text='Bring up Message', command=messageWindow).pack()
root.mainloop()

No, there is no way to change the text of the buttons for the built-in dialogs.

It's not very hard to do, and it gives you absolute control over what is in the dialog widget.

It is right that you can not change names in tkinter messagebox but you can make your own class of messagebox so that you can reuse it several times.following code is just same as tkinter messagebox but it has args , kwargs as argumesnts. it is just for easiness. I am also learning so code has not flash and alarm. The person who will edit my code i will give thanks to him/her.

class Message(object):
    def __init__(self,parent, *args, **kwargs):
        self.parent=parent
        top=Toplevel(self.parent)
        top.geometry("400x200")
        top.transient(self.parent)

        top.title(args[0])
        f0=Frame(top)
        top.l1=Label(f0, text=args[1])
        top.l1.pack(pady=30)
        f0.pack()
        top.grab_set()
        top.f1=Frame(top)
        for key in kwargs:
            key=Button(top.f1, text=f"{kwargs[key]}")
            key.pack(side=LEFT)
        top.f1.pack()

the messagebox will look like this

No, We Can't Change The Text Of The Buttons In The Messagebox. But We Can Create A New Window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM