2016-12-26 404 views
-2

Look at the img.Tkinterのメッセージボックスの位置を合わせ

いくつかのいずれかが中央にメッセージボックスにテキストを合わせるようにして私を助けることができます。おかげ


EDIT:期待される結果:

enter image description here

+0

あなたが何を意味するのか「センター"? 1つの行を別の行の中心に置き、ウィンドウの四角形の中央のテキスト?独自のウィンドウを作成することができます( 'Label'で' tk.Toplevel'を使用し、テキストを整列します:[example](https://github.com/furas/python-examples/tree/master/tkinter/align-grid-pack ).BTW: '\ 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


あなたがメッセージボックスコードを含むファイル

import tkinter.messagebox 

print(tkinter.messagebox.__file__) 

を見つけて、それが行われたかを確認するためにエディタで開くことができます。


EDIT:あなたもクラスMsgBoxを作成し、それを何度も使用することができます。

例は、クラス内のいくつかの要素を変更する方法を示していますGitHubの上のラベルのフォント、ボタンのテキストや位置

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


コード:furas/python-examples/tkinter/messagebox/own-messagebox

関連する問題