[英]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.