【问题标题】:Message Box in PythonPython中的消息框
【发布时间】:2010-09-20 10:05:38
【问题描述】:

是否有UI库可以在python中创建消息框或输入框?

【问题讨论】:

    标签: python user-controls user-interface


    【解决方案1】:

    您最好的选择是 tkMessageBox 模块,它应该适用于所有系统(因为 Python 通常会与 Tkinter 一起提供)。

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

    【讨论】:

      【解决方案2】:

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

      您可以在这里获取 EasyGui:http://easygui.sourceforge.net/

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          来自 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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-02
            • 1970-01-01
            • 2020-10-07
            • 2017-04-29
            • 1970-01-01
            • 2021-08-04
            相关资源
            最近更新 更多