Tkinter 消息框对齐 [英] Tkinter MessageBox Align

查看:39
本文介绍了Tkinter 消息框对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我将消息框中的文本与中心对齐吗?谢谢

Can some one help me with aligning the text in the message box to the center. Thanks

预期结果:

推荐答案

您可以使用 Toplevel() 来创建自己的消息窗口,然后就可以为所欲为.

You can use Toplevel() to create own message window and then you can do what you want.

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:

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

BTW: you can find file with messagebox code

import tkinter.messagebox

print(tkinter.messagebox.__file__)

然后在编辑器中打开以查看它是如何制作的.

and then open in editor to see how it was made.

您还可以创建类 MsgBox 并多次使用它.

you can also create class MsgBox and use it many times.

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

Example shows how to change some elements in class: label font, button text and position

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 上的代码:furas/python-examples/tkinter/messagebox/own-messagebox

code on GitHub: furas/python-examples/tkinter/messagebox/own-messagebox

这篇关于Tkinter 消息框对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆