简体   繁体   English

如何使用 Python tkinter 设置消息框的字体?

[英]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.我正在使用tkinter中提供的简单消息框,我想知道是否有任何方法可以更改字体。

This is the general idea of what I want, but the font= option doesn't work.这是我想要的一般想法,但font=选项不起作用。

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有关如何更改对话框文本的信息,请参见此处: 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.之后一定要调用r.option_clear()将字体设置回正常。

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. Tkinter 为 Windows 或 Mac 调用系统对话框并为 Linux 生成对话框。 In all cases is imposible change Tkinter dialogs.在所有情况下都是不可能改变 Tkinter 对话框的。

you can't.你不能。 write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!使用顶层widgted (tkinter.Toplevel()) 和标签编写您自己的消息框!

Something like this (from http://effbot.org/tkinterbook/label.htm )像这样的东西(来自http://effbot.org/tkinterbook/label.htm

from Tkinter import *

master = Tk()

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

mainloop()

i hope it helps!我希望它有帮助!

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

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