【问题标题】:Change words on tkinter Messagebox buttons更改 tkinter 消息框按钮上的单词
【发布时间】:2013-04-26 18:16:59
【问题描述】:

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

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

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

这可能吗?

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

【问题讨论】:

  • 有可能,但我不建议这样做。为什么不使用 SimpleDialog?
  • 这似乎是不可能的。
  • @Johnny:我实际上过早地放弃了 tkinter.simpledialog,因为它看起来不如消息框灵活。是否可以用 simpledialog 覆盖按钮文本?
  • 这是不可能的,因为没有选项可以更改“ok”文本,但是如果您修改 tkinter 模块,这是可能的。这就是我不建议的

标签: python python-3.x tkinter messagebox


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你不能在 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()
      

      the messagebox will look like this

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多