Python中的消息框

时间:2008-11-02 21:22:30

标签: python user-controls user-interface

是否有用于在python中创建消息框或输入框的UI库?

4 个答案:

答案 0 :(得分:11)

你最好的选择是tkMessageBox模块,它应该适用于所有系统(因为Python通常会附带Tkinter)。

如果您可以限制自己使用特定的操作系统,可能会有更好的选择。

答案 1 :(得分:3)

可以使用EasyGui创建简单的消息框和输入框,EasyGui是一个使用Tkinter的小型库,Python随附。

您可以在此处获取EasyGui:http://easygui.sourceforge.net/

答案 2 :(得分:2)

我听说过关于wx python的好东西,它也是多平台的。

答案 3 :(得分:1)

来自Tkinter的

import *
import os

    class Dialog(Toplevel):

    def __init__(self, parent, title = None):

        Toplevel.__init__(self, parent)
        self.transient(parent)

        if title:
            self.title(title)

        self.parent = parent

        self.result = None

        body = Frame(self)
        self.initial_focus = self.body(body)
        body.pack(padx=5, pady=5)

        self.buttonbox()

        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = self

        self.protocol("WM_DELETE_WINDOW", self.cancel)

        self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                                  parent.winfo_rooty()+50))

        self.initial_focus.focus_set()

        self.wait_window(self)

    #
    # construction hooks

    def body(self, master):
        # create dialog body.  return widget that should have
        # initial focus.  this method should be overridden

        pass

    def buttonbox(self):
        # add standard button box. override if you don't want the
        # standard buttons

        box = Frame(self)

        w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
        w.pack(side=LEFT, padx=5, pady=5)
        w = Button(box, text="Cancel", width=10, command=self.cancel)
        w.pack(side=LEFT, padx=5, pady=5)

        self.bind("<Return>", self.ok)
        self.bind("<Escape>", self.cancel)

        box.pack()

    #
    # standard button semantics

    def ok(self, event=None):

        if not self.validate():
            self.initial_focus.focus_set() # put focus back
            return

        self.withdraw()
        self.update_idletasks()

        self.apply()

        self.cancel()

    def cancel(self, event=None):

        # put focus back to the parent window
        self.parent.focus_set()
        self.destroy()

    #
    # command hooks

    def validate(self):

        return 1 # override

    def apply(self):

        pass # override