2016-12-26 217 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