更改tkinter消息框的大小

时间:2019-07-03 01:43:42

标签: python tkinter messagebox

在python中,我尝试更改tkinter消息框窗口的宽度,以使文本可以放在一行上。

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()

4 个答案:

答案 0 :(得分:0)

无法调整消息框的大小。

  

何时使用消息小部件

     

该小部件可用于使用单个字体显示 短消息 。您通常可以改用普通标签。如果需要以多种字体显示文本,请使用“文本”小部件。 -effbot

另请参阅:

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我设法拥有适合自己的尺寸 “ tkMessageBox.showinfo(title =” Help“,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文件。

答案 3 :(得分:0)

这是另一种获得您正在寻找的效果但不使用消息框的方法。它看起来更长,但它在定制方面提供了更多。

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()