2016-12-26 158 views
-2

Look at the img.Tkinter的消息框对齐

有人可以帮助我在消息框中的文本对齐到中心。由于


编辑:预期结果:

enter image description here

+0

你是什么意思“中心“?居中一行到另一行,在窗口矩形中居中文本?你总是可以创建自己的窗口(使用带'Label'的'tk.Toplevel'并对齐文本:[示例](https://github.com/furas/python-examples/tree/master/tkinter/align-grid-pack )。顺便说一句:它看起来像'\ n'后面有空格 – furas

+0

https://i-msdn.sec.s-msft.com/dynimg/IC86459.jpeg看图片...我想要的文字是在中心。谢谢 –

回答

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