Tkinter MessageBox对齐

时间:2016-12-26 01:16:13

标签: tkinter

Look at the img.

有人可以帮我将消息框中的文字与中心对齐。感谢

编辑:预期结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用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的:

enter image description here

BTW:您可以找到包含留言箱代码的文件

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

enter image description here

GitHub上的

代码:furas/python-examples/tkinter/messagebox/own-messagebox