简体   繁体   中英

How to set font of a messagebox with Python tkinter?

I'm using the simple message-boxes provided in tkinter and I'm wondering if there's any way to change the font.

This is the general idea of what I want, but the font= option doesn't work.

from tkinter import Tk
import tkinter.messagebox as tkmsg

_root = Tk()
_root.withdraw()
tkmsg.showinfo(
    "Info",
    "Some monospaced text",
    font=("Monospace", 15)
)
_root.destroy()

Is there any way to change the font or do I have to use a custom dialog?

See here for how to change the dialog box text: Control Font in tkMessageBox

In short (copied verbatim from the link above):

You can configure the font for just dialog boxes by doing the following:

 from Tkinter import * import tkMessageBox r = Tk() r.option_add('*Dialog.msg.font', 'Helvetica 12') tkMessageBox.showinfo(message='Hello')

Be sure to call r.option_clear() to set the font back to normal afterwards.

You can change the default font for captions:

import tkinter as tk
from tkinter import messagebox as mb
from tkinter import font
root = tk.Tk() 
font1 = font.Font(name='TkCaptionFont', exists=True)
font1.config(family='courier new', size=20)
mb.showinfo(message='Hello')

You should write your own messagbox. Tkinter invoke system dialog for Windows or Mac and genetate dialogs for Linux. In all cases is imposible change Tkinter dialogs.

you can't. write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!

Something like this (from http://effbot.org/tkinterbook/label.htm )

from Tkinter import *

master = Tk()

w = Label(master, text="Hello, world!")
w.pack()

mainloop()

i hope it helps!

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