改变 tkinter 消息框的大小

change size of tkinter messagebox

在 python 中,我正在尝试更改 tkinter 消息框的宽度 window 以便文本可以放在一行中。

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
messagebox.showinfo("info","this information goes beyond the width of the messagebox")
root.mainloop()

无法调整消息框的大小。

When to use the Message Widget

The widget can be used to display short text messages, using a single font. You can often use a plain Label instead. If you need to display text in multiple fonts, use a Text widget. -effbot

另见:

@CharleyPathak 是正确的。您要么需要在文本中间放置一个换行符,因为消息框可以显示多行,要么创建一个自定义对话框。

我设法找到了适合我的尺码 "tkMessageBox.showinfo(title="帮助", message = str(readme))" 这样:

我想显示帮助文件 (readme.txt)。

def helpfile(filetype):
    if filetype==1:
        with open("readme.txt") as f:
            readme = f.read()
            tkMessageBox.showinfo(title="Help", message = str(readme))

我打开文件 readme.txt 并对其进行了编辑,使所有行的长度不超过 65 个字符。这对我很有效。我认为重要的是不要有中间包含 CR/LF 的长行。所以请正确格式化txt文件。

这是另一种方法,可以达到您想要的效果,但不使用消息框。它看起来更长,但它只是在定制方面提供了更多。

def popupmsg():
    popup = tk.Tk()

    def leavemini():
        popup.destroy()

    popup.wm_title("Coming Soon")
    popup.wm_attributes('-topmost', True)     # keeps popup above everything until closed.
    popup.wm_attributes("-fullscreen", True)  # I chose to make mine fullscreen with transparent effects.
    popup.configure(background='#4a4a4a')     # this is outter background colour
    popup.wm_attributes("-alpha", 0.95)       # level of transparency
    popup.config(bd=2, relief=FLAT)           # tk style

    # this next label (tk.button) is the text field holding your message. i put it in a tk.button so the sizing matched the "close" button
    # also want to note that my button is very big due to it being used on a touch screen application.

    label = tk.Button(popup, text="""PUT MESSAGE HERE""", background="#3e3e3e", font=headerfont,
                      width=30, height=11, relief=FLAT, state=DISABLED, disabledforeground="#3dcc8e")
    label.pack(pady=18)
    close_button = tk.Button(popup, text="Close", font=headerfont, command=leavemini, width=30, height=6,
                             background="#4a4a4a", relief=GROOVE, activebackground="#323232", foreground="#3dcc8e",
                             activeforeground="#0f8954")
    close_button.pack()