繁体   English   中英

更改 tkinter 消息框按钮上的单词

[英]Change words on tkinter Messagebox buttons

我正在使用 tkinter 的“askokcancel”消息框通过弹出窗口警告用户不可逆转的操作。

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

我想将“确定”按钮的文本(从“确定”)更改为“删除”之类的内容,以使其看起来不那么温和。

这可能吗?

如果没有,还有什么方法可以实现它? 最好不要引入任何依赖项......

为什么不打开一个子窗口,从而使用您自己的按钮创建您自己的框,如下所示:

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()

不,无法更改内置对话框的按钮文本。

您最好的选择是创建自己的对话框。 这并不难,它使您可以完全控制对话框小部件中的内容。

正确的是,您不能在 tkinter 消息框中更改名称,但您可以创建自己的消息框类,以便您可以多次重复使用它。以下代码与 tkinter 消息框相同,但它具有 args 、 kwargs 作为参数。 这只是为了方便。 我也在学习所以代码没有闪烁和警报。 编辑我的代码的人我会感谢他/她。

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()

消息框看起来像这样

不,我们不能更改消息框中按钮的文本。 但我们可以创建一个新窗口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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