2

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?

kiri
  • 2,522
  • 4
  • 26
  • 44

4 Answers4

3

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.

Community
  • 1
  • 1
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Unfortunately it did not work for me in Python 3.4.4 (even after changing MessageBox for the newer messagebox capitalization). And when I did r.option_add('*font', 'Courier 12') it changed all the fonts except the messagebox fonts. – CuriousMarc Dec 27 '17 at 21:59
  • @CuriousMarc Nor for me in Python 3.6.5. – C. R. Yasuo Aug 28 '18 at 12:49
3

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')
cyberthanasis
  • 209
  • 2
  • 4
0

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.

Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
-5

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!

Edit: This is a very old answer, 3 years later someone said it's possible: Control Font in tkMessageBox