Tkinter消息框对齐

Tkinter消息框对齐,tkinter,Tkinter,有人能帮我把留言框里的文字对齐到中间吗。谢谢 编辑:预期结果: 您可以使用Toplevel()创建自己的消息窗口,然后您可以执行您想要的操作 import tkinter as tk # --- functions --- def about(): win = tk.Toplevel() win.title("ABOUT") l = tk.Label(win, text="One\ntwo two\nThree Three T

有人能帮我把留言框里的文字对齐到中间吗。谢谢


编辑:预期结果:

您可以使用
Toplevel()
创建自己的消息窗口,然后您可以执行您想要的操作

import tkinter as tk

# --- functions ---

def about():
    win = tk.Toplevel()
    win.title("ABOUT")
    
    l = tk.Label(win, text="One\ntwo two\nThree Three Three", bg='white')
    l.pack(ipadx=50, ipady=10, fill='both', expand=True)
    
    b = tk.Button(win, text="OK", command=win.destroy)
    b.pack(pady=10, padx=10, ipadx=20, side='right')
    
# --- main ---

root = tk.Tk()

b = tk.Button(root, text="About", command=about)
b.pack(fill='x', expand=True)

b = tk.Button(root, text="Close", command=root.destroy)
b.pack(fill='x', expand=True)

root.mainloop()
Linux:


顺便说一句:您可以找到带有messagebox代码的文件

import tkinter.messagebox

print(tkinter.messagebox.__file__)
然后在编辑器中打开,查看它是如何制作的


编辑:您还可以创建类
MsgBox
,并多次使用它

示例显示如何更改类中的某些元素:标签字体、按钮文本和位置

import tkinter as tk

# --- classes ---
# you can put this in separated file (it will need `import tkinter`)

import tkinter 

class MsgBox(tkinter.Toplevel):

    def __init__(self, title="MsgBox", message="Hello World"):
        tkinter.Toplevel.__init__(self)

        self.title(title)
        
        self.label = tkinter.Label(self, text=message)
        self.label['bg'] = 'white'
        self.label.pack(ipadx=50, ipady=10, fill='both', expand=True)

        self.button = tkinter.Button(self, text="OK")
        self.button['command'] = self.destroy
        self.button.pack(pady=10, padx=10, ipadx=20, side='right')
        
# --- functions ---

def about():

    msg = MsgBox("ABOUT", "One\nTwo Two\nThree Three Three")
    msg.label['font'] = 'Verdana 20 bold'
    msg.button['text'] = 'Close'
    msg.button.pack(side='left')
    
# --- main ---

root = tk.Tk()

b = tk.Button(root, text="About", command=about)
b.pack(fill='x', expand=True)

b = tk.Button(root, text="Close", command=root.destroy)
b.pack(fill='x', expand=True)

root.mainloop()


GitHub上的代码:

你说的“中心”是什么意思?将一行居中到另一行,在窗口矩形中居中文本?您始终可以创建自己的窗口(使用
tk.Toplevel
标签并对齐文本:。顺便说一句:
\n
后面似乎有空格。请看图片…我希望文本在中间。谢谢