简体   繁体   English

更改 tkinter 消息框的大小

[英]change size of tkinter messagebox

In python, I am attempting the change the width of the tkinter messagebox window so that text can fit on one line.在 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()

It's not possible to adjust the size of messagebox.无法调整消息框的大小。

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.您通常可以使用普通的 Label 来代替。 If you need to display text in multiple fonts, use a Text widget.如果您需要以多种字体显示文本,请使用文本小部件。 - effbot -效果器

Also see:另见:

@CharleyPathak is correct. @CharleyPathak 是正确的。 You either need to put a newline in the middle of the text, because message boxes can display multiple lines, or create a custom dialog box.您要么需要在文本中间放置一个换行符,因为消息框可以显示多行,要么创建一个自定义对话框。

I managed to have a proper size for my "tkMessageBox.showinfo(title="Help", message = str(readme))" this way:我设法通过这种方式为我的“tkMessageBox.showinfo(title="Help", message = str(readme))”设置了合适的大小:

I wanted to show a help file (readme.txt).我想显示一个帮助文件(readme.txt)。

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

I opened the file readme.txt and EDITED IT so that the length of all lines did not exeed about 65 chars.我打开文件 readme.txt 并编辑它,以便所有行的长度不超过 65 个字符。 That worked well for me.这对我来说效果很好。 I think it is important NOT TO HAVE LONG LINES which include CR/LF in between.我认为重要的是不要有很长的行,其中包括中间的 CR/LF。 So format the txt file properly.所以正确格式化txt文件。

Heres another method that gets the effect youre looking for but doesnt use messagebox.这是另一种获得您正在寻找的效果但不使用消息框的方法。 it looks a lot longer but it just offers much more in terms of customization.它看起来更长,但它只是在定制方面提供了更多。

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

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

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