2

I want to create information dialogue with tkMessagebox with a fixed width. I didn't see any options in the tkMessagebox.showinfo function that can handle this. Is there any way? Thanks!

4

2 回答 2

2

据我所知,您无法调整 tkMessageBox 的大小,但如果您愿意付出努力,您可以创建自定义对话框。

这个小脚本演示了它:

from tkinter import * #If you get an error here, try Tkinter not tkinter

def Dialog1Display():
    Dialog1 = Toplevel(height=100, width=100) #Here

def Dialog2Display():
    Dialog2 = Toplevel(height=1000, width=1000) #Here

master=Tk()

Button1 = Button(master, text="Small", command=Dialog1Display)
Button2 = Button(master, text="Big", command=Dialog2Display)

Button1.pack()
Button2.pack()
master.mainloop()

当您运行脚本时,您应该会看到一个带有两个按钮的主窗口,按下其中一个按钮后,您将创建一个TopLevel可以调整大小的窗口,如#Here. 这些顶级窗口就像标准窗口一样,可以调整大小并具有子窗口小部件。此外,如果您尝试将子小部件打包或网格化到TopLevel窗口中,那么您需要使用.geometrynot -widthor -height,这将是这样的:

from tkinter import *

def Dialog1Display():
    Dialog1 = Toplevel()
    Dialog1.geometry("100x100")

def Dialog2Display():
    Dialog2 = Toplevel()
    Dialog2.geometry("1000x1000")

master=Tk()

Button1 = Button(master, text="Small", command=Dialog1Display)
Button2 = Button(master, text="Big", command=Dialog2Display)

Button1.pack()
Button2.pack()
master.mainloop()

希望我有所帮助,请尝试在此处阅读TopLevel小部件:http: //effbot.org/tkinterbook/toplevel.htm

于 2015-02-04T21:00:35.523 回答
2

.option_add 可能仅适用于 linux 操作系统,但您可以控制字体、换行位置和框的宽度:

    root.option_add('*Dialog.msg.font', 'Helvetica 24')
    root.master.option_add('*Dialog.msg.width', 34)
    root.master.option_add("*Dialog.msg.wrapLength", "6i")

(其中“6i”是线的长度,以英寸为单位)

于 2020-06-10T16:02:14.513 回答